try/finally in threads

Discussion in 'Python' started by George Sakkis, Jul 3, 2007.

  1. I posted this on the Pyro list but I'm not sure if it's related
    specifically to Pyro. The "finally" clause below is not executed when
    f() runs on on a (daemon) thread and the program exits. DAEMON here is
    a global Pyro.code.Daemon instance.

    def f():
    try: DAEMON.requestLoop()
    finally:
    # nothing is printed if f() runs in a thread
    print "i am here!!"
    DAEMON.shutdown()
    print "i am over!!"

    Is "finally" not guaranteed to be executed in a non-main thread or is
    there something else going on ?

    George
    George Sakkis, Jul 3, 2007
    #1
    1. Advertising

  2. George Sakkis wrote:

    > I posted this on the Pyro list but I'm not sure if it's related
    > specifically to Pyro. The "finally" clause below is not executed when
    > f() runs on on a (daemon) thread and the program exits. DAEMON here is
    > a global Pyro.code.Daemon instance.
    >
    > def f():
    > try: DAEMON.requestLoop()
    > finally:
    > # nothing is printed if f() runs in a thread
    > print "i am here!!"
    > DAEMON.shutdown()
    > print "i am over!!"
    >
    > Is "finally" not guaranteed to be executed in a non-main thread or is
    > there something else going on ?


    Well, that's pretty much the idea behind daemon threads - that they are
    terminated immediately. If it were otherwise, a little endless-loop in that
    finally-statement of yours would cause the program termination to hang
    endlessly.

    Diez
    Diez B. Roggisch, Jul 3, 2007
    #2
    1. Advertising

  3. On Jul 3, 5:05 am, "Diez B. Roggisch" <> wrote:

    > George Sakkis wrote:
    > > I posted this on the Pyro list but I'm not sure if it's related
    > > specifically to Pyro. The "finally" clause below is not executed when
    > > f() runs on on a (daemon) thread and the program exits. DAEMON here is
    > > a global Pyro.code.Daemon instance.

    >
    > > def f():
    > > try: DAEMON.requestLoop()
    > > finally:
    > > # nothing is printed if f() runs in a thread
    > > print "i am here!!"
    > > DAEMON.shutdown()
    > > print "i am over!!"

    >
    > > Is "finally" not guaranteed to be executed in a non-main thread or is
    > > there something else going on ?

    >
    > Well, that's pretty much the idea behind daemon threads - that they are
    > terminated immediately. If it were otherwise, a little endless-loop in that
    > finally-statement of yours would cause the program termination to hang
    > endlessly.


    Thanks, that makes sense; if the thread is non-daemon, it enters the
    finally block as expected.

    George
    George Sakkis, Jul 3, 2007
    #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. VB Programmer

    Question: Try,Catch,Finally

    VB Programmer, Aug 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    367
    Kevin Spencer
    Aug 7, 2003
  2. VB Programmer

    The problem with Try Catch Finally...

    VB Programmer, Aug 12, 2003, in forum: ASP .Net
    Replies:
    23
    Views:
    773
    Chad Myers
    Aug 15, 2003
  3. localhost
    Replies:
    1
    Views:
    380
    Steven Cheng[MSFT]
    Dec 25, 2003
  4. Wayne

    Try Catch Finally and Threads

    Wayne, Jun 6, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    3,348
    Winista
    Jun 7, 2006
  5. David Lozzi

    Try...Catch...Finally not firing finally?

    David Lozzi, Apr 23, 2007, in forum: ASP .Net
    Replies:
    12
    Views:
    794
    Alvin Bruney [MVP]
    May 11, 2007
Loading...

Share This Page