Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

391
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda