Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:edu.au': 0.07; 'script,': 0.07; 'python': 0.08; 'correct.': 0.09; 'unix,': 0.09; 'capturing': 0.16; 'control-c': 0.16; 'ctrl-c': 0.16; 'ctrl-c.': 0.16; 'forks': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'initially,': 0.16; 'input,': 0.16; 'input.': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'presuming': 0.16; 'received:202.125.174': 0.16; 'received:202.125.174.133': 0.16; 'received:boardofstudies.nsw.edu.au': 0.16; 'received:cskk.homeip.net': 0.16; 'received:harvey.boardofstudies.nsw.edu.au': 0.16; 'received:homeip.net': 0.16; 'received:nsw.edu.au': 0.16; 'shells': 0.16; 'simpler.': 0.16; 'stallman': 0.16; 'cc:addr :python-list': 0.16; 'this:': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; 'cc:no real name:2**0': 0.20; 'process,': 0.21; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'tue,': 0.23; 'runs': 0.23; 'loop,': 0.23; 'aug': 0.24; 'code': 0.25; "i'm": 0.27; 'function': 0.27; '(the': 0.28; 'script.': 0.29; 'looks': 0.29; 'fix': 0.29; 'script': 0.29; 'cc:addr:python.org': 0.30; 'parent': 0.30; 'returns,': 0.30; 'does': 0.32; 'it.': 0.33; 'actually': 0.33; 'that,': 0.33; 'done': 0.34; '...': 0.34; 'header:User-Agent:1': 0.34; 'richard': 0.34; 'controlling': 0.34; 'function.': 0.34; 'rather': 0.35; 'running': 0.35; 'leader': 0.35; 'charset:us- ascii': 0.36; 'received:au': 0.36; 'using': 0.37; 'put': 0.37; 'run': 0.37; 'but': 0.37; 'something': 0.37; 'especially': 0.37; 'two': 0.37; 'could': 0.38; 'steven': 0.38; 'skip:o 20': 0.38; 'subject:: ': 0.39; 'ok,': 0.39; 'happens': 0.40; 'huge': 0.61; 'your': 0.61; 'received:202': 0.66; 'cameron': 0.67; 'processes,': 0.67; 'receive': 0.67; 'kills': 0.84; 'scenario': 0.93; 'delivers': 0.96 Date: Fri, 9 Sep 2011 10:03:34 +1000 From: Cameron Simpson To: Steven D'Aprano Subject: Re: killing a script MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4e5c6376$0$29983$c3e8da3$5496439d@news.astraweb.com> User-Agent: Mutt/1.5.21 (2010-09-15) References: <4e5c6376$0$29983$c3e8da3$5496439d@news.astraweb.com> Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1315526624 news.xs4all.nl 2471 [2001:888:2000:d::a6]:59490 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12982 On 30Aug2011 14:13, Steven D'Aprano 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 DoD#743 http://www.cskk.ezoshosting.com/cs/ DRM: the functionality of refusing to function. - Richard Stallman