Skip to content

Instantly share code, notes, and snippets.

@xqm32
Created June 12, 2026 09:41
Show Gist options
  • Select an option

  • Save xqm32/a1159b26c193404d4238a0083f88080b to your computer and use it in GitHub Desktop.

Select an option

Save xqm32/a1159b26c193404d4238a0083f88080b to your computer and use it in GitHub Desktop.
asynccontext.py
from contextlib import asynccontextmanager
from anyio import fail_after, run, sleep
@asynccontextmanager
async def func():
try:
with fail_after(1):
print(1)
yield
print(2)
except:
print(3)
raise
finally:
print(4)
async def main():
async with func():
try:
print(5)
await sleep(2)
print(6)
except:
print(7)
raise
finally:
print(8)
run(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment