Alex Rivera | Logout

How can I provide a "callback" to an API?

Asked 2009-08-23T17:30:50.150
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?

Edit
Report

2 Answers

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()

answered 2009-08-23T17:41:35.077
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.

answered 2009-08-23T17:44:09.687

Your Answer