Description
Using selectors with buffered file objects is confusing because the selector will only report whether there is something to read in the underlying file descriptor and ignore the internal buffer. This is especially relevant because buffered file objects are used in many places in the standard library, e.g. sys.stdin
or socket.makefile()
.
I propose to add a corresponding warning to the docs, e.g. like this:
Selectors only look at the underlying file descriptors, so they do not know about data that is buffered in python code. Buffered file objects are used in many places in the standard library, e.g.
sys.stdin
orsocket.makefile()
. In those cases, it is recommended to bypass the buffering by usingos.read(fileobj.fileno(), size)
instead offileobj.read(size)
.
Even better would be add code to handle buffered file objects. Unfortunately, I think that BufferedIOBase
does not expose whether it has data in its buffer.