Open this example in the interactive library
Source code
import asyncio
async def main():
task = asyncio.create_task(asyncio.sleep(60))
task.cancel()
try:
await task
except asyncio.CancelledError:
return 'cancelled'
print(asyncio.run(main()))Sample output
cancelled
