0
我正在嘗試多線程在Python 3中,但我不明白爲什麼我的示例將無法工作。它應該只是打印數字,並作爲確認再次打印數字。 但它會遇到很多錯誤。Python簡單ProcessPoolExecutor示例不會工作
代替from concurrent import futures
def print_me(num):
print('Zahl: ' + str(num))
return num
def say_bye(job):
num = job.result()
print(str(num) + ' is out!')
def test_multi():
num_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
with futures.ProcessPoolExecutor(max_workers=4) as executor:
for num in num_list:
job = executor.submit(print_me, num)
job.add_done_callback(say_bye(job))
test_multi()
從理論上說,:
for num in num_list:
job = executor.submit(print_me, num)
job.add_done_callback(say_bye(job))`
我可以利用這一點,還是?
executor.map(print_me, num_list)