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

John O'Hagan research at
Fri Feb 4 16:48:55 CET 2011

On Thu, 3 Feb 2011, Nobody wrote:
> On Tue, 01 Feb 2011 08:30:19 +0000, John O'Hagan wrote:
> > 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.
> It isn't. os.path.devnull is a string, not a file. But even if you did:
> 	proc.stdout = open(os.path.devnull, 'w')
> that still wouldn't work.

As mentioned earlier in the thread, I did in fact use open(), this was a typo,
> > Is it possible to re-assign the stdout of a subprocess after it has
> > started?
> No.
> > Or just close it? What's the right way to read stdout up to a given
> > line, then discard the rest?
> If the server can handle the pipe being closed, go with that. Otherwise,
> options include redirecting stdout to a file and running "tail -f" on the
> file from within Python, or starting a thread or process whose sole
> function is to read and discard the server's output.

Thanks, that's all clear now. 

But I'm still a little curious as to why even unsuccessfully attempting to 
reassign stdout seems to stop the pipe buffer from filling up.


More information about the Python-list mailing list