Skip to content

Instantly share code, notes, and snippets.

@graingert
Created August 4, 2022 11:59
Show Gist options
  • Save graingert/e8ddd68a0513bd7c02531a77a79cdacd to your computer and use it in GitHub Desktop.
Save graingert/e8ddd68a0513bd7c02531a77a79cdacd to your computer and use it in GitHub Desktop.
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())
@gvanrossum
Copy link

Please file a CPython issue for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment