說我這樣做:Pool.map - 爲什麼工作進程不會提前崩潰?
import multiprocessing as mp
def f(x):
raise OverflowError # raised BEFORE the print
print x
if __name__ == '__main__':
pool = mp.Pool(processes=1)
for _ in pool.imap_unordered(f, range(10)):
pass
pool.close()
pool.join()
日期:
Traceback (most recent call last):
File "test0.py", line 9, in <module>
for _ in pool.imap_unordered(f, range(10)):
File "/Users/usualme/anaconda/lib/python2.7/multiprocessing/pool.py", line 659, in next
raise value
OverflowError
好輸出是有道理的。在print
聲明之前引發異常,所以沒有輸出。現在幾乎相同的代碼,但我換2號線:
import multiprocessing as mp
def f(x):
print x
raise OverflowError # raised AFTER the print
if __name__ == '__main__':
pool = mp.Pool(processes=1)
for _ in pool.imap_unordered(f, range(10)):
pass
pool.close()
pool.join()
日期:
0
1
2
3
4
5
6
7
8
9
Traceback (most recent call last):
File "test0.py", line 9, in <module>
for _ in pool.imap_unordered(f, range(10)):
File "/Users/usualme/anaconda/lib/python2.7/multiprocessing/pool.py", line 659, in next
raise value
OverflowError
我不明白的輸出。我希望數字0跟着堆棧跟蹤,或者所有10個數字和10堆棧跟蹤。爲什麼它會打印所有的數字和只有一個堆棧跟蹤?爲什麼工作進程等待最終崩潰?