Python multiprocessor with boolean and multiple arguments

I have a function that takes several arguments, some of which are logical. I am trying to pass this to multiprocessing pool.apply_async and want to pass some arguments with attached names.

Here is an example script 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 interested in changing res = p.apply_async(testFunc, args = (2, 4, False)) to something like:

 res = p.apply_async(testFunc, args = (2, 4, calcY = False)) 
+6
source share
1 answer

apply_async has args and kwds keyword arguments, which you can use as follows:

 res = p.apply_async(testFunc, args=(2, 4), kwds={'calcY': False}) 
+8
source

Source: https://habr.com/ru/post/925694/


All Articles