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

From "John O'Hagan" <mail@johnohagan.com>
Organization johnohagan.com
Subject Reassign or discard Popen().stdout from a server process
Date 2011-02-01 08:30 +0000
Newsgroups comp.lang.python
Message-ID <mailman.1533.1296549030.6505.python-list@python.org> (permalink)

Show all headers | 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