是否可以对在Python中返回某些内容的函数进行多进程处理? [英] Is it possible to multiprocess a function that returns something in Python?

查看:58
本文介绍了是否可以对在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/

这篇关于是否可以对在Python中返回某些内容的函数进行多进程处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆