473,232 Members | 1,528 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,232 software developers and data experts.

try finally doesn't

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

def x():
try:
time.sleep(60)
finally:
print 'thread'

thread.start_new_thread(x,())
time.sleep(60)


Jul 18 '05 #1
6 1565
Colin Brown:
When I run this code and then immediately do a Control-C, I do not get the
'thread' printed?


Output buffering. It does when you import sys and add sys.stdout.flush()
after the print statement.

--
René Pijlman
Jul 18 '05 #2
"Rene Pijlman" <re********************@my.address.is.invalid> wrote in
message news:21********************************@4ax.com...
Colin Brown:
When I run this code and then immediately do a Control-C, I do not get the'thread' printed?


Output buffering. It does when you import sys and add sys.stdout.flush()
after the print statement.

--
René Pijlman


Also when I run this on Win2K, I get:
C:\test>cmd
Microsoft Windows 2000 [Version 5.00.2195]
(C) Copyright 1985-2000 Microsoft Corp.

C:\test>python -u t3b.py

C:\test>

-----------------------------------------
import thread, time, sys

def x():
try:
time.sleep(60)
finally:
print 'thread'
sys.stdout.flush()

thread.start_new_thread(x,())
time.sleep(5)

Jul 18 '05 #3
Colin Brown wrote:
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

def x():
try:
time.sleep(60)
finally:
print 'thread'

thread.start_new_thread(x,())
time.sleep(60)


From the signal docs:

Some care must be taken if both signals and threads are used in the same
program. The fundamental thing to remember in using signals and threads
simultaneously is: always perform signal() operations in the main thread of
execution. Any thread can perform an alarm(), getsignal(), or pause(); only
the main thread can set a new signal handler, and the main thread will be
the only one to receive signals (this is enforced by the Python signal
module, even if the underlying thread implementation supports sending
signals to individual threads). This means that signals can't be used as a
means of inter-thread communication. Use locks instead.

So your thread never gets interrupted.

Regards,

Diez
Jul 18 '05 #4
"Colin Brown" <cb****@metservice.com> writes:
"Rene Pijlman" <re********************@my.address.is.invalid> wrote in
message news:21********************************@4ax.com...
Colin Brown:
When I run this code and then immediately do a Control-C, I do not get the'thread' printed?


Output buffering. It does when you import sys and add sys.stdout.flush()
after the print statement.

--
René Pijlman


Also when I run this on Win2K, I get:


Oh, if you're on Windows, things are likely to be different.

What version of Python are you using?

Cheers,
mwh

--
I have gathered a posie of other men's flowers, and nothing but
the thread that binds them is my own. -- Montaigne
Jul 18 '05 #5

"Michael Hudson" <mw*@python.net> wrote in message
news:m3************@pc150.maths.bris.ac.uk...
....
Oh, if you're on Windows, things are likely to be different. ....

Sorry! Can't blame Bill for this one. It does the same thing for me under
RedHat Linux also ;-)
What version of Python are you using?


Python 2.3.2

Colin

---------------------------------------------------------------
[cbrown@matahari cbrown]$ python2.3
Python 2.3.2 (#1, Oct 6 2003, 10:07:16)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

[cbrown@matahari cbrown]$ cat tryfin.py
import thread, time, sys

def x():
try:
time.sleep(60)
finally:
print 'thread'
sys.stdout.flush()
sys.stderr.flush()

thread.start_new_thread(x,())
time.sleep(5)
[cbrown@matahari cbrown]$ python2.3 -u tryfin.py
[cbrown@matahari cbrown]$

Jul 18 '05 #6
The interaction of signals, threads and exiting is pretty minimal in
Python. I think that in this case a KeyboardInterrupt is delivered to
the main thread, and when it's uncaught it exits the program, closing
all threads.

Python doesn't even have the capability of delivering an exception to an
arbitrary thread, and that would go double for native code with no GIL.

You'll have to write your code more like this:

exiting = 0
active_threads = 0

def x():
global active_threads
active_threads += 1
for i in range(60):
if exiting:
active_threads -= 1
return
sleep(1)

t = thread.start_new_thread(x, ())

try:
time.sleep(60)
finally:
exiting = 1
while active_threads:
sleep(1)

.... except that you'd want to make active_threads into something that
doesn't suffer from race conditions.

Jeff
PS code untested, and you should consider using the threading module
anyway, if you've decided you must use threads to get the job done.

Jul 18 '05 #7

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

Similar topics

3
by: Raymond Lesher | last post by:
Oops! Forgot a line of code!!!!! Sorry. This code example should display my problem properly. Why does the finally clause in the Test method (below) NOT execute in my "foo" example, but DOES...
13
by: KefX | last post by:
This may have been discussed before, but I'm kind of confused as to why Python doesn't support having both an except ~and~ a finally clause, like this: try: raise RuntimeException except:...
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...
9
by: Andy Turner | last post by:
I was just pondering about what the Finally in an exception actually does. Well, I realise that it always gets called regardless of whether the exception is thrown or not, but isn't that the same...
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....
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.