Skip to content

Instantly share code, notes, and snippets.

@njsmith
Created June 5, 2021 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njsmith/25df3e0adb56743ae2d8f50a8015103e to your computer and use it in GitHub Desktop.
Save njsmith/25df3e0adb56743ae2d8f50a8015103e to your computer and use it in GitHub Desktop.
async def with_item_timeout(aiter, timeout):
aiter = aiter.__aiter__()
while True:
with trio.move_on_after(timeout) as cscope:
try:
val = await aiter.__anext__()
except StopAsyncIteration:
return
if cscope.cancelled_caught:
print("cancelled")
return
yield val
async for val in with_item_timeout(some_async_iterable):
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment