Interrupt python thread

B

BlueBird

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
 
F

Fredrik Lundh

James said:
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>
 
J

janislaw

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
 
B

BlueBird

        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
 
G

Gabriel Genellina

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

Dieter Maurer

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
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top