Read/Write from/to a process

Donn Cave donn at drizzle.com
Tue Oct 25 11:29:12 EDT 2005


Quoth "jas" <codecraig at gmail.com>:
| Steve Holden wrote:
|> Look at how you might do it in other languages. Then you'll realise this
|> isn't (just) a Python problem.
|
| Yea your right.  However, for example, in Java, one can use the Process
| class, and then read from the stream until its the end (i.e. -1 is
| returned).  However, with Python when reading from
| subprocess.Popen.stdout ...I don't know when to stop (except for
| looking for a ">" or something).  Is there a standard, like read until
| "-1" or something?

Sure, end of file is '', a string with 0 bytes.  That means the other
end of the pipe has closed, usually due to exit of the other process
that was writing to it.  Not much help for you there, nor would the
Java equivalent be, I presume.

Even on UNIX, where pipes are a mainstay of ordinary applications,
this one would very likely need a pseudotty device instead, to prevent
C library block buffering, and it would still be difficult and unreliable.

Ironically the best support I've seen came from a platform that didn't
use pipes much at all, VAX/VMS (I put that in the past tense because
for all I know it may have evolved in this respect.)  The pipe-like
VMS device was called a "mailbox", and the interesting feature was
that you could be notified when a read had been queued on the device.

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list