Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass and run a callback method in Python

I have a Manager (main thread), that creates other Threads to handle various operations. I would like my Manager to be notified when a Thread it created ends (when run() method execution is finished).

I know I could do it by checking the status of all my threads with the Thread.isActive() method, but polling sucks, so I wanted to have notifications.

I was thinking of giving a callback method to the Threads, and call this function at the end of the run() method:

class Manager():     ...     MyThread(self.on_thread_finished).start() # How do I pass the callback      def on_thread_finished(self, data):         pass     ...  class MyThread(Thread):     ...     def run(self):         ....         self.callback(data) # How do I call the callback?     ... 

Thanks!

like image 844
nbarraille Avatar asked Jul 23 '11 14:07

nbarraille


People also ask

How do I call a callback function in Python?

In the Parallel Python Module, the submit function is known as the callback function. The callback function acts as an argument for any other function. The other function in which the callback function is an argument calls the callback function in its function definition.

How do you pass a callback function?

Passing a function to another function or passing a function inside another function is known as a Callback Function. Syntax: function geekOne(z) { alert(z); } function geekTwo(a, callback) { callback(a); } prevfn(2, newfn);

How do you get data from a callback function in Python?

The easiest way I can think of to do this in your code is to just store the input as a variable in the CallBackStuff object. Then after you've called the call-back function, you can just access the input data from the CallBackStuff instance.

When callback is executed?

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. The above example is a synchronous callback, as it is executed immediately.


1 Answers

The thread can't call the manager unless it has a reference to the manager. The easiest way for that to happen is for the manager to give it to the thread at instantiation.

class Manager(object):     def new_thread(self):         return MyThread(parent=self)     def on_thread_finished(self, thread, data):         print thread, data  class MyThread(Thread):      def __init__(self, parent=None):         self.parent = parent         super(MyThread, self).__init__()      def run(self):         # ...         self.parent and self.parent.on_thread_finished(self, 42)  mgr    = Manager() thread = mgr.new_thread() thread.start() 

If you want to be able to assign an arbitrary function or method as a callback, rather than storing a reference to the manager object, this becomes a bit problematic because of method wrappers and such. It's hard to design the callback so it gets a reference to both the manager and the thread, which is what you will want. I worked on that for a while and did not come up with anything I'd consider useful or elegant.

like image 89
kindall Avatar answered Sep 22 '22 03:09

kindall