add an AnyIO equivalent of trio.as_safe_channel
- Ngôn ngữ chính
- Python
- Star
- 2.5k
- Fork
- 260
- Merge trung bình
- 2 ngày 8 giờ
- Pull request đã merge (30 ngày)
- 19
Mô tả
### 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
Hướng dẫn đóng góp
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- backend
- Loại issue
- Tính năng
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Ít trao đổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 45/100