python退出Process多进程


import os
import signal
import sys
from multiprocessing import Process


class Watcher(object):

    def __init__(self):
        self.child = os.fork()
        if self.child == 0:
            return
        else:
            self.watch()

    def watch(self):
        try:
            os.wait()
        except KeyboardInterrupt:
            self.kill()
        sys.exit()

    def kill(self):
        try:
            os.kill(self.child, signal.SIGKILL)
        except OSError:
            pass


def start_worker():
    time.sleep(10)


if __name__ == '__main__':
    Watcher()  # Ctrl+c 杀死进程
    process_list = []
    for i in range(4):
        process_list.append(Process(target=start_worker, args=()))

    for p in process_list:
        p.start()

    for p in process_list:
        p.join()

你可能感兴趣的:(python,多进程)