In python (2.6.6) what is the best way I can have a thread/process checking a network (message queue) for things while concurrently doing work (compiling). If i receive a command down the message queue, i must be able to kill and spawn compile threads.
-
Which OS? For killing stuff it matters – Prof. Falken Oct 23 '12 at 08:38
-
gnu/linux (debian). The actual killing, building, and messaging, i got. Just the threading/multiproces im having issues with. – James Bennet Oct 23 '12 at 08:51
2 Answers
There's no official Python API for killing threads -- you need to use an OS-specific method.
If you spawn a new process (using multiprocessing.Process
) then you can kill it with .terminate()
. Of course, this will cause it to stop immediately -- it won't clean up after itself and it may pollute any shared data structures.

- 120,462
- 19
- 136
- 170
-
-
I find the easiest way is to use an [`Executor`](http://docs.python.org/dev/library/concurrent.futures.html) -- they're in the stdlib in 3.2+ but have been [backported](http://pypi.python.org/pypi/futures) to 2.6+. You register a callback on a future with [`Future.add_done_callback`](http://docs.python.org/py3k/library/concurrent.futures.html#concurrent.futures.Future.add_done_callback). – Katriel Oct 23 '12 at 09:35
As noted, there is no Python API for killing threads. You must either use multiprocessing or a cooperative design pattern.
If you want to do the latter, you can simply use a cooperative design pattern, where the worker threads periodically check if they should be killed. E.g.:
while not self.stopped:
do_come_compilation_stuff()
Then in your main thread, you should set the stopped flag for the worker thread. This is also a useful design patter if you want your program to correctly handle signals such as CTRL-C.

- 10,935
- 3
- 32
- 51