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


Groups > comp.lang.python > #55820

Reassign or discard Popen().stdout from a server process

Path csiph.com!eeepc.pasdenom.info!news.pasdenom.info!news.dougwise.org!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <johnmohagan@gmail.com>
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:localnet': 0.05; 'startup': 0.05; 'subject:process': 0.07; 'silently': 0.09; 'subprocess': 0.09; 'wary': 0.09; 'output': 0.10; 'server,': 0.12; 'case.': 0.15; "hasn't": 0.15; '(around': 0.16; 'added:': 0.16; 'failing,': 0.16; 'input,': 0.16; 'proc': 0.16; 'realised': 0.16; 'received:124.168': 0.16; 'sec),': 0.16; 'simplified': 0.16; 'stdout': 0.16; 'subject:discard': 0.16; 'subject:server': 0.16; 'worked.': 0.16; 'seems': 0.17; 'written': 0.20; 'fix': 0.20; 'file,': 0.21; 'tells': 0.21; 'sender:addr:gmail.com': 0.22; 'buffer': 0.23; 'working.': 0.23; 'thanks,': 0.26; 'closing': 0.27; 'bugs': 0.28; "i'm": 0.28; "can't": 0.28; 'discard': 0.28; "what's": 0.29; 'blocks': 0.30; 'form:': 0.30; 'stops': 0.30; 'server': 0.30; 'import': 0.31; 'to:addr:python-list': 0.31; 'starting': 0.31; "doesn't": 0.32; 'although': 0.32; "won't": 0.32; 'empty': 0.33; 'arising': 0.34; 'slow': 0.34; 'there': 0.34; 'pass': 0.34; 'header:User-Agent:1': 0.34; 'but': 0.36; 'line,': 0.36; 'think': 0.37; 'close': 0.37; 'skip:s 20': 0.38; 'it?': 0.38; 'received:209.85': 0.38; 'either': 0.38; 'received:google.com': 0.38; 'effect': 0.38; 'some': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; "it's": 0.40; 'ready': 0.60; 'fill': 0.61; 'header:Message-Id:1': 0.61; 'from:addr:mail': 0.64; 'misdirected': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:sender:from:organization:to:subject:date :user-agent:mime-version:content-type:content-transfer-encoding :message-id; bh=/Vv8OZV+G2oXcCFK3GWZLVcM6jsCr9AgiXrUbHcmQeo=; b=PXsYWX3/SaGhkZkN6i20+0LjedPZI/DcI7fpQor1HmdEHsybmSoBruWCTd9fHqqGnR 8ZbdslOoh9FFXfcsATeTD8aw6XGWJmwoY4Yzwn7hbatOlZF9WZcp2PgUf/+K74A8SDzv 7VTcRuAPMSFotb/nYOGvVNS5j/wkfr63uSi+Y=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=sender:from:organization:to:subject:date:user-agent:mime-version :content-type:content-transfer-encoding:message-id; b=IHzNJiTjvGlNkBhJX56TysgHrTS8nO/uqdULAt2HPEOJLyjHlsh7vhCuj0akuG4oZt zB7cAct9Bqu/nwPOAC5oVt0jSnZmrpLfDpTkxC1tQ3Xe3Xq7zhowc9pv3xtqmZoMdVUk ONvQCBI+eoBUKISU1UoI2jxWwMFoLyQJF5jfo=
Sender "John O'Hagan" <johnmohagan@gmail.com>
From "John O'Hagan" <mail@johnohagan.com>
Organization johnohagan.com
To python-list@python.org
Subject Reassign or discard Popen().stdout from a server process
Date Tue, 1 Feb 2011 08:30:19 +0000
User-Agent KMail/1.13.5 (Linux/2.6.32-2011-01-16-16.13; KDE/4.4.5; i686; ; )
MIME-Version 1.0
Content-Type Text/Plain; charset="utf-8"
Content-Transfer-Encoding 7bit
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.1533.1296549030.6505.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 82.94.164.166
X-Trace 1296549030 news.xs4all.nl 32470 [::ffff:82.94.164.166]:36257
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:55820

Show key headers only | View raw


I'm starting a server process as a subprocess. Startup is slow and 
unpredictable (around 3-10 sec), so I'm reading from its stdout until I get a 
line that tells me it's ready before proceeding, in simplified form:

import subprocess
proc = subprocess.Popen(['server', 'args'], stdout=subprocess.PIPE)
while proc.stdout.readline() != "Ready.\n":
    pass

Now I can start communicating with the server, but I eventually realised that 
as I'm no longer reading stdout, the pipe buffer will fill up with output from 
the server and before long it blocks and the server stops working. 

I can't keep reading because that will block - there won't be any more output 
until I send some input, and I don't want it in any case.

To try to fix this I added:

proc.stdout = os.path.devnull

which has the effect of stopping the server from failing, but I'm not convinced 
it's doing what I think it is. If I replace devnull in the above line with a 
real file, it stays empty although I know there is more output, which makes me 
think it hasn't really worked. 

Simply closing stdout also seems to stop the crashes, but doesn't that mean 
it's still being written to, but the writes are just silently failing? In 
either case I'm wary of more elusive bugs arising from misdirected stdout.

Is it possible to re-assign the stdout of a subprocess after it has started? 
Or just close it? What's the right way to read stdout up to a given line, then 
discard the rest?

Thanks,

john

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


Thread

Reassign or discard Popen().stdout from a server process "John O'Hagan" <mail@johnohagan.com> - 2011-02-01 08:30 +0000
  Re: Reassign or discard Popen().stdout from a server process "John O'Hagan" <research@johnohagan.com> - 2011-02-04 15:48 +0000
    Re: Reassign or discard Popen().stdout from a server process "John O'Hagan" <research@johnohagan.com> - 2011-02-10 08:35 +0000
    Re: Reassign or discard Popen().stdout from a server process Nobody <nobody@nowhere.com> - 2011-02-09 05:24 +0000
  Re: Reassign or discard Popen().stdout from a server process Nobody <nobody@nowhere.com> - 2011-02-03 09:48 +0000

csiph-web