使用Pool和multiprocessing同时将两个函数应用于两个列表 [英] Applying two functions to two lists simultaneously using Pool and multiprocessing

查看:268
本文介绍了使用Pool和multiprocessing同时将两个函数应用于两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(大)名单,其中有男性特工和女性特工.

I have a (large) list with male and female agentes.

我想对每个应用不同的功能.

I want to apply different functions to each.

在这种情况下如何使用Pool?鉴于代理之间是相互独立的.

How can I use Pool in such a case? Given that the agents are independent of each other.

一个例子是:

males = ['a', 'b', 'c']
females = ['d', 'e', 'f']
for m in males:
    func_m(m)
for f in females:
    func_f(f)

我是这样开始的:

from multiprocessing import Pool
p = Pool(processes=2)
p.map() # Here is the problem

我想要类似的东西:

p.ZIP(func_f for f in females, func_m for m in males) # pseudocode

推荐答案

可以使用map_async异步启动计算.这将启动所有需要的工作,然后您可以对结果使用get方法将它们收集在一个列表中.

This is possible to launch the computation asynchronously using map_async. This launches all the job needed and you can then collect them in a single list using the get method on the results.

from multiprocessing import Pool

pool = Pool(4)
res_male = pool.map_async(func_m, males)
res_females = pool.map_async(fun_f, females)

res = res_male.get()
res.extend(res_females.get())

您还可以使用更现代的concurrent.futures API,该API对于此类计算更直观.

You could also look to the more modern concurrent.futures API which is more intuitive for this kind of computations.

这篇关于使用Pool和multiprocessing同时将两个函数应用于两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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