473,503 Members | 338 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

canceling and joining threads

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...)

Dec 19 '05 #1
4 1622
"sir_alex" <ch********@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.

Dec 19 '05 #2
Op 2005-12-19, sir_alex schreef <ch********@libero.it>:
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/ant...hon/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

--
Antoon Pardon
Dec 19 '05 #3
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...)

Dec 19 '05 #4
Op 2005-12-19, sir_alex schreef <ch********@libero.it>:
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.

--
Antoon Pardon
Dec 20 '05 #5

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

Similar topics

7
1587
by: Tony | last post by:
Hi there, Is there any event you can catch that would you allow to cancel the deletion of a file or directory. The FileSystemEventHandler allows you to catch file operations after they have...
0
1887
by: Allan Bredahl | last post by:
Hi All I am trying to construct an application that is able to cancel a machine shutdown, reboot or logoff. And after performing some stuff to perform the original shutdown order :...
4
4048
by: welie | last post by:
I have a problem canceling a check box update when placing a check in it. Checkbox is not bound. Here is what happens. User clicks a check box. In the BeforeUpdate method of the control, if...
9
1862
by: Eric Sabine | last post by:
Can someone give me a practical example of why I would join threads? I am assuming that you would typically join a background thread with the UI thread and not a background to a background, but...
11
6629
by: Tore Halset | last post by:
Hello. I am trying to port an old java application from MS SQL Server to PostgreSQL running on Mac OS X. I have access to the java source code and can make modifications. I have tried with...
0
1227
by: Bart McFarling | last post by:
postgres 7.4.2 on a RedHat Enterprise Server using libpq on SCO Open Server Seems that if a command takes too long I get ERROR:Canceling query due to user request. I have ulimit=unlimited...
0
1212
by: Lloyd Zusman | last post by:
I have a python-2.5 program running under linux in which I spawn a number of threads. The main thread does nothing while these subsidiary threads are running, and after they all complete, the main...
0
1622
by: rcarmich | last post by:
I am having an issue canceling a beginReceive call on a timeout. For example, the following function: public int ReadBytes(Socket theSock, byte arr, int startByte, int length, int timeout) {...
0
1176
by: Qui Sum | last post by:
I write ip/port scanner so users using search on our LAN know if the particular ftp is online or not. The class I write has the following structure: public class IPScanner { public...
0
7203
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
7087
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7281
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
7334
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...
1
6993
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
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.