try finally doesn't

Diez B. Roggisch deets_noospaam at web.de
Wed Nov 26 17:16:21 EST 2003


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




More information about the Python-list mailing list