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: Make event loops with statement context managers
Type: enhancement Stage: resolved
Components: asyncio Versions: Python 3.5
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: Mathias Fröjdman, asvetlov, gvanrossum, martin.panter, vstinner, yselivanov
Priority: normal Keywords:

Created on 2015-08-05 12:38 by Mathias Fröjdman, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
patch Mathias Fröjdman, 2015-08-05 12:38 Patch against python 3.5b4 asyncio/base_events.py
Messages (7)
msg248032 - (view) Author: Mathias Fröjdman (Mathias Fröjdman) * Date: 2015-08-05 12:38
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
msg248034 - (view) Author: Mathias Fröjdman (Mathias Fröjdman) * Date: 2015-08-05 12:47
(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.
msg248035 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2015-08-05 13:02
This seems the wrong idea to me. Event loops should be long-lived, so the
context manager would ideally see very little use.
msg248087 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-08-05 23:17
+1 for me. Asyncio examples already have this try/finally pattern. I
already proposed to support context manager some months ago.

Guido, I don't understand your point. Usually the main function id
loop.run_until_complete/.run_forever. That's all. It doesn't mean that the
loop only runs a few milliseconds.
msg248090 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-08-06 00:57
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).
msg248116 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2015-08-06 09:10
My worry is that the context manager will make people believe it's a good
pattern to create an event loop just to make one call. If tests violate
this pattern, add a context manager helper function to test_utils.py.

On Thu, Aug 6, 2015 at 2:57 AM, Martin Panter <report@bugs.python.org>
wrote:

>
> Martin Panter added the comment:
>
> >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).
>
> ----------
> nosy: +vadmium
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue24795>
> _______________________________________
>
msg308820 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2017-12-20 21:18
Superseded by `asyncio.run()` function.

P.S.
Context manager is not a solution because `loop.shutdown_asyncgens()` should be called before `loop.close()` and the method is a coroutine.
History
Date User Action Args
2022-04-11 14:58:19adminsetgithub: 68983
2018-02-24 00:27:18martin.panterlinkissue32875 superseder
2017-12-20 21:18:01asvetlovsetstatus: open -> closed

nosy: + asvetlov
messages: + msg308820

resolution: wont fix
stage: resolved
2015-08-06 09:10:42gvanrossumsetmessages: + msg248116
2015-08-06 00:57:52martin.pantersetnosy: + martin.panter
messages: + msg248090
2015-08-05 23:17:50vstinnersetmessages: + msg248087
2015-08-05 13:02:49gvanrossumsetmessages: + msg248035
2015-08-05 12:47:21Mathias Fröjdmansetmessages: + msg248034
2015-08-05 12:38:30Mathias Fröjdmancreate