Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

390
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!