【发布时间】:2019-06-21 15:51:25
【问题描述】:
我想我需要一些建议。以下是我的代码:
from multiprocessing import Pool
import time
import sys
def testing(number):
count = 0
while True:
print('Count: {}'.format(count))
count += 1
if count > number:
print('Exiting...')
sys.exit()
else:
print('Looping Over')
time.sleep(1)
if __name__ == '__main__':
with Pool(2) as p:
p.map(testing, [3, 2])
预期结果:
一旦所有子线程都退出,程序(主线程)应该退出。
实际结果:
$ python3 test_exit.py
Count: 0
Looping Over
Count: 0
Looping Over
Count: 1
Looping Over
Count: 1
Looping Over
Count: 2
Looping Over
Count: 2
Exiting... <<< Exited 1st thread.
Count: 3
Exiting... <<< Exited 2nd thread.
....and it stays here as if stuck or something. It never gives control back to Shell.
预期结果:
$ python3 test_exit.py
Count: 0
Looping Over
Count: 0
Looping Over
Count: 1
Looping Over
Count: 1
Looping Over
Count: 2
Looping Over
Count: 2
Exiting...
Count: 3
Exiting...
$ <<< Note: I am expecting to be dropped back to Shell prompt
问题:
就池/地图使用而言,我的方法有什么问题吗?
【问题讨论】: