Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Call a function when a thread exits

Reply
Thread Tools

Call a function when a thread exits

 
 
Giampaolo Rodola'
Guest
Posts: n/a
 
      05-08-2009
Hi,
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():
time.sleep(1)
print "hello"

def cleanup():
print "thread finished, starting cleanup operations..."

t = threading.Thread(target=myfun)
t.atexit(target=cleanup)
t.start()


Is there a reason why there's no such thing in the threading module?


--- Giampaolo
http://code.google.com/p/pyftpdlib/
 
Reply With Quote
 
 
 
 
Carl Banks
Guest
Posts: n/a
 
      05-08-2009
On May 7, 6:12*pm, "Giampaolo Rodola'" <gne...@gmail.com> wrote:
> Hi,
> 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():
> * * time.sleep(1)
> * * print "hello"
>
> def cleanup():
> * * print "thread finished, starting cleanup operations..."
>
> t = threading.Thread(target=myfun)
> t.atexit(target=cleanup)
> t.start()
>
> 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,

def mycleanfun():
try:
myfun()
finally:
cleanup()

t = threading.Thread(target=mycleanfun)
t.start()


Carl Banks
 
Reply With Quote
 
 
 
 
Giampaolo Rodola'
Guest
Posts: n/a
 
      05-08-2009
On 8 Mag, 03:33, Carl Banks <pavlovevide...@gmail.com> wrote:
> On May 7, 6:12*pm, "Giampaolo Rodola'" <gne...@gmail.com> wrote:
>
>
>
>
>
> > Hi,
> > 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():
> > * * time.sleep(1)
> > * * print "hello"

>
> > def cleanup():
> > * * print "thread finished, starting cleanup operations..."

>
> > t = threading.Thread(target=myfun)
> > t.atexit(target=cleanup)
> > t.start()

>
> > 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,
>
> def mycleanfun():
> * * try:
> * * * * myfun()
> * * finally:
> * * * * cleanup()
>
> t = threading.Thread(target=mycleanfun)
> t.start()
>
> Carl Banks


Yes, I know. I was wondering if it could be a good idea proposing
something like an atexit() function to be included in threading
module.


--- Giampaolo
http://code.google.com/p/pyftpdlib
http://code.google.com/p/psutil
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why we should call Thread.start(),not directly call Thread.run()? junzhang1983@gmail.com Java 5 06-20-2008 03:12 PM
Terminating a thread when program exits. Ian Wilson Java 4 01-24-2007 10:53 AM
Should an object declared in a function persist after function exits? Jeff Javascript 2 10-18-2006 05:58 AM
various exits from function with one general clean-up? Felix Kater C Programming 7 12-11-2004 05:04 PM
system call exits the loop Bob Helber Perl 1 01-13-2004 03:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57