Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12982

Re: killing a script

Date 2011-09-09 10:03 +1000
From Cameron Simpson <cs@zip.com.au>
Subject Re: killing a script
References <4e5c6376$0$29983$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.887.1315526624.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 30Aug2011 14:13, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
| On Tue, 30 Aug 2011 08:53 am Arnaud Delobelle wrote:
| >> Yes, but if I am not mistaken, that will require me to put a line or
| >> two after each os.system call. That's almost like whack-a-mole at the
| >> code level rather than the Control-C level. OK, not a huge deal for
| >> one script, but I was hoping for something simpler. I was hoping I
| >> could put one line at the top of the script and be done with it.
| > 
| > Write a function!  That's what they're for after all :)
| 
| I'm not sure that this is actually as simple as that, especially using
| os.system.
| 
| As I understand it, the scenario is this:
| 
| The main script looks something like this:
| 
| for x in whatever:
|     os.system('something.py x')
| 
| Each time through the loop, a new Python process is started. Each process
| runs in the foreground, capturing standard input, and so hitting Ctrl-C
| kills *that* process, not the main script. Unless, by chance, the Ctrl-C
| happens after the system call returns, but before the next one starts, it
| is completely invisible to the parent process (the main script). Wrapping
| os.system in a function does nothing to fix that.

Presuming you're talking about UNIX, this is not correct.

Ctrl-C at the terminal delivers SIGINT to _every_ process in the controlling
process group for the terminal. It also has _nothing_ to do with the standard
input.

When you run a script, yea even a Python script, thus:

  myscript ...

then job control capable shells (all of them, these days) put the python
process running "myscript" in its own process group as the leader
(being, initially, the only process in the group). If myscript forks
other processes, as happens in os.system(), they are _also_ in that
process group. _ALL_ of them receive the SIGINT from your Ctrl-C.

Cheers,
-- 
Cameron Simpson <cs@zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

DRM: the functionality of refusing to function. - Richard Stallman

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

killing a script "Russ P." <russ.paielli@gmail.com> - 2011-08-28 18:15 -0700
  Re: killing a script MRAB <python@mrabarnett.plus.com> - 2011-08-29 02:52 +0100
    Re: killing a script "Russ P." <russ.paielli@gmail.com> - 2011-08-28 19:41 -0700
      Re: killing a script Chris Angelico <rosuav@gmail.com> - 2011-08-29 12:51 +1000
        Re: killing a script "Russ P." <russ.paielli@gmail.com> - 2011-08-28 20:08 -0700
          Re: killing a script Chris Rebert <clp2@rebertia.com> - 2011-08-28 20:16 -0700
            Re: killing a script "Russ P." <russ.paielli@gmail.com> - 2011-08-28 20:22 -0700
            Re: killing a script "Russ P." <russ.paielli@gmail.com> - 2011-08-29 14:26 -0700
          Re: killing a script Thomas Jollans <t@jollybox.de> - 2011-08-29 09:49 +0200
          Re: killing a script Arnaud Delobelle <arnodel@gmail.com> - 2011-08-29 23:53 +0100
            Re: killing a script Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-30 14:13 +1000
              Re: killing a script Hans Mulder <hansmu@xs4all.nl> - 2011-08-30 12:40 +0200
              Re: killing a script Cameron Simpson <cs@zip.com.au> - 2011-09-09 10:03 +1000
                Re: killing a script Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-09 19:07 +1000
                Re: killing a script Hans Mulder <hansmu@xs4all.nl> - 2011-09-09 12:13 +0200
                Re: killing a script Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-09 22:16 +1000
                Re: killing a script Cameron Simpson <cs@zip.com.au> - 2011-09-10 08:29 +1000
                Re: killing a script Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-10 11:25 +1000
                Re: killing a script Chris Angelico <rosuav@gmail.com> - 2011-09-10 11:37 +1000
                Re: killing a script Cameron Simpson <cs@zip.com.au> - 2011-09-10 18:49 +1000
                Re: killing a script Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-11 15:08 +1000
                Re: killing a script Nobody <nobody@nowhere.com> - 2011-09-10 10:47 +0100
      Re: killing a script Paul Rubin <no.email@nospam.invalid> - 2011-08-28 19:53 -0700
  Re: killing a script Nobody <nobody@nowhere.com> - 2011-08-29 07:52 +0100

csiph-web