1
爲什麼此代碼可以與線程一起使用,但不能處理?爲什麼我不能在提交給ProcessPoolExecutor的可調參數中創建/訪問期貨?
import concurrent.futures as f
import time
def wait_on_b():
time.sleep(2)
print(b.result())
return 5
def wait_5():
time.sleep(2)
return 6
THREADS = False
if THREADS:
executor = f.ThreadPoolExecutor()
else:
executor = f.ProcessPoolExecutor()
a = executor.submit(wait_on_b)
b = executor.submit(wait_5)
print(a.result()) # works fine if THREADS, BrokenProcessPool otherwise
的docs做警告:
調用執行人或提交給ProcessPoolExecutor可調用的方法,未來將導致死鎖。
該文檔似乎不提拋出一個異常,那麼是否意味着ProcessPoolExecutor
莫名其妙地發現了僵局,通過殺死這兩個進程得到解決呢?
更重要的是,這是爲什麼僵局不可避免的與流程(避免與線程),什麼是解決辦法,如果我想使用期貨多個進程,而不會因此受到限制?