python_asyncio_course/6_tasks/6_9_cancelation.py
2024-11-17 23:18:06 +03:00

23 lines
430 B
Python

import asyncio
async def cancel_test():
try:
await asyncio.sleep(5)
except asyncio.CancelledError:
print("Printing after cancel")
finally:
return 5
async def main():
task = asyncio.create_task(cancel_test())
await asyncio.sleep(0.1)
print(task.cancelling())
await asyncio.sleep(0.1)
print(task.cancelled())
print(task.result())
return task
asyncio.run(main())