All those strange parentheses in python docs
Can someone please walk me through what happens when the python docs say:
apply_async (func [, args [, kwds [, callback]]])
I'm not sure what the functions func, args, kwds and callback mean here. I understand that you are basically passing apply_async a function and then arguments to use with that function (apparently this is what "func" and "args" mean). I am less clear about what "kwds" or "callback" is, or why the square brackets are nested the way they are. Can someone please explain?
source to share
The parentheses indicate that the field is optional. You MUST pass the name of the function (func) to apply_async, and you CAN pass its arguments, keywords, etc.
I would guess it is in the python style guide, although I didn't look to check: https://www.python.org/dev/peps/pep-0008/
(the guide is worth reading even if this answer is not present!)
EDIT:
To expand on this - if you have a function that takes no arguments (let's say it accidentally seeds itself and then does some computation), you can call it with:
from multiprocessing import Pool
pool = Pool(4) # use four processes
pool.apply_async(function_name)
if your function requires arguments, then this works:
pool.apply_async(function_name, argument_name)
or
pool.apply_async(function_name, args =(arg1_name, arg2_name))
if you use keywords:
pool.apply_async(function_name, args=(arg1, arg2, etc), kwds={key_name: value_name})
until now I had no reason to use keywords, arguments always did what I needed. There may be some special deep magic in kwds that I forgot because it doesn't work for me.
source to share