Overview
- An asyncio.Task object is marked done and the method done() returns True if the wrapped coroutine has exited.
- The exit of the coroutine could be due to any of these reasons:
- It returns False when the wrapped coroutine is in suspended or in running state.
Example
# Example Python program that prints
# the status of a asyncio.Task before
# and after it is complete
import asyncio
# Define coroutine
async def cr():
for i in range(0, 10, 2):
print(i)
await asyncio.sleep(0)
# A coroutine that will bootstrap a Task
async def main():
tsk = asyncio.create_task(cr())
# Task is not done yet
print("Task done:%s"%tsk.done())
await tsk
# Task is done now
print("Task done:%s"%tsk.done())
# Run the coroutine main() in an asyncio event loop
l = asyncio.new_event_loop()
asyncio.set_event_loop(l)
asyncio.run(main())Output
Task done:False
0
2
4
6
8
Task done:True