Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python': 0.08; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:()': 0.09; 'converting': 0.15; '>how': 0.16; '>it': 0.16; '>of': 0.16; '>try:': 0.16; 'amiga': 0.16; 'bieber': 0.16; 'cleanly': 0.16; 'ctrl-c': 0.16; 'email addr:ix.netcom.com': 0.16; 'email name:wlfraed': 0.16; 'from:addr:ix.netcom.com': 0.16; 'from:addr:wlfraed': 0.16; 'from:name:dennis lee bieber': 0.16; 'message-id:@4ax.com': 0.16; 'received:wlfraed': 0.16; 'subject:Killing': 0.16; 'subject:threads': 0.16; 'url:netcom': 0.16; 'url:wlfraed': 0.16; 'value;': 0.16; 'wulfraed': 0.16; 'wrote:': 0.16; 'operations.': 0.18; 'jan': 0.19; 'url:home': 0.21; 'maybe': 0.21; '+0100,': 0.23; 'complete,': 0.23; 'received:166': 0.23; 'code': 0.25; 'tried': 0.27; 'coding': 0.28; 'lee': 0.28; 'work.': 0.28; 'exiting': 0.30; 'threads': 0.30; 'skip:( 20': 0.31; 'does': 0.32; 'objects': 0.32; 'supposed': 0.32; 'tue,': 0.32; 'list': 0.32; 'to:addr:python-list': 0.33; 'rather': 0.34; 'flag': 0.34; 'stuck': 0.34; 'operations': 0.34; 'header:X-Complaints-To:1': 0.34; 'probably': 0.35; 'running': 0.35; 'something': 0.35; 'thread': 0.36; 'charset:us-ascii': 0.36; 'received:org': 0.37; 'using': 0.37; 'signal': 0.38; 'some': 0.38; 'created': 0.38; 'should': 0.38; "i'd": 0.39; 'recommended': 0.39; 'called': 0.39; 'subject:: ': 0.39; 'did': 0.39; 'to:addr:python.org': 0.40; 'data': 0.40; 'quick': 0.60; 'stop': 0.61; 'course,': 0.61; 'waiting': 0.62; 'designers': 0.67; 'incorporate': 0.67; 'processes,': 0.67; 'directly.': 0.68; 'collection': 0.68; 'become': 0.69; 'dennis': 0.73; 'calls,': 0.84; 'heck,': 0.84; 'killed': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: Killing threads, and os.system() Date: Tue, 31 Jan 2012 11:04:05 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: mobile-166-147-103-093.mycingular.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328025863 news.xs4all.nl 6907 [2001:888:2000:d::a6]:44552 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19644 On Tue, 31 Jan 2012 08:45:53 +0100, Laurent Claessens 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 }). >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/