Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How use multiprocessing.Pool.map with iterator and separat input for function

How can I pass complete parameters, without splitting them among the cores, to function myfun and at the same time pass the respective element from the collection [1,2,3,4,5]

p=Pool(5)
p.map(myfun(df_A,df_B),[1,2,3,4,5])

If I implement it this way, the function gets the parameters df_A and df_B but not an element from the collection

Here is an example how myfun can look like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def myfunc(df_A, df_B, e):
    do_something
    print(df_A.iloc[e],df_A.iloc[e])

e is one element of the collection [1,2,3,4,5]

>Solution :

You could consider something like this (there are probably better ways):

from multiprocessing import Pool

def myfunc(a, b, c):
    print(a, b, c)

df_A = 1
df_B = 2

def main():
    with Pool() as pool:
        pool.starmap(myfunc,[[df_A, df_B, x] for x in range(1, 6)])

if __name__ == '__main__':
    main()

Output:

1 2 1
1 2 2
1 2 3
1 2 4
1 2 5

EDIT: Now using starmap because knowledge of the myfunc function signature was made available after my original post

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading