An `async` task can only be cancelled when it reaches an `await`. If there is no `await`, the generator (function with `yield`) can not be cancelled properly and may keep running even after cancellation is requested.
An `async` task can only be cancelled when it reaches an `await`. If there is no `await`, the generator can not be cancelled properly and may keep running even after cancellation is requested.
Since this small example does not need any `await` statements, we add an `await anyio.sleep(0)` to give the event loop a chance to handle cancellation.
Since this small example does not need any `await` statements, we add an `await asyncio.sleep(0)` to give the event loop a chance to handle cancellation.
This would be even more important with large or infinite streams.