Is it possible to multiprocess a function that returns something in Python?(是否可以对在 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 中返回某些内容的函数进行多处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否可以对在 Python 中返回某些内容的函数进行多处理?


- 计算测试数量的Python单元测试 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01