prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.28k stars 715 forks source link

FIX: do not create a new event loop so often #1718

Closed tacaswell closed 1 year ago

tacaswell commented 1 year ago

Partially reverts prompt-toolkit/python-prompt-toolkit#1714

carlwgeorge commented 1 year ago

This looks to resolve #1719.

jonathanslenders commented 1 year ago

Thank you, but I don't really like the global.

The problem here is that we have a situation where an event loop was set, but is not running, and calling get_running_loop() fails. We could call get_event_loop() which would return the event loop that was previously set, but that emits a warning on newer Python versions. So, if we call get_running_loop(), and that fails, after which we create and set a new loop, that will leak file descriptors from the previous loop.

I'll look into using asyncio.run() instead, and dropping Python 3.6. I think that's fine.

tacaswell commented 1 year ago

As near as I can tell, there is no way to ask asyncio "do you have a set but not running event loop?" as all flavors of get_event_loop have both the auto-creation (+ warning) baked in. I do not think making new loops is so much the problem, but failing to close them (which leaves some pipes open)

I think the options (other than switching to asyncio.run()) are all not great:

tacaswell commented 1 year ago

I see #1721 was opened so I'm going to close this.

Sorry for the noise!