Skip to content

Instantly share code, notes, and snippets.

@dsvictor94
Created October 21, 2018 02:03
Show Gist options
  • Save dsvictor94/39b23387005ef8fe96afa1fa6a1d4dea to your computer and use it in GitHub Desktop.
Save dsvictor94/39b23387005ef8fe96afa1fa6a1d4dea to your computer and use it in GitHub Desktop.
import asyncio
async def A():
try:
await asyncio.sleep(100)
except asyncio.CancelledError:
print("canceled (A)")
async def B():
try:
await A()
except asyncio.CancelledError:
print("canceled (B)")
raise
print('will block forever and prevents C to cancels')
await asyncio.sleep(100000)
async def C():
try:
await B()
except asyncio.CancelledError:
print("canceled (C)")
raise
task = asyncio.ensure_future(C())
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(task)
except KeyboardInterrupt:
task.cancel()
try:
loop.run_until_complete(task)
except asyncio.CancelledError:
pass
print("Task is canceled:", task.cancelled())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment