Created
June 12, 2026 09:41
-
-
Save xqm32/a1159b26c193404d4238a0083f88080b to your computer and use it in GitHub Desktop.
asynccontext.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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