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

Make event loops with statement context managers #68983

Closed
MathiasFrjdman mannequin opened this issue Aug 5, 2015 · 7 comments
Closed

Make event loops with statement context managers #68983

MathiasFrjdman mannequin opened this issue Aug 5, 2015 · 7 comments
Labels
topic-asyncio type-feature A feature request or enhancement

Comments

@MathiasFrjdman
Copy link
Mannequin

MathiasFrjdman mannequin commented Aug 5, 2015

BPO 24795
Nosy @gvanrossum, @vstinner, @asvetlov, @vadmium, @1st1
Files
  • patch: Patch against python 3.5b4 asyncio/base_events.py
  • 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 2017-12-20.21:18:01.081>
    created_at = <Date 2015-08-05.12:38:28.664>
    labels = ['type-feature', 'expert-asyncio']
    title = 'Make event loops with statement context managers'
    updated_at = <Date 2017-12-20.21:18:01.080>
    user = 'https://bugs.python.org/MathiasFrjdman'

    bugs.python.org fields:

    activity = <Date 2017-12-20.21:18:01.080>
    actor = 'asvetlov'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-12-20.21:18:01.081>
    closer = 'asvetlov'
    components = ['asyncio']
    creation = <Date 2015-08-05.12:38:28.664>
    creator = 'Mathias Fr\xc3\xb6jdman'
    dependencies = []
    files = ['40129']
    hgrepos = []
    issue_num = 24795
    keywords = []
    message_count = 7.0
    messages = ['248032', '248034', '248035', '248087', '248090', '248116', '308820']
    nosy_count = 6.0
    nosy_names = ['gvanrossum', 'vstinner', 'asvetlov', 'martin.panter', 'yselivanov', 'Mathias Fr\xc3\xb6jdman']
    pr_nums = []
    priority = 'normal'
    resolution = 'wont fix'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue24795'
    versions = ['Python 3.5']

    @MathiasFrjdman
    Copy link
    Mannequin Author

    MathiasFrjdman mannequin commented Aug 5, 2015

    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

    @MathiasFrjdman MathiasFrjdman mannequin added topic-asyncio type-feature A feature request or enhancement labels Aug 5, 2015
    @MathiasFrjdman
    Copy link
    Mannequin Author

    MathiasFrjdman mannequin commented Aug 5, 2015

    (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.

    @gvanrossum
    Copy link
    Member

    This seems the wrong idea to me. Event loops should be long-lived, so the
    context manager would ideally see very little use.

    @vstinner
    Copy link
    Member

    vstinner commented Aug 5, 2015

    +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.

    @vadmium
    Copy link
    Member

    vadmium commented Aug 6, 2015

    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).

    @gvanrossum
    Copy link
    Member

    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\>


    @asvetlov
    Copy link
    Contributor

    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.

    @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
    topic-asyncio type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants