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
Make event loops with statement context managers #68983
Comments
Since asyncio event loops have to be closed nowadays, it would be pretty convenient and pythonic to make BaseEventLoop a context manager that calls self.close() in __exit__ the same way as contextlib.closing() does it. Example: import asyncio
with asyncio.get_event_loop() as loop:
loop.run_until_complete(func()) instead of import asyncio
from contextlib import closing
with closing(asyncio.get_event_loop()) as loop:
loop.run_until_complete(func()) or event the bulkier import asyncio
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(func())
finally:
loop.close() The attached patch applies to Python 3.5b4's asyncio/base_events.py |
(Just noticed http://bugs.python.org/issue19860, which I originally failed to notice when just searching for "asyncio loop" and not context manager) Anyway, in recent Python/asyncio versions, failing to close the event loop before exiting whole the process can cause problems, so I think the case is valid now. |
This seems the wrong idea to me. Event loops should be long-lived, so the |
+1 for me. Asyncio examples already have this try/finally pattern. I Guido, I don't understand your point. Usually the main function id |
From what I can see, the examples in the current documentation tend to diectly call loop.close() without an exception handler. Only two examples have the bare-bones try / finally handler (which is important for the example that uses Ctrl+C). |
My worry is that the context manager will make people believe it's a good On Thu, Aug 6, 2015 at 2:57 AM, Martin Panter <report@bugs.python.org>
|
Superseded by P.S. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: