Skip to content

Instantly share code, notes, and snippets.

@amirasaran
Created October 27, 2016 06:36
Show Gist options
  • Save amirasaran/e91c7253c03518b8f7b7955df0e954bb to your computer and use it in GitHub Desktop.
Save amirasaran/e91c7253c03518b8f7b7955df0e954bb to your computer and use it in GitHub Desktop.
Python threading with callback function (callback function run after thread is finished)
import time
import threading
class BaseThread(threading.Thread):
def __init__(self, callback=None, callback_args=None, *args, **kwargs):
target = kwargs.pop('target')
super(BaseThread, self).__init__(target=self.target_with_callback, *args, **kwargs)
self.callback = callback
self.method = target
self.callback_args = callback_args
def target_with_callback(self):
self.method()
if self.callback is not None:
self.callback(*self.callback_args)
def my_thread_job():
# do any things here
print "thread start successfully and sleep for 5 seconds"
time.sleep(5)
print "thread ended successfully!"
def cb(param1, param2):
# this is run after your thread end
print "callback function called"
print "{} {}".format(param1, param2)
# example using BaseThread with callback
thread = BaseThread(
name='test',
target=my_thread_job,
callback=cb,
callback_args=("hello", "world")
)
thread.start()
@Crimees
Copy link

Crimees commented Jan 23, 2018

Amirasaran, what a legend

@adityapatadia
Copy link

Does the callback run in child thread or the Manager thread?

@xianminx
Copy link

xianminx commented Apr 16, 2018

@adityapatadia In the current case, I guess the callback runs in child thread(callee thread), but not manager thread. This can lead to some issues in certain cases. By definition, callback function should be called in the same thread as the caller in some cases. Thus, it is better to move callback out of the callee thread. This requirement is mandatory when programming for GUI applications, which often requires UI update operations must be called on the UI main thread. For example, callee thread is dispatched to fetch network resources and when done, UI thread is passed the resources and updates the UI.

However, things become complex when you dive into the internals. If you want the callback be called in the manager thread, manager thread should have a looper to check the status and result of the callee thread periodically.

@kid-yume
Copy link

kid-yume commented May 30, 2019

@aamirasaran how would i give the my_thread_job an argument? when i pass 'args' I get an error for the callback args

@amirasaran
Copy link
Author

amirasaran commented Jul 29, 2019

@kid-yume I'm sorry for having responded late

you can use like this

example using BaseThread with callback and my_thread_job args & kwargs

my_thread_job_args = ('first_arg', 'secound_args')
my_thread_job_kwargs = {
    'first': 1,
    'secound':2,
}
thread = BaseThread(
    name='test',
    target=my_thread_job,
    callback=cb,
    callback_args=("hello", "world"),
    args=my_thread_job_args,
    kwargs=my_thread_job_kwargs
)

thread.start()

@repen
Copy link

repen commented Nov 22, 2019

Thanks. You Saved me from new bike

@pratham2003
Copy link

pratham2003 commented Dec 18, 2019

Is there a way to get return value from the thread?

e.g.

def my_thread_job():
    return 123; # Consider the return value is dynamic

def cb(val):
    print(val) # Should be 123

thread = BaseThread(
    name='test',
    target=my_thread_job,
    callback=cb
)

thread.start()

P.S.
After some thought... something like this might work 🤔

def target_with_callback(self):
        return_val = self.method()
        if self.callback is not None:
            if self.callback_args:
                self.callback(*self.callback_args)
            else:
                self.callback(return_val)

@pratham2003
Copy link

@amirasaran how do we access my_thread_job_args inside my_thread_job in your example?

Can you please post the body of my_thread_job?

@zzeitlin
Copy link

@pratham2003 If anybody else finds this, it would be something similar to how you pop'd the target :

class BaseThread(threading.Thread):
    def __init__(self, callback=None, callback_args=None, *args, **kwargs):
        target = kwargs.pop('target')
        firstarg = kwargs.pop('first')                # inserted
        secondarg = kwargs.pop('secound')   # inserted
        super(BaseThread, self).__init__(target=self.target_with_callback, *args, **kwargs)
        self.callback = callback
        self.method = target
        self.firstarg = firstarg                        # inserted
        self.seccondarg = secondarg           # inserted
        self.callback_args = callback_args


    def target_with_callback(self):         
        self.method(self.firstarg, self.secondarg)     # inserted parameters (note: "method" has been defined as "target" which is my_thread_job()
        if self.callback is not None:
            self.callback(*self.callback_args)

def my_thread_job(arg1, arg2):     # inserted parameters
    # do any things here
    print "thread start successfully and sleep for 5 seconds"
    time.sleep(5)
    print(arg1)
    print(arg2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment