我要做什么:
在创建多处理池后发生异常时,当父进程终止时,子进程将变成僵尸。 为了解决这个问题,我捕获了异常,并明确并收获了池中的所有子代。
问题是
这会导致父级挂起,而不是退出并返回到终端命令行。
后一种行为是意外的,我不知道为什么。但是回溯表明这是池中的问题。
问题是 为什么会挂起?
import multiprocessing as mp
def deadPool():
print("Everybody out of the pool")
active = mp.active_children()
for a in active:
print("reaping",a)
a.terminate()
print('deadpool')
try:
pool = mp.Pool(mp.cpu_count()//2)
print (" running with pool of ",mp.cpu_count()//2)
p = 1/0 # intentional bug to generate exception
finally:
deadPool()
这里是对此的调用:
python ~/Documents/testMp.py
running with pool of 4
Everybody out of the pool
reaping <ForkProcess(ForkPoolWorker-1, started daemon)>
reaping <ForkProcess(ForkPoolWorker-4, started daemon)>
reaping <ForkProcess(ForkPoolWorker-2, started daemon)>
reaping <ForkProcess(ForkPoolWorker-3, started daemon)>
deadpool
Traceback (most recent call last):
File "/Users/foo/testMp.py", line 15, in <module>
p = 1/0 # intentional bug
ZeroDivisionError: division by zero
这时父进程已挂起。
按Ctrl-c退出此挂起的进程可发出键盘异常,并返回到终端提示
^CError in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/Users/foo/anaconda3/envs/jupyter2/lib/python3.7/multiprocessing/util.py", line 265, in _run_finalizers
finalizer()
File "/Users/foo/anaconda3/envs/jupyter2/lib/python3.7/multiprocessing/util.py", line 189, in __call__
res = self._callback(*self._args, **self._kwargs)
File "/Users/foo/anaconda3/envs/jupyter2/lib/python3.7/multiprocessing/pool.py", line 581, in _terminate_pool
cls._help_stuff_finish(inqueue, task_handler, len(pool))
File "/Users/foo/anaconda3/envs/jupyter2/lib/python3.7/multiprocessing/pool.py", line 566, in _help_stuff_finish
inqueue._rlock.acquire()
KeyboardInterrupt
如您所见,它似乎卡在了“ _terminate_pool”中,这与多进程有关