Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'raised': 0.07; 'finished,': 0.09; 'from:addr:python': 0.09; 'subprocess': 0.09; 'exception': 0.12; '(none': 0.16; '[errno': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'oserror:': 0.16; 'pythonic': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'reply-to:addr:python-list': 0.16; 'try/except': 0.16; 'wrote:': 0.18; 'header:In-Reply-To:1': 0.22; 'skip:" 30': 0.28; 'error': 0.29; 'skip:p 30': 0.29; 'args)': 0.30; 'header:User-Agent:1': 0.33; 'there': 0.33; 'to:addr:python- list': 0.34; 'probably': 0.34; 'received:84': 0.34; 'all.': 0.34; 'elegant': 0.34; 'reply-to:addr:python.org': 0.34; 'issue': 0.35; 'file': 0.36; 'subject:with': 0.36; 'but': 0.37; 'think': 0.37; 'signal': 0.38; 'should': 0.39; 'to:addr:python.org': 0.40; 'more': 0.61; 'type': 0.61; 'alive': 0.67; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'race': 0.77; 'processus': 0.84; 'running,': 0.84; 'safer,': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=J8QoHXbS c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=9jsOeB20M3cA:10 a=cnDK9OXQaNoA:10 a=OUOv7kDek9cA:10 a=IkcTkHD0fZMA:10 a=6jXDz_yh2wKmuOLkj70A:9 a=lyc96o49awFmSGgW_JcA:7 a=QEXdDO2ut3YA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Mon, 02 Jan 2012 23:51:37 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Avoid race condition with Popen.send_signal References: <20120103000914.79f2ac94@bouzin.lan> In-Reply-To: <20120103000914.79f2ac94@bouzin.lan> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org 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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1325548300 news.xs4all.nl 6852 [2001:888:2000:d::a6]:56918 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18356 On 02/01/2012 23:09, Jérôme wrote: > Hi all. > > When a subprocess is running, it can be sent a signal with the send_signal > method : > > process = Popen( args) > process.send_signal(signal.SIGINT) > > If the SIGINT is sent while the process has already finished, an error is > raised : > > File "/usr/lib/python2.7/subprocess.py", line 1457, in send_signal > os.kill(self.pid, sig) > OSError: [Errno 3] Aucun processus de ce type > > To avoid this, I can check that the process is still alive : > > process = Popen( args) > process.poll() > if (None == process.returncode): > process.send_signal(signal.SIGINT) > > It makes safer, but there is still an issue if the process ends between > poll() and send_signal(). > > What is the clean way to avoid this race condition ? > > Should I use try/except to catch the error or is there a more elegant way to > go ? > I think that catching the exception is probably the most Pythonic way.