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


Groups > comp.lang.python > #18355

Avoid race condition with Popen.send_signal

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <jerome@jolimont.fr>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.078
X-Spam-Evidence '*H*': 0.85; '*S*': 0.00; 'raised': 0.07; 'finished,': 0.09; 'subprocess': 0.09; '(none': 0.16; '[errno': 0.16; 'header:X-Face:1': 0.16; 'oserror:': 0.16; 'received:lan': 0.16; 'try/except': 0.16; 'thanks.': 0.26; 'skip:" 30': 0.28; 'error': 0.29; 'skip:p 30': 0.29; 'args)': 0.30; 'there': 0.33; 'to:addr:python-list': 0.34; 'all.': 0.34; 'elegant': 0.34; 'issue': 0.35; 'file': 0.36; 'subject:with': 0.36; 'but': 0.37; 'signal': 0.38; 'should': 0.39; 'to:addr:python.org': 0.40; 'more': 0.61; 'type': 0.61; 'received:89': 0.64; 'alive': 0.67; 'race': 0.77; 'processus': 0.84; 'running,': 0.84; 'safer,': 0.84
X-Spam-Checker-Version SpamAssassin 3.3.1 (2010-03-16) on jeftof
X-Spam-Level
X-Spam-Status No, score=-1.0 required=7.0 tests=ALL_TRUSTED autolearn=unavailable version=3.3.1
Date Tue, 3 Jan 2012 00:09:14 +0100
From Jérôme <jerome@jolimont.fr>
To python-list@python.org
Subject Avoid race condition with Popen.send_signal
X-Mailer Claws Mail 3.7.10 (GTK+ 2.24.8; x86_64-pc-linux-gnu)
X-Face "kBB1-!wF@,"j_&tJ&7; T,t)PeQkZg5?.:{p,s>/,+?b6pN5!yxZy^nRXA=*?W+|J9OG!W[rdx^VA^Sx`R"g,; +MzhAq"tZFg27W4qX+ZXvLX=%piZ6c.7@oSDHyQ0Mff#HGx<{
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4323.1325545598.27778.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1325545598 news.xs4all.nl 6937 [2001:888:2000:d::a6]:48735
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:18355

Show key headers only | View raw


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 ?

Thanks.

-- 
Jérôme

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


Thread

Avoid race condition with Popen.send_signal Jérôme <jerome@jolimont.fr> - 2012-01-03 00:09 +0100
  Re: Avoid race condition with Popen.send_signal Adam Skutt <askutt@gmail.com> - 2012-01-02 17:19 -0800
    Re: Avoid race condition with Popen.send_signal Heiko Wundram <modelnine@modelnine.org> - 2012-01-03 13:31 +0100
      Re: Avoid race condition with Popen.send_signal Adam Skutt <askutt@gmail.com> - 2012-01-03 05:40 -0800
        Re: Avoid race condition with Popen.send_signal Heiko Wundram <modelnine@modelnine.org> - 2012-01-03 14:56 +0100

csiph-web