Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for cleanup coroutines before event loop is closed. #87004

Closed
xloem mannequin opened this issue Jan 6, 2021 · 5 comments
Closed

Wait for cleanup coroutines before event loop is closed. #87004

xloem mannequin opened this issue Jan 6, 2021 · 5 comments
Labels
3.8 only security fixes 3.10 only security fixes topic-asyncio type-feature A feature request or enhancement

Comments

@xloem
Copy link
Mannequin

xloem mannequin commented Jan 6, 2021

BPO 42838
Nosy @asvetlov, @1st1, @xloem

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2022-03-18.10:24:26.007>
created_at = <Date 2021-01-06.13:56:08.313>
labels = ['type-feature', '3.8', '3.10', 'expert-asyncio']
title = 'Wait for cleanup coroutines before event loop is closed.'
updated_at = <Date 2022-03-18.10:26:23.855>
user = 'https://github.com/xloem'

bugs.python.org fields:

activity = <Date 2022-03-18.10:26:23.855>
actor = 'xloem'
assignee = 'none'
closed = True
closed_date = <Date 2022-03-18.10:24:26.007>
closer = 'xloem'
components = ['asyncio']
creation = <Date 2021-01-06.13:56:08.313>
creator = 'xloem'
dependencies = []
files = []
hgrepos = []
issue_num = 42838
keywords = []
message_count = 5.0
messages = ['384503', '415411', '415430', '415432', '415481']
nosy_count = 3.0
nosy_names = ['asvetlov', 'yselivanov', 'xloem']
pr_nums = []
priority = 'normal'
resolution = 'out of date'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue42838'
versions = ['Python 3.8', 'Python 3.10']

@xloem
Copy link
Mannequin Author

xloem mannequin commented Jan 6, 2021

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.

@xloem xloem mannequin added 3.8 only security fixes 3.10 only security fixes topic-asyncio type-feature A feature request or enhancement labels Jan 6, 2021
@asvetlov
Copy link
Contributor

asyncio.run() does the task

@xloem
Copy link
Mannequin Author

xloem mannequin commented Mar 17, 2022

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?

@xloem xloem mannequin reopened this Mar 17, 2022
@xloem xloem mannequin reopened this Mar 17, 2022
@asvetlov
Copy link
Contributor

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.

@xloem
Copy link
Mannequin Author

xloem mannequin commented Mar 18, 2022

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.

@xloem xloem mannequin closed this as completed Mar 18, 2022
@xloem xloem mannequin closed this as completed Mar 18, 2022
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 only security fixes 3.10 only security fixes topic-asyncio type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant