[New-bugs-announce] [issue24795] Make event loops with statement context managers

Mathias Fröjdman report at bugs.python.org
Wed Aug 5 14:38:31 CEST 2015


New submission from Mathias Fröjdman:

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

----------
components: asyncio
files: patch
messages: 248032
nosy: Mathias Fröjdman, gvanrossum, haypo, yselivanov
priority: normal
severity: normal
status: open
title: Make event loops with statement context managers
type: enhancement
versions: Python 3.5
Added file: http://bugs.python.org/file40129/patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24795>
_______________________________________


More information about the New-bugs-announce mailing list