我认为我需要一些建议。下面是我的代码:
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
问题:
我的连接池/映射使用方法有什么问题吗?