0

My program is simply:

def mult(name,title):
    while True:
        name = str(name)+str(title)

pool = multiprocessing.pool(processes=5)
pool.map_async(mult,?...?)
pool.join()

My problem is how to pass the multiple arguments in "map_async()". I tried to supply them as an array like "map_async(mult,['go','stop']), but an exception is raised for missing arguments.

I want to pass the two arguments to function "mult" using map_async. How can I do it?

Mohammed Baashar
  • 484
  • 3
  • 6
  • 20

2 Answers2

1

Assuming that you're using Python 3, you can use starmap or starmap_async:

args = [("name", "title"), ("name2", "title2")]

def mult(name,title):
    return str(name)+str(title)

pool = multiprocessing.Pool(processes=5)
result = pool.starmap(mult,args)

>> ['nametitle', 'name2title2']

See it run

If you really want to use async:

args = [("name", "title"), ("name2", "title2")]

def mult(name,title):
  return str(name)+str(title)

pool = multiprocessing.Pool(processes=5)
map_result = pool.starmap_async(mult,args)
result = map_result.get(timeout = 2)

>> ['nametitle', 'name2title2']

See it run

I changed your mult function because it doesn't make any sense. It starts an infinite loop with while True and each loop assigns a value to name, but the value is never returned.

It is extremely unclear what result you are hoping for, but hopefully this will start you on the right track.

Erik White
  • 336
  • 2
  • 8
  • could you shed more light on "...,product(names,repeat=2))?? thank you for the helpful answer, but I didnt understand how to pass my 2 arguments to the function – Mohammed Baashar May 24 '20 at 09:02
  • 1
    I've updated my answer showing how to use it. The `product` call was just for the linked question' – Erik White May 24 '20 at 11:04
  • ? product is a function in itertools ! your answer is vague and doesn't work and the function is wrong even ! this answer from 2011 gives better explanation which you seem to have copy-pasted from it ! – Mohammed Baashar May 24 '20 at 19:23
  • https://stackoverflow.com/questions/5442910/python-multiprocessing-pool-map-for-multiple-arguments – Mohammed Baashar May 24 '20 at 19:23
  • 1
    I made a typo in the function call which I have fixed, I also added links so you can see the code running. I have no idea what you are trying to achieve with your `mult` function, but I have shown how you can use `pool.starmap` and `pool.starmap_async` with multiple variables. – Erik White May 24 '20 at 21:16
0

starmap_async worked but my app couldn't run with it due to it accessing the camera; I oversimplified my function in this question for the sake of simplicity. I resorted to "Popen()" instead and managed to run the app. Thank you very much Erik White; starmap is a great function that I might use in the future.

Mohammed Baashar
  • 484
  • 3
  • 6
  • 20