Call a function when a thread exits

Discussion in 'Python' started by Giampaolo Rodola', May 8, 2009.

  1. 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/
     
    Giampaolo Rodola', May 8, 2009
    #1
    1. Advertising

  2. Giampaolo Rodola'

    Carl Banks Guest

    On May 7, 6:12 pm, "Giampaolo Rodola'" <> 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
     
    Carl Banks, May 8, 2009
    #2
    1. Advertising

  3. On 8 Mag, 03:33, Carl Banks <> wrote:
    > On May 7, 6:12 pm, "Giampaolo Rodola'" <> 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
     
    Giampaolo Rodola', May 8, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Helber

    system call exits the loop

    Bob Helber, Jan 13, 2004, in forum: Perl
    Replies:
    1
    Views:
    735
    Bob Helber
    Jan 13, 2004
  2. Ian Wilson
    Replies:
    4
    Views:
    647
    Ian Wilson
    Jan 24, 2007
  3. Felix Kater
    Replies:
    7
    Views:
    258
    Albert van der Horst
    Dec 11, 2004
  4. Replies:
    5
    Views:
    1,620
    Roedy Green
    Jun 20, 2008
  5. Jeff
    Replies:
    2
    Views:
    102
Loading...

Share This Page