Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying two functions to two lists simultaneously using Pool and multiprocessing

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

I want to apply different functions to each.

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

An example would be:

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

I started like that:

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

I would like to have something like:

p.ZIP(func_f for f in females, func_m for m in males) # pseudocode
like image 734
B Furtado Avatar asked Oct 30 '22 11:10

B Furtado


1 Answers

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())

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

like image 99
Thomas Moreau Avatar answered Nov 11 '22 13:11

Thomas Moreau