When creating asyncio tasks with asyncio.create_task()
or asyncio.ensure_future()
, you create independent units of work
that execute concurrently. However, if you don’t save the returned task object in a variable or collection, the task may be garbage collected at any
time, even before it’s done.
This happens because the event loop only maintains a weak reference to tasks. Without a strong reference:
- Tasks may be terminated unpredictably before completion
- Application behavior becomes inconsistent and difficult to debug
- Exceptions raised within the task are silently ignored
- Results of the task execution are lost
- Resources may not be properly released
For a task to run to completion and handle exceptions properly, you must save the task reference and eventually await it.