Created
October 21, 2018 02:03
-
-
Save dsvictor94/39b23387005ef8fe96afa1fa6a1d4dea 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 | |
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