non blocking read()

U

Uwe Mayer

Hi,

I use select() to wait for a file object (stdin) to become readable. In that
situation I wanted to read everything available from stdin and return to
the select statement to wait for more.

However, the file object's read method blocks if the number of bytes is 0 or
negative.

Is there no way to read everything a channel's got currently got without
blocking?

Uwe
 
D

Donn Cave

Uwe Mayer said:
Hi,

I use select() to wait for a file object (stdin) to become readable. In that
situation I wanted to read everything available from stdin and return to
the select statement to wait for more.

However, the file object's read method blocks if the number of bytes is 0 or
negative.

Is there no way to read everything a channel's got currently got without
blocking?


Yes, there is a way - os.read() (also known as posix.read())

It's better not to mix buffered I/O (like file object
I/O functions) with select() at all, because select()
actually applies to system level file descriptors and
doesn't know anything about the buffer.

Get the file descriptor with fileno(), and never refer
to the file object again after that.

Donn Cave, (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top