3

Can someone please walk me through what is going on when the python docs say:

apply_async(func[, args[, kwds[, callback]]])

( at https://docs.python.org/2/library/multiprocessing.html#multiprocessing.pool.multiprocessing.Pool.apply_async).

I'm not sure what func, args, kwds, and callback are supposed to mean here. I understand that you basically pass apply_async a function, and then arguments to use with that function (presumably this is what "func" and "args" mean). I'm less clear on what "kwds" or "callback" is, or why the square brackets are nested inside each other the way they are. Can someone explain?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
user3436624
  • 715
  • 2
  • 8
  • 18

1 Answers1

2

The brackets indicate that the field is optional. You MUST pass apply_async a function name (func) and you CAN pass it arguments, keywords, etc.

I would imagine that it is in the python style guide, though I haven't searched to verify: https://www.python.org/dev/peps/pep-0008/

(the guide's worth a read even if this answer isn't in there!)

EDIT:

To expand upon this - if you have a function that does not take any arguments (say it randomly seeds itself and then performs some calculations) you could 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 are using keywords:

pool.apply_async(function_name, args=(arg1, arg2, etc), kwds={key_name: value_name})

so far, I have not had reason to use keywords, arguments have always done what I've needed. There could be special deep magic in kwds that I left out because it hasn't come up for me.

codeMonkey
  • 410
  • 1
  • 5
  • 13