問題描述
在 Python 中,我看到了許多調用多處理但目標只是打印一些內容的示例.我有一個場景,其中目標返回 2 個變量,我需要稍后使用.例如:
In Python I have seen many examples where multiprocessing is called but the target just prints something. I have a scenario where the target returns 2 variables, which I need to use later. For example:
def foo(some args):
a = someObject
b = someObject
return a,b
p1=multiprocess(target=foo,args(some args))
p2=multiprocess(target=foo,args(some args))
p3=multiprocess(target=foo,args(some args))
現在呢?我可以執行 .start 和 .join,但如何檢索單個結果?我需要為我執行的所有作業捕獲返回 a,b,然后處理它.
Now what? I can do .start and .join, but how do I retrieve the individual results? I need to catch the return a,b for all the jobs I execute and then work on it.
推薦答案
是的,當然 - 您可以使用多種方法.最簡單的方法之一是共享 Queue
.在此處查看示例:http://eli.thegreenplace.net/2012/01/16/python-parallelizing-cpu-bound-tasks-with-multiprocessing/
Yes, sure - you can use a number of methods. One of the easiest ones is a shared Queue
. See an example here: http://eli.thegreenplace.net/2012/01/16/python-parallelizing-cpu-bound-tasks-with-multiprocessing/
這篇關于是否可以對在 Python 中返回某些內容的函數進行多處理?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!