使用 python 进行多处理

发布于 2024-09-26 01:43:44 字数 372 浏览 2 评论 0原文

我如何控制这个函数池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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鱼窥荷 2024-10-03 01:43:44

存储 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.

衣神在巴黎 2024-10-03 01:43:44

您确定需要知道您的两名员工中哪一位正在做什么?在这种情况下,您可能最好使用进程和队列,因为这听起来需要多个进程之间进行一些通信。

如果您只想知道哪个结果由哪个工作进程处理,您可以简单地返回一个元组:

#!/usr/bin/python
import multiprocessing

def fun(..)
...
    return value, multiprocessing.current_process()._name

my_pool = multiprocessing.Pool(2)
async_result = []

for i in range(5):
    async_result.append(my_pool.apply_async(fun, [i]))

# some code going to be here....

my_pool.join()
result = {}
for i in range(5):
    result[i] = async_result[i].get()

如果您有不同的输入变量作为列表,则 map_async 命令可能是更好的决定

#!/usr/bin/python
import multiprocessing

def fun(..)
...
...
    return value, multiprocessing.current_process()._name

my_pool = multiprocessing.Pool()

async_results = my_pool.map_async(fun, range(5))

# some code going to be here....

results = async_results.get()

:最后一行加入池。请注意,结果是一个元组列表,每个元组包含您的计算值和计算该值的进程的名称。

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:

#!/usr/bin/python
import multiprocessing

def fun(..)
...
    return value, multiprocessing.current_process()._name

my_pool = multiprocessing.Pool(2)
async_result = []

for i in range(5):
    async_result.append(my_pool.apply_async(fun, [i]))

# some code going to be here....

my_pool.join()
result = {}
for i in range(5):
    result[i] = async_result[i].get()

If you have the different input variables as a list, the map_async command might be a better decision:

#!/usr/bin/python
import multiprocessing

def fun(..)
...
...
    return value, multiprocessing.current_process()._name

my_pool = multiprocessing.Pool()

async_results = my_pool.map_async(fun, range(5))

# some code going to be here....

results = async_results.get()

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文