canceling and joining threads

S

sir_alex

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? 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, 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...)
 
O

Ove Svensson

sir_alex said:
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.
 
A

Antoon Pardon

Op 2005-12-19 said:
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),

You can have one thread raise an exception in an other thread.
Read my answer in the "thread and alarm" thread.
for implementing something like an
"abort" button? 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,

Well you ask the main thread to wait for the other thread to finish.
So that is normal behaviour.
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...)

Some questions.

1) Did you call gtk.gdk.threads_init() ?

2) Did you call gtk or gtk.gdk calls from an other thread?

3) Are you on windows of linux?


Some introductiory text about using threads with gtk is on
http://www.pardon-sleeuwaegen.be/antoon/python/page0.html

However a lot is linux specific.


There is also a mainling list for pygtk users. You can subscribe
here:

http://www.daa.com.au/mailman/listinfo/pygtk
 
S

sir_alex

1) no, i didn't; i'll go and read that function
2) well, the actual situation is: there's the main thread (the
application itself) and then the second thread that i create with
threading.Thread, in which i make some things; in this thread i call
gtk.gdk to update a window with a progress bar...
3) i'm on linux (ubuntu, specifically, but i don't think the distro is
important...)
 
A

Antoon Pardon

Op 2005-12-19 said:
1) no, i didn't; i'll go and read that function

gtk.gdk.threads_init, has to be called before gtk.main
if you want threads in a pygtk program.
2) well, the actual situation is: there's the main thread (the
application itself) and then the second thread that i create with
threading.Thread, in which i make some things; in this thread i call
gtk.gdk to update a window with a progress bar...

You will have to put your gtk.gdk calls between gtk.gdk.threads_enter
and gtk.gdk.threads_leave calls to avoud blocking.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top