Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I use key word arguments with python multiprocessing pool apply_async

Tags:

I'm trying to get to grips with pythons multiprocessing module, specifically the apply_async method of Pool. I'm trying to call a function with arguments and keyword arguments. If I call the function without kwargs it's fine but when I try to add in a keyword argument I get: TypeError: apply_async() got an unexpected keyword argument 'arg2' Below is the test code that I'm running

#!/usr/bin/env python
import multiprocessing
from time import sleep
def test(arg1, arg2=1, arg3=2):
    sleep(5)

if __name__ == '__main__':
    pool = multiprocessing.Pool()
    for t in range(1000):
        pool.apply_async(test, t, arg2=5)
    pool.close()
    pool.join()

How can I call the function so that it accepts keyword arguments?

like image 612
cts Avatar asked Feb 11 '13 10:02

cts


People also ask

How do you pass arguments in multiprocessing in Python?

Passing Keyword Arguments to Multiprocessing Processes We can also pass in arguments corresponding to the parameter name using the kwargs parameter in the Process class. Instead of passing a tuple, we pass a dictionary to kwargs where we specify the argument name and the variable being passed in as that argument.

How does pool Apply_async work?

apply_async() The apply_async() function can be called directly to execute a target function in the process pool. The call will not block, but will instead immediately return an AsyncResult object that we can ignore if our function does not return a value.


2 Answers

Pass the keyword args in a dictionary (and the positional arguments in a tuple):

pool.apply_async(test, (t,), dict(arg2=5))
like image 129
Janne Karila Avatar answered Sep 19 '22 18:09

Janne Karila


original answer: python multiprocessing with boolean and multiple arguments

apply_async has args and kwds keyword arguments which you could use like this:

res = p.apply_async(testFunc, args=(2, 4), kwds={'calcY': False})
like image 38
Chang Avatar answered Sep 20 '22 18:09

Chang