nonblocking read()
Donn Cave
donn at u.washington.edu
Mon Nov 15 19:41:12 EST 2004
In article <cnbgd0$ls4$1 at news.apple.com>,
Peter Ammon <peter_ammon at rocketmail.com> wrote:
> I would like to read from a pipe in which data may arrive slowly. From
> experimenting, it looks like os.read() will block until it returns the
> maximum amount of data you asked for, in the second parameter to read(),
> or until it hits EOF. I cannot find a way to return only the data that
> the file object has immediately available.
>
> If no data is available, blocking is OK.
>
> The only workaround I can think of is to call select() in a loop,
> reading and storing one byte each time, and then returning them when
> select() indicates that the pipe is not ready for reading.
I don't think that will work either!
But os.read (a.k.a. posix.read) does just what you want.
Use it with a file descriptor -
fp = os.popen(cmd, 'r')
fd = fp.fileno()
while 1:
data = os.read(fd, 4096)
if not data:
break
dispose_of(data)
Don't use the file object read or readline methods at all,
because they could leave data in their own input buffer,
where posix.read can't see it (nor select, which is why
I doubt your work-around would work.)
Donn Cave, donn at u.washington.edu
More information about the Python-list
mailing list