[issue24795] Make event loops with statement context managers

2017-12-20 Thread Andrew Svetlov

Andrew Svetlov  added the comment:

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.

--
nosy: +asvetlov
resolution:  -> wont fix
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24795] Make event loops with statement context managers

2015-08-06 Thread Guido van Rossum

Guido van Rossum added the comment:

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

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24795] Make event loops with statement context managers

2015-08-05 Thread Martin Panter

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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24795] Make event loops with statement context managers

2015-08-05 Thread STINNER Victor

STINNER Victor added the comment:

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

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24795] Make event loops with statement context managers

2015-08-05 Thread Guido van Rossum

Guido van Rossum added the comment:

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

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24795] Make event loops with statement context managers

2015-08-05 Thread Mathias Fröjdman

Mathias Fröjdman added the comment:

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

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24795] Make event loops with statement context managers

2015-08-05 Thread Mathias Fröjdman

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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com