Could some explain how call back methods work, and if possible, give me an example in Python? So as far as I understand them, they are methods which are provided by the user of an API, to the API, so that the user doesn’t have to wait till that particular API function completes. So does the user program continue executing, and once the callback method is called by the API, return to point in the program where the callback method was provided? How does a callback method essentially affect the ‘flow’ of a program?
Sorry if I’m being vague here.
Callbacks are just user-supplied hooks. They allow you to specify what function to call in case of certain events.
re.subhas a callback, but it sounds like you are dealing with a GUI, so I’ll give a GUI example:Here is a very simple example of a callback:
When you press the
OKbutton, the program prints “Running my_callback”.If you play around with this code:
you’ll see that pressing either button blocks the GUI from responding until the callback finishes. So the “user does have to wait till that particular API function completes”.