This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Wait for cleanup coroutines before event loop is closed.
Type: enhancement Stage: resolved
Components: asyncio Versions: Python 3.10, Python 3.8
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, xloem, yselivanov
Priority: normal Keywords:

Created on 2021-01-06 13:56 by xloem, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg384503 - (view) Author: xloem (xloem) Date: 2021-01-06 13:56
To handle destruction of resources especially during exceptions, it would be nice if there were some way to provide coroutines/tasks that run at the termination or closure of an event loop.  There are a lot of api options here.  Maybe a simple one would be to have close() call a run_until_closed() function that starts all of these and steps the loop to wait for them, before closing, and provide run_close() functions to queue them for delay.
msg415411 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2022-03-17 13:17
asyncio.run() does the task
msg415430 - (view) Author: xloem (xloem) Date: 2022-03-17 17:05
I'm sorry, is this closure an error? Could you explain more how to use asyncio.run() to clean up resources when an unhandled exception is thrown? Is this new with a recent python improvement?
msg415432 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2022-03-17 17:17
asyncio.run() present since Python 3.7
After the main coroutine finish, it cancels all background tasks and waits for their termination.

Background tasks can use old good `try/finally` for resources cleanup.
An additional API is not required IMHO.
msg415481 - (view) Author: xloem (xloem) Date: 2022-03-18 10:24
hey, I don't have the capacity to stay on this, but thanks for the attention, time, and clear response.

there are of course other situations such as returning a resource to library code or manual loop management, but I don't have the use case present any more.

maybe an aatexit library could patch this in if needed.
History
Date User Action Args
2022-04-11 14:59:39adminsetgithub: 87004
2022-03-18 10:26:23xloemsetresolution: wont fix -> out of date
2022-03-18 10:25:02xloemsetresolution: out of date -> wont fix
2022-03-18 10:24:26xloemsetstatus: open -> closed

messages: + msg415481
2022-03-17 17:17:37asvetlovsetmessages: + msg415432
2022-03-17 17:05:02xloemsetstatus: closed -> open

messages: + msg415430
2022-03-17 13:17:06asvetlovsetstatus: open -> closed
resolution: out of date
messages: + msg415411

stage: resolved
2021-01-06 13:56:08xloemcreate