是否可以对在 Python 中返回某些内容的函数进行多处理?
问题描述
在 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/
相关文章