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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With