Skip to content

Add stream method for non-destructive reading #562

Description

@agronholm

Things to check first

  • I have searched the existing issues and didn't find my feature already requested there

Feature description

Sometimes applications or protocol implementations would benefit from the ability to see if there's data ready for reading from the stream, but without actually removing said data from the buffer. For socket streams, this could be implemented using recv() with the MSG_PEEK flag. This should work on all platforms. For TLS streams, this is also doable but a tad more complex. One consideration would be that the stream might not return the same result when a destructive read is done after a non-destructive read with a yield point in between, but I think this could just be documented (and can be avoided by not sharing the stream among tasks in the first place).

Use case

This was mainly inspired by redis-py wanting to defensively check if the stream has any more data to be read. That said, I've come across other cases which I can't quite remember, where the knowledge of pending incoming data would help optimize the operation of a protocol implementation.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions