Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

401
Visualizações
kill the sub processes by signal, but position affects?

I use the signal function to kill all sub-processes in the mul-process program, the code is shown blow, save as a file named mul_process.py:

import time
import os
import signal
from multiprocessing import Process

processes = []

def fun(x):
    print 'current sub-process pid is %s' % os.getpid()
    while True:
        print 'args is %s' % x
        time.sleep(100)

def term(sig_num, frame):
    print 'terminate process %d' % os.getpid()
    for p in processes:
        print p.pid
    try:
        for p in processes:
            print 'process %d terminate' % p.pid
            p.terminate()
            p.join()
    except Exception as e:
        print str(e)

if __name__ == '__main__':
    print 'current main-process pid is %s' % os.getpid()
    for i in range(3):
        t = Process(target=fun, args=(str(i),))
        t.start()
        processes.append(t)
    signal.signal(signal.SIGTERM, term)
    try:
        for p in processes:
            p.join()
    except Exception as e:
        print str(e)

Using 'python mul_process.py' to launch the program on Ubuntu 10.04.4 and Python 2.6, when it start running, in another tab, I use kill -15 with the main process pid to send signal SIGTERM to kill all processes, when the main process receive the signal SIGTERM, it exit after terminate all sub processes, but when I use kill -15 with the sub process pid, it does not work, the program still alive and running as before, and does not print the sentence defined in the function term, seems that the subprocess doesn't receive the SIGTERM.As I know, the sub process will inherit the signal handler, but it doesn`t work, here is the first question.

And then I move the line 'signal.signal(signal.SIGTERM, term)' to position after line 'if name == 'main':', like this:

if __name__ == '__main__':
    signal.signal(signal.SIGTERM, term)
    print 'current main-process pid is %s' % os.getpid()
    for i in range(3):
        t = Process(target=fun, args=(str(i),))
        t.start()
        processes.append(t)
    try:
        for p in processes:
            p.join()
    except Exception as e:
        print str(e) 

Launch the program, and use kill -15 with the main process pid to send the signal SIGTERM, the program receive the signal and call the function term but also doesn't kill any subprocessed and exit itself, this is the second question.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Few problems in your program- Agree that subprocess will inherit signal handler in your 2nd code snippet, But global variable "processes" list won't be shared. So list of process would be available with main process only. "process" would be empty list for other sub process. You can use queue or pipe kind of mechanism to pass list of process to sub processes. But it will bring another problem

You terminate process1 and handler of process1 try to terminate process2 to process4.

Now process 2 also has same handler,

So Process 2 handler again try to terminate all other process

which will push your program into infinite loop.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda