73

I was reading some module documentation and saw something I didn't understand, in the explanation of parameters for a method:

callback - callback function which will be called with argument list equal to callbackargs+(result,) as soon as calculation is done
callbackargs - additional arguments for callback function group - job group, is used when wait(group) is called to wait for jobs in a given group to finish

How can I call the method and correctly supply arguments for these parameters? What are they used for, and how does this "callback" scheme work?

0

5 Answers 5

255

A callback is a function provided by the consumer of an API that the API can then turn around and invoke (calling you back). If I setup a Dr.'s appointment, I can give them my phone number, so they can call me the day before to confirm the appointment. A callback is like that, except instead of just being a phone number, it can be arbitrary instructions like "send me an email at this address, and also call my secretary and have her put it in my calendar.

Callbacks are often used in situations where an action is asynchronous. If you need to call a function, and immediately continue working, you can't sit there wait for its return value to let you know what happened, so you provide a callback. When the function is done completely its asynchronous work it will then invoke your callback with some predetermined arguments (usually some you supply, and some about the status and result of the asynchronous action you requested).

If the Dr. is out of the office, or they are still working on the schedule, rather than having me wait on hold until he gets back, which could be several hours, we hang up, and once the appointment has been scheduled, they call me.

In this specific case, the documented method will compute the result, put it together with any callbackargs specified, and call callback, passing it those values as the arguments.

3
  • 31
    This gets a big upvote from me (still only worth the same as any other upvote though) for a very understandable analogy that will probably be helpful for beginners or anyone new to this technique.
    – John Y
    Commented Aug 23, 2009 at 18:13
  • 8
    This is a very good explanation for beginners, it just lacks one thing - a sample code. Now that a beginner knows what it is, could you please give us a simple example of what it would look like using your analog as function names ea: callDoctor() callPatientBack() etc
    – Alfa Bravo
    Commented Sep 30, 2017 at 7:58
  • For more theoretical, language-agnostic explanations, please see What is a callback function?. I tried to fix the question to make the practical aspect - how to use the API in question - as clear as possible. Commented Jan 6, 2023 at 2:35
21

If you want some code to be executed as soon as the result is ready, put that code into a function and pass it as the callback argument. For example, supposing no other arguments are needed for that function:

def itsdone(result):
    print(f"Done! result={result}")
...
submit(..., callback=itsdone)

For more on how this works in Python, see e.g. my presentation on the topic.

4
  • 1
    Don't you mean "at the time" instead of "as long"?
    – Dykam
    Commented Aug 23, 2009 at 17:49
  • I meant "as soon as", just like the docs I quoted -- let me edit to fix, thanks! Commented Aug 23, 2009 at 18:00
  • 3
    Can you please repost the presentation link? Commented Sep 25, 2020 at 21:36
  • 1
    @AlexMartelli, presentation link is not working.
    – garej
    Commented Jan 23, 2021 at 6:57
3

Looking at the link, just looks like a hook which is called.

callback - callback function which will be called with argument list equal to callbackargs+(result,) as soon as calculation is done

The "as soon as calculation is done" bit seems ambiguous. The point, as far as I can see of this thing is that the submit() call distributes work to other servers and then returns. Because the finishing is asynchronous, rather block, it allows you to provide a function which is called when some unit of work finishes. If you do:

submit( ..., callback=work_finished, ... )

Then submit will ensure work_finished() is called when the unit of distributed work is completed on the target server.

When you call submit() you can provide a callback which is called in the same runtime as the caller of submit() ... and it is called after the distribution of the workload function is complete.

Kind of like "call foo(x,y) when you have done some stuff in submit()"

But yea, the documentation could be better. Have a ganders at the ppython source and see at which point the callback is called in submit()

2

A callback is a function you define that's later called by a function you call.

As an example, consider how AJAX works: you write code that calls a back-end server function. At some point in the future, it returns from that function (the "A" stands for Asynchronous, which is what the "Parallel" in "Parallel Python" is all about). Now - because your code calls the code on the server, you want it to tell you when it's done, and you want to do something with its results. It does so by calling your callback function.

When the called function completes, the standard way for it to tell you it's done is for you to tell it to call a function in your code. That's the callback function, and its job is to handle the results/output from the lower-level function you've called.

2

A callback is simply a function. In Python, functions are just more objects, and so the name of a function can be used as a variable, like so:

def func():
    ...

something(func)

Note that many functions which accept a callback as an argument usually require that the callback accept certain arguments. In this case, the callback function will need to accept a list of arguments specified in callbackargs.

Not the answer you're looking for? Browse other questions tagged or ask your own question.