我有一个简单的测试程序,使用 Python 3.6.3 编写并执行。它在一台拥有四个核心的机器上运行。
import multiprocessing
import time
def f(num):
print(multiprocessing.current_process(), num)
time.sleep(1)
if (num % 2):
raise Exception
pool = multiprocessing.Pool(5)
try:
pool.map(f, range(1,20))
except Exception as e:
print("EXCEPTION")
pool.close()
pool.join()
使用 pool = multiprocessing.Pool(5)
输出:
<ForkProcess(ForkPoolWorker-1, started daemon)> 1
<ForkProcess(ForkPoolWorker-2, started daemon)> 2
<ForkProcess(ForkPoolWorker-3, started daemon)> 3
<ForkProcess(ForkPoolWorker-4, started daemon)> 4
<ForkProcess(ForkPoolWorker-5, started daemon)> 5
<ForkProcess(ForkPoolWorker-2, started daemon)> 6
<ForkProcess(ForkPoolWorker-1, started daemon)> 7
<ForkProcess(ForkPoolWorker-4, started daemon)> 8
<ForkProcess(ForkPoolWorker-3, started daemon)> 9
<ForkProcess(ForkPoolWorker-5, started daemon)> 10
<ForkProcess(ForkPoolWorker-2, started daemon)> 11
<ForkProcess(ForkPoolWorker-1, started daemon)> 12
<ForkProcess(ForkPoolWorker-4, started daemon)> 13
<ForkProcess(ForkPoolWorker-3, started daemon)> 14
<ForkProcess(ForkPoolWorker-5, started daemon)> 15
<ForkProcess(ForkPoolWorker-2, started daemon)> 16
<ForkProcess(ForkPoolWorker-1, started daemon)> 17
<ForkProcess(ForkPoolWorker-3, started daemon)> 18
<ForkProcess(ForkPoolWorker-4, started daemon)> 19
EXCEPTION
但是,如果我将进程池的处理器数量更改为等于或小于我的计算机上的核心数,那么每次调用f()
(其中num
为偶数)时,都不会打印输出。
使用pool = multiprocessing.Pool(4)
的输出结果:
<ForkProcess(ForkPoolWorker-1, started daemon)> 1
<ForkProcess(ForkPoolWorker-2, started daemon)> 3
<ForkProcess(ForkPoolWorker-3, started daemon)> 5
<ForkProcess(ForkPoolWorker-2, started daemon)> 7
<ForkProcess(ForkPoolWorker-1, started daemon)> 9
<ForkProcess(ForkPoolWorker-3, started daemon)> 11
<ForkProcess(ForkPoolWorker-3, started daemon)> 13
<ForkProcess(ForkPoolWorker-1, started daemon)> 15
<ForkProcess(ForkPoolWorker-2, started daemon)> 17
<ForkProcess(ForkPoolWorker-1, started daemon)> 19
EXCEPTION
我不明白为什么这些进程会被终止,特别是当异常直到函数中的打印语句之后才被抛出。更让我困惑的是,只有在池中的进程计数等于或小于机器上的核心数时,才会发生这种情况。
if __name__ == '__main__':
- Jean-François Fabre