Call a function when a thread exits
pavlovevidence at gmail.com
Fri May 8 03:33:38 CEST 2009
On May 7, 6:12 pm, "Giampaolo Rodola'" <gne... at gmail.com> wrote:
> I'm searching for a smooth way to call a certain function when a
> thread has finished its job.
> I guess I can keep calling isAlive() in a loop and call my function
> when it returns False but it's not very elegant.
> Actually I'm a bit surprised it doesn't exists an "atexit" function.
> Something like:
> import threading, time
> def myfun():
> print "hello"
> def cleanup():
> print "thread finished, starting cleanup operations..."
> t = threading.Thread(target=myfun)
> Is there a reason why there's no such thing in the threading module?
You can define your target function to clean up on exit. Using your
definitions of myfun and cleanup,
t = threading.Thread(target=mycleanfun)
More information about the Python-list