Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #19644
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Subject | Re: Killing threads, and os.system() |
| Date | 2012-01-31 11:04 -0500 |
| References | <jg867e$6n2$1@news.univ-fcomte.fr> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5249.1328025863.27778.python-list@python.org> (permalink) |
On Tue, 31 Jan 2012 08:45:53 +0100, Laurent Claessens
<moky.math@gmail.com> wrote:
>
>My aim is to kill everything with ctrl-C (KeyboardInterrupt).
>
>Of course I tried to do
>
>try:
> [...]
>except KeyboardInterrupt :
> for task in task_list :
> task.stop()
> #task_list is the list of threads to be killed
>
When did .stop() become a method of threads? Or is it a method
created by you which is supposed to set some local data in the thread to
signal is should exit?
>
>It does not work.
>
For years, the recommended coding style requires the thread to wait
on an event/condition or poll a local/global flag value; the thread is
then responsible for cleanly exiting /itself/.
Of course, if that thread is stuck waiting for a call to os.system()
to complete, then it can not do anything...
os.system() is a rather limited, restrictive, call -- best used for
quick one-of operations. If running Python 2.6+, I'd recommend
converting from os.system() to subprocess.Popen(). .Popen() objects now
have .terminate() and .kill() methods.
Heck, if the only reason you have been using threads is to permit
multiple parallel os.system() calls, you can probably drop the threads
entirely, and use a collection of subprocess.Popen() objects directly.
You will/should code something to handle stdin/stdout/stderr of those
objects -- unfortunately lowest-common-denominator IPC system does not
make it easy to do interactive operations ({muse: who do we have to kill
to persuade OS designers to incorporate something like the Amiga ARexx
"rexxport" system <G>}).
>How can I produce an "emergency" stop of all processes, including the
>externals programs that were called by os.system() ?
As hinted above: Python 2.6 or newer, subprocess.Popen(), using
.terminate(), .kill(), or maybe .send_signal()
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Killing threads, and os.system() Laurent Claessens <moky.math@gmail.com> - 2012-01-31 08:45 +0100
Re: Killing threads, and os.system() Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-01-31 11:04 -0500
Re: Killing threads, and os.system() Laurent Claessens <moky.math@gmail.com> - 2012-01-31 18:18 +0100
Re: Killing threads, and os.system() Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-01-31 14:34 -0500
Re: Killing threads, and os.system() Laurent Claessens <moky.math@gmail.com> - 2012-01-31 18:18 +0100
Re: Killing threads, and os.system() John Nagle <nagle@animats.com> - 2012-02-03 00:14 -0800
Re: Killing threads, and os.system() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-03 09:25 +0000
Re: Killing threads, and os.system() Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-02-03 11:20 -0500
Re: Killing threads, and os.system() Paul Rubin <no.email@nospam.invalid> - 2012-02-03 15:42 -0800
csiph-web