主进程完成后,子进程如何充当守护进程

时间:2018-04-09 19:11:43

标签: python python-multiprocessing python-daemon

我想知道即使主要流程存在,如何让子流程继续运行? 而且游泳池案也可以这样做吗?

import os
import time
from multiprocessing import Process, current_process

def doubler(number):
    result = number * 2
    proc_name = current_process().name
    print('{0} doubled to {1} by: {2}'.format( number, result, proc_name))
    time.sleep(2)
    print('eixt{0}'.format(proc_name))

def doubler1():
    print("manse!!!")

if __name__ == '__main__':
    numbers = [5, 10, 15, 20, 25]
    procs = []

    for index, number in enumerate(numbers):
        proc = Process(target=doubler, args=(number,))
        #proc.daemon = True
        procs.append(proc)
        proc.start()

    proc = Process(target=doubler1)
    procs.append(proc)
    proc.start()

    print('main process exit')

   # for proc in procs:
   #     proc.join()

0 个答案:

没有答案