Skip to content

Confusing behavior of selectors with buffered file objects #101053

Open
@xi

Description

@xi

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 or socket.makefile(). In those cases, it is recommended to bypass the buffering by using os.read(fileobj.fileno(), size) instead of fileobj.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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions