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


Groups > comp.lang.python > #18356 > unrolled thread

Re: Avoid race condition with Popen.send_signal

Started byMRAB <python@mrabarnett.plus.com>
First post2012-01-02 23:51 +0000
Last post2012-01-02 23:51 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Avoid race condition with Popen.send_signal MRAB <python@mrabarnett.plus.com> - 2012-01-02 23:51 +0000

#18356 — Re: Avoid race condition with Popen.send_signal

FromMRAB <python@mrabarnett.plus.com>
Date2012-01-02 23:51 +0000
SubjectRe: Avoid race condition with Popen.send_signal
Message-ID<mailman.4324.1325548300.27778.python-list@python.org>
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.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web