Created
August 4, 2022 11:59
-
-
Save graingert/e8ddd68a0513bd7c02531a77a79cdacd to your computer and use it in GitHub Desktop.
This file contains 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
import asyncio | |
import contextlib | |
async def child(sock): | |
try: | |
with contextlib.closing(sock): | |
await asyncio.sleep(2) | |
except Exception as e: | |
print(f"child task got error: {type(e)=} {e=}") | |
raise | |
class Sock: | |
async def aclose(self): | |
await asyncio.sleep(1) | |
async def main(): | |
try: | |
sock = Sock() | |
async with asyncio.timeout(1): | |
async with asyncio.TaskGroup() as tg: | |
# Make two concurrent calls to child() | |
tg.create_task(child(Sock())) | |
tg.create_task(child(Sock())) | |
async with contextlib.aclosing(sock): | |
await asyncio.sleep(2) | |
except* AttributeError: | |
print("handled value error") | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please file a CPython issue for this.