[英]How to get the result of multiprocessing.Pool.apply_async
解決方案非常簡單:
import multiprocessing
def func():
return 2**3**4
p = multiprocessing.Pool()
result = p.apply_async(func).get()
print(result)
由於Pool.apply_async()
返回AsyncResult
,因此您只需從AsyncResult.get()
方法獲取結果即可。
希望這可以幫助!
一個簡單的方法是擁有一個這樣的輔助類:
class Result():
def __init__(self):
self.val = None
def update_result(self, val):
self.val = val
result = Result()
def f(x):
return x*x
pool.apply_async(f, (10,), callback=result.update_result)
當線程運行並計算結果時,它將調用你的回調函數來更新result.val。
無論如何,您需要檢查線程是否已完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.