add an AnyIO equivalent of trio.as_safe_channel
- Lenguaje dominante
- Python
- Estrellas
- 2.5k
- Forks
- 260
- Merge medio
- 2 d 8 h
- PR fusionados (30 d)
- 19
Descripción
### Things to check first
- [x] I have searched the existing issues and didn't find my feature already requested there
### Feature description
trio has https://trio.readthedocs.io/en/stable/reference-core.html#trio.as_safe_channel we should add an anyio.as_safe_memory_object_stream
### Use case
From the trio docs:
The yield keyword offers a very convenient way to write iterators… which makes it really unfortunate that async generators are so difficult to call correctly. Yielding from the inside of a cancel scope or a nursery to the outside violates structured concurrency with consequences explained in PEP 789. Even then, resource cleanup errors remain common (PEP 533) unless you wrap every call in aclosing().
This decorator gives you the best of both worlds: with careful exception handling and a background task we preserve structured concurrency by offering only the safe interface, and you can still write your iterables with the convenience of yield. For example:
```py
@as_safe_channel
async def my_async_iterable(arg, *, kwarg=True):
while ...:
item = await ...
yield item
async with my_async_iterable(...) as recv_chan:
async for item in recv_chan:
...
```
# notes
This should be in core AnyIO because it requires private access to send_stream._state.open_receive_channels
Guía de contribución
Línea de trabajo
Start by reading Trio’s as_safe_channel reference and the AnyIO memory-object-stream entry points, with particular attention to the mentioned send_stream._state.open_receive_channels access. Done means AnyIO exposes an as_safe_memory_object_stream equivalent that supports the documented async-generator usage while preserving the stated structured-concurrency and cleanup guarantees.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- backend
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Tranquilo
- Claridad
- Bastante claro
- Aptitud para principiantes
- 45/100