Read only as much as is available in Python

时间:2018-12-03 13:07:14

标签: python blocking

Suppose an I/O notification framework (such as select or GLib watches) just notified me that there is data available on a pipe. Assume that the underlying file descriptor is in blocking mode instead of non-blocking. How do I read as much data as is available immediately without blocking?

Using the Unix system call API I would just call read(). The Python read() method for file-like objects seems to have more complex behavior - the documentation is not crystal clear but I get the impression that it tries to do several low-level OS reads until it gets the requested number of bytes or end-of-file. To read only as much as is available right away, should I get the underlying file descriptor number and call os.read() on that?

Also, can I call os.read() in a loop until it returns less than the requested number of bytes? That is, can I do this loop each time the select notifies me that data is available? Does that run the risk of blocking?

0 个答案:

没有答案