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?
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']
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']
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.