agronholm / agronholm/anyio

Add stream method for non-destructive reading

Ouverte
#562 10 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
enhancement
Langage dominant
Python
Étoiles
2.5k
Forks
260
Merge moyen
1 j 8 h
PR mergées (30 j)
17

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.

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.