Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallel Python: What is a callback?

In Parallel Python it has something in the submit function called a callback (documentation) however it doesn't seem to explain it too well. I've posted on their forum a couple days ago and I've not received a response. Would someone explain what a callback is and what it's used for?

like image 751
Nope Avatar asked Aug 23 '09 17:08

Nope


People also ask

What is a Python callback?

In Python, a callback is simply a function or a method passed to LocalSolver. A callback takes two parameters: the LocalSolver object that triggers the event and the type of the callback. It is possible to use the same callback method or object for multiple events or multiple LocalSolver instances.

What is meant by callback?

Definition of callback 1 : a return call. 2a : recall sense 5. b : a recall of an employee to work after a layoff. c : a second or additional audition for a theatrical part.

What is callback explain with an example?

"I will call back later!" A callback is a function passed as an argument to another function. This technique allows a function to call another function. A callback function can run after another function has finished.

What is callback statement?

A callback function is a function that is passed as an argument to another function, to be “called back” at a later time. A function that accepts other functions as arguments is called a higher-order function, which contains the logic for when the callback function gets executed.


2 Answers

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, Parallel Python's submit function will invoke your callback with any arguments you supply and the result of func, once func has finished executing.

like image 60
Logan Capaldo Avatar answered Sep 25 '22 06:09

Logan Capaldo


The relevant spot in the docs:

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 

So, if you want some code to be executed as soon as the result is ready, you put that code into a function and pass that function as the callback argument. If you don't need other arguments, it will be just, e.g.:

def itsdone(result):   print "Done! result=%r" % (result,) ... submit(..., callback=itsdone) 

For more on the callback pattern in Python, see e.g. my presentation here.

like image 42
Alex Martelli Avatar answered Sep 25 '22 06:09

Alex Martelli