agronholm / agronholm/anyio

Add stream method for non-destructive reading

Open
#562 10 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
2.5k
Forks
260
Avg merge
2d 8h
Merged PRs (30d)
19

Description

### Things to check first

- [X] 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.