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

Interrupt python thread

Hi,

I have a program with a master thread and several slave threads.

Whenever an exception occurs, in the master thread or in one of the
slave threads, I would like to interrupt all the threads and the main
program. Threading API does not seem to provide a way to stop a
thread, is there anyway to achieve that ?

The closest thing I found is thread.interrupt_main() but it's far from
perfect :
- it only allow to interrupt the main thread
- if the main thread is sleeping, it does not interrupt it (at least
on windows)

cheers,

Philippe
Aug 24 '08 #1
6 5706
James Matthews wrote:
Group all the threads in a list and call the stop method on all of them.
what stop method?

</F>

Aug 24 '08 #2
James Matthews wrote:
def __stop(self):
self.__block.acquire()
self.__stopped = True
self.__block.notifyAll()
self.__block.release()
have you tried using that method? what happened? looking at the code,
what do you think it does?

</F>

Aug 25 '08 #3
On 24 Sie, 10:48, BlueBird <p...@freehackers.orgwrote:
>
Whenever an exception occurs, in the master thread or in one of the
slave threads, I would like to interrupt all the threads and the main
program. Threading API does not seem to provide a way to stop a
thread, is there anyway to achieve that ?
Note that killing, stopping, suspending and resuming Your thread at
arbitrary time leads to unpredictable results. For this reason i.e.
Java deprecated all such functions, and python didn't introduce them
at all. It makes sense as it leads to better-designed-code.

Regards
JW
Aug 25 '08 #4
On Aug 24, 8:35*pm, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
On Sun, 24 Aug 2008 01:48:46 -0700 (PDT), BlueBird
<p...@freehackers.orgdeclaimed the following in comp.lang.python:
Whenever an exception occurs, in the master thread or in one of the
slave threads, I would like to interrupt all the threads and the main
program. Threading API does not seem to provide a way to stop a
thread, is there anyway to achieve that ?

* * * * The only safe way to "abort" a thread is by having it exit on its
own. This means one needs a means of setting an attribute that each
thread periodically checks within a while loop.
Unfortunately, this does not map very well with my program. Each of my
threads are calling foreign code (still written in python though),
which might be busy for 1 to 10 minutes with its own job.

I wanted something to easily interrupt every thread to prevent my
program to stall for 10 minutes if I want to stop it (getting tired of
killing python all the time).

Philippe
Aug 25 '08 #5
En Mon, 25 Aug 2008 05:00:07 -0300, BlueBird <ph**@freehackers.org>
escribi�:
On Aug 24, 8:35Â*pm, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
>Â* Â* Â* Â* The only safe way to "abort" a thread is by having it exit on
its
own. This means one needs a means of setting an attribute that each
thread periodically checks within a while loop.

Unfortunately, this does not map very well with my program. Each of my
threads are calling foreign code (still written in python though),
which might be busy for 1 to 10 minutes with its own job.

I wanted something to easily interrupt every thread to prevent my
program to stall for 10 minutes if I want to stop it (getting tired of
killing python all the time).
If the foreign Python code is running on your own process, can't you make
it check a flag periodically?
If it runs as another process, killing it is a lot safer than killing a
thread.

--
Gabriel Genellina

Aug 25 '08 #6
En Mon, 25 Aug 2008 05:00:07 -0300, BlueBird <ph**@freehackers.org>
escribi¡Z:
Unfortunately, this does not map very well with my program. Each of my
threads are calling foreign code (still written in python though),
which might be busy for 1 to 10 minutes with its own job.

I wanted something to easily interrupt every thread to prevent my
program to stall for 10 minutes if I want to stop it (getting tired of
killing python all the time).
At the C level, Python has function to send an exception to a thread.
The threads will see the exception only when it executes Python code
(i.e. not when it is waiting or running in external (e.g. "C") code).

You may use (e.g.) "PyRex" to make a Python wrapper available
to your Python code.

Dieter
Aug 30 '08 #7

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

Similar topics

19
by: Jane Austine | last post by:
As far as I know python's threading module models after Java's. However, I can't find something equivalent to Java's interrupt and isInterrupted methods, along with InterruptedException....
26
by: djw | last post by:
Hi, Folks- I have a question regarding the "proper" use of try: finally:... Consider some code like this: d = Device.open() try: d.someMethodThatCanRaiseError(...) if SomeCondition: raise...
3
by: Russell Stuart via .NET 247 | last post by:
I always though .Net monitors were meant to be similar to thejava synchronisation mechanisms. The code below proves mewrong. When run under the debugger, it produces just "Thread 1"lines, as I...
2
by: bvermeersch | last post by:
Hi, Here I am again, just learning C# and always wanting to learn the hard things first. As you might already know from other posts I'm trying to convert c++ code to C#. The problem I'm...
0
by: John Bailo | last post by:
Does c# have a method for threads equivalent to the .interrupt() method? I want to implement a watchdog thread and have it throw an error onces the timeout value is reached. then I want it to...
2
by: =?Utf-8?B?QnJ1Y2UgSFM=?= | last post by:
I'm using VS2005 Winforms. I want a method to allow the user to be able to interrupt an SQL query if he decides he doesn't want to wait for it any longer. I've used IAsyncResult to open an...
0
by: antrillion | last post by:
Is there any way to interrupt a blocking i/o call (on a System.IO.Stream) in a thread from another thread? Such as Console.ReadLine() for an example? Thread.Interrupt doesn't seem to do the trick.
1
by: =?Utf-8?B?TXJOb2JvZHk=?= | last post by:
I have a problem where I am sending a series of commands using P/Invoke's SendInput function which may last up to a minute long. I want to offer the user a way to break the commands by pressing the...
7
by: Anil | last post by:
I have a Javascript program which runs in the browser and has functions work(), and stop(). It listens to commands from the server to work() and can be interrupted by the server to stop(). I am...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...
0
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
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...

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.