Python close children when closing main process

2019-07-20 19:57发布

I have have a main process that forks a number of subprocesses. I want to be able to kill these child processes off when my main process gets the kill signal. Ideally I would want to do something along the lines of:

def handler(signum, frame, pid_list):
    log('Killing Process')
    for pid in pid_list:
       os.kill(pid, signal.SIGTERM)
       os.waitpid(pid, 0)         # need
    sys.exit()

if __name__ == "__main__":
    <code that creates child processes, pids>
    signal.signal(signal.SIGTERM, handler(pid_list))

But of course, that doesn't work... any suggestions?

2条回答
做个烂人
2楼-- · 2019-07-20 20:37

As @tony suggested you could set daemon=True flag on a child process created using multiprocessing module. To install it on python2.4, type: pip install multiprocessing.

The child processes won't be terminated if the main process is killed by a signal so you need to provide an appropriate signal handler:

#!/usr/bin/env python
import logging, signal, sys, time
import multiprocessing as mp # `pip install multiprocessing` on Python <2.6

class AddProcessNameFilter(logging.Filter):
    """Add missing on Python 2.4 `record.processName` attribute."""
    def filter(self, r):
        r.processName = getattr(r, 'processName', mp.current_process().name)
        return logging.Filter.filter(self, r)

def print_dot():
    while True:
        mp.get_logger().info(".")
        time.sleep(1)

def main():
    logger = mp.log_to_stderr()
    logger.setLevel(logging.INFO)
    logger.addFilter(AddProcessNameFilter()) # fix logging records

    # catch TERM signal to allow finalizers to run and reap daemonic children
    signal.signal(signal.SIGTERM, lambda *args: sys.exit(-signal.SIGTERM))

    # create daemonic child processes
    processes = [mp.Process(target=print_dot) for _ in range(2)]
    for p in processes:
        p.daemon = True
        p.start()    
    print_dot()

if __name__=="__main__":
    mp.freeze_support()
    main()
查看更多
3楼-- · 2019-07-20 20:43

What about use this flag when you create a subprocess?

查看更多
登录 后发表回答