canceling and joining threads

Ove Svensson svensson_ove at hotmail.com
Mon Dec 19 13:51:48 CET 2005


"sir_alex" <chimaera29 at libero.it> writes:

> Hello everybody! I have a couple of questions about threads: the first
> is, is there the possibility to cancel a thread while it is executing
> (like the C function thread_cancel), for implementing something like an
> "abort" button? 

As far as I know, python thread does not support thread cancellation,
and that is probably a good thing. Thread cancellation can be _very_
difficult to get right. If not done right, you end up with resource
leaks and/or deadlock. 

It is much better to use some explicit synchronization mechanism to 
tell a thread that it shall terminate itself. You could, for example,
use a state variable, a mutex and a condition variable.

>                 And the second is, i have a GUI in which there's a
> button that launches a thread implemented using the threading module
> (creating a class which inherits from threading.Thread and overriding
> __init__ and run), i chose to use threads to avoid the problem of
> freezing my windows, but after i call classobject.start() this happens:
> if i then call classobject.join(), then the button is freezed, because
> now the button waits the end of the thread, 

And that is only to be expected. Join is not a mechanism for terminating
threads, it us a mechanism for _blocking__ until the designated thread 
terminates.

>                                             if i don't make this call
> the button releases itself (that is a good behavior) but the thread
> freezes until i give a ctrl-c in the console from which i launched my
> app. So, how can i make my button release while the thread is
> executing? (my GUI is implemented in GTK and i use libglade) (sorry for
> the length of this post...)

When the button is being pressed, use some explicit synchronization
to inform the thread that it shall terminate.




More information about the Python-list mailing list