473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

try/finally in threads

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

Jul 3 '07 #1
2 1739
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
Jul 3 '07 #2
On Jul 3, 5:05 am, "Diez B. Roggisch" <d...@nospam.web.dewrote:
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

Jul 3 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Colin Brown | last post by:
When I run this code and then immediately do a Control-C, I do not get the 'thread' printed? Colin Brown PyNZ ----------------------------------------------------------- import thread, time ...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
16
by: Bill | last post by:
Say I have a childThread currently is running a finally block to cleanup external resources. At the same time the main thread calls childThread.Abort(). The question is: when the...
5
by: C# beginner | last post by:
Hi all If inside a try-catch-finally block, return statements are used, will the finally block get executed or not? Your replies are very important to me. Thanks a lot.
13
by: Jon Davis | last post by:
I understand that the finally sub-block will execute regardless of whether try succeeded or not. But so will code that follows try...catch. So then what is the difference between ... try {...
12
by: Carl J. Van Arsdall | last post by:
Hey python people, I'm interested in using the try/finally clause to ensure graceful cleanup regardless of how a block of code exits. However, I still am interested in capturing the exception....
3
by: Eran.Yasso | last post by:
Hi, I wondering where should i use finally and where should i use catch? I have a thread, which the thread dispatcher waits for the thread. if i abort the thread, an exception happens. why...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.