使用 python 进行多处理
我如何控制这个函数池apply_asyn的返回值 假设我有以下很酷的内容,
import multiprocessing:
de fun(..)
...
...
return value
my_pool = multiprocessing.Pool(2)
for i in range(5) :
result=my_pool.apply_async(fun, [i])
some code going to be here....
digest_pool.close()
digest_pool.join()
here i need to proccess the results
我如何控制每个进程的结果值并知道检查它属于哪个进程,
how can i control the return value of this function pool apply_asyn
supposing that I have the following cool
import multiprocessing:
de fun(..)
...
...
return value
my_pool = multiprocessing.Pool(2)
for i in range(5) :
result=my_pool.apply_async(fun, [i])
some code going to be here....
digest_pool.close()
digest_pool.join()
here i need to proccess the results
how can i control the result value for every proccess and know to check to which proccess it belongs ,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
存储 for 循环中“i”的值,然后打印它或返回并将其保存在其他地方。
因此,如果发生一个进程,您可以通过查看变量 i 来检查它是来自哪个进程。
希望这有帮助。
store the the value of 'i' from the for loop and either print it or return and save it somewhere else.
so if a process happens you can check from which process it was by looking at the variable i.
Hope this helps.
您确定需要知道您的两名员工中哪一位正在做什么?在这种情况下,您可能最好使用进程和队列,因为这听起来需要多个进程之间进行一些通信。
如果您只想知道哪个结果由哪个工作进程处理,您可以简单地返回一个元组:
如果您有不同的输入变量作为列表,则
map_async
命令可能是更好的决定:最后一行加入池。请注意,结果是一个元组列表,每个元组包含您的计算值和计算该值的进程的名称。
Are you sure, that you need to know, which of your two workers is doing what right now? In such a case you might be better off with Processes and Queues, because, this sounds as some communication between the multiple processes is required.
If you just want to know, which result was processed by which worker, you can simply return a tuple:
If you have the different input variables as a list, the
map_async
command might be a better decision:The last line joins the pool. Note, that results is a list of tuples, each tuple containing of your calculated value and the name of the process who calculated it.