Make asyncio.get_event_loop a builtin

Hi all, Just a simple idea I wanted to bring forth. Although I know that you get a lot more asyncio control by importing the asyncio module itself, I'd like to see a way to make simple asynchronous applications without ever importing asyncio itself. To that end, I propose making asyncio.get_event_loop() a builtin. This would enable simple things like this (example copied from websockets.readthedocs.io with slight modifications): import websockets async def hello(): async with websockets.connect('wss://echo.websocket.org') as ws: amsg = 'a message' print(f'> {amsg}') await ws.send(amsg) ret = await ws.recv() print(f'< {ret}') get_event_loop().run_until_complete(hello()) See how that never imported asyncio? I just think it would be more convenient this way. But there may be major problems that I hadn't anticipated, so as always: Thoughts? Sincerely, Ken H ilton ;

On 5/22/2018 5:21 AM, Ken Hilton wrote:
(Should that really be 'hello()' rather than 'hello'? I don't remember.) I like the idea of making coroutines easier and use. It would make more sense to me to expose an eventloop class as a builtin, so that one would write eventloop().run_until_complete(hello) eventloop would not necessarily have to be exactly the same as the default returned by get_event_loop. Would all the asyncio eventloop methods be needed? For running coroutines, it would be even nicer to write eventloop().run(hello) Eventloop could have an .__init__ method, or be a factory function, with a 'loop' parameter. The value specifies which eventloop implementation adaptor to use. The default might be 'asyncio', with alternatives such as 'uvloop', 'tkloop' (partly prototyped), 'twisted', and others. The adaptors should all have the same api: .run method that exits on completion, or a .run (forever) method paired with .stop. -- Terry Jan Reedy

On Tue, May 22, 2018 at 2:09 PM Terry Reedy <tjreedy@udel.edu> wrote: [..]
eventloop().run_until_complete(hello)
eventloop().run(hello)
We have asyncio.run() in 3.7: https://docs.python.org/3.7/library/asyncio-task.html#asyncio.run (event loop can be customized via policy). Yury

To that end, I propose making asyncio.get_event_loop() a builtin.
I like the idea of making coroutines easier and use. I do too, but ...
this is a key point -- while asyncio is in the standard library, it is not intended to be THE async event loop implementation -- there are others already, and hopefully that will continue (trio looks pretty cool, for instance...) so yes to making async easier, but no to putting asycio in builtins. even the idea of a builtin EventLoop that other implementations could register with seems kinda pointless -- why not import the one you want? -CHB -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov

On 5/22/2018 5:21 AM, Ken Hilton wrote:
(Should that really be 'hello()' rather than 'hello'? I don't remember.) I like the idea of making coroutines easier and use. It would make more sense to me to expose an eventloop class as a builtin, so that one would write eventloop().run_until_complete(hello) eventloop would not necessarily have to be exactly the same as the default returned by get_event_loop. Would all the asyncio eventloop methods be needed? For running coroutines, it would be even nicer to write eventloop().run(hello) Eventloop could have an .__init__ method, or be a factory function, with a 'loop' parameter. The value specifies which eventloop implementation adaptor to use. The default might be 'asyncio', with alternatives such as 'uvloop', 'tkloop' (partly prototyped), 'twisted', and others. The adaptors should all have the same api: .run method that exits on completion, or a .run (forever) method paired with .stop. -- Terry Jan Reedy

On Tue, May 22, 2018 at 2:09 PM Terry Reedy <tjreedy@udel.edu> wrote: [..]
eventloop().run_until_complete(hello)
eventloop().run(hello)
We have asyncio.run() in 3.7: https://docs.python.org/3.7/library/asyncio-task.html#asyncio.run (event loop can be customized via policy). Yury

To that end, I propose making asyncio.get_event_loop() a builtin.
I like the idea of making coroutines easier and use. I do too, but ...
this is a key point -- while asyncio is in the standard library, it is not intended to be THE async event loop implementation -- there are others already, and hopefully that will continue (trio looks pretty cool, for instance...) so yes to making async easier, but no to putting asycio in builtins. even the idea of a builtin EventLoop that other implementations could register with seems kinda pointless -- why not import the one you want? -CHB -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
participants (4)
-
Chris Barker
-
Ken Hilton
-
Terry Reedy
-
Yury Selivanov