Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 18th, 2005, 06:39 PM
Peter Ammon
Guest
 
Posts: n/a
Default nonblocking read()

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.

Are there better approaches? Thanks,

-Peter



  #2  
Old July 18th, 2005, 06:39 PM
Donn Cave
Guest
 
Posts: n/a
Default Re: nonblocking read()

In article <cnbgd0$ls4$1@news.apple.com>,
Peter Ammon <peter_ammon@rocketmail.com> wrote:
[color=blue]
> 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.[/color]

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@u.washington.edu
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,414 network members.