I have a function that takes multiple arguments, some of which are boolean. I'm trying to pass this to the multiprocessing pool.apply_async
and want to pass some args with the names attached.
Here's an example script that I'm working with:
from multiprocessing import Pool
def testFunc(y, x, calcY=True):
if calcY == True:
return y*y
elif calcY == False:
return x*x
if __name__ == "__main__":
p = Pool()
res = p.apply_async(testFunc, args = (2, 4, False))
print res.get()
This works, but I'm curious about changing the res = p.apply_async(testFunc, args = (2, 4, False))
to something like:
res = p.apply_async(testFunc, args = (2, 4, calcY = False))
Use Pool. The multiprocessing pool starmap() function will call the target function with multiple arguments. As such it can be used instead of the map() function. This is probably the preferred approach for executing a target function in the multiprocessing pool that takes multiple arguments.
multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads.
Python multiprocessing Pool can be used for parallel execution of a function across multiple input values, distributing the input data across processes (data parallelism). Below is a simple Python multiprocessing Pool example.
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})
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