Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util/helper: introduce ensure_event_loop()
Calling asyncio.get_event_loop() with no current event loop is deprecated since Python 3.10 and will be an error in some future Python release [1]. Using it causes errors in IPython when using a RemotePlace. Instead of using asyncio.get_event_loop(), add a new helper function ensure_event_loop(): Try to retrieve the OS thread's event loop from a ContextVar. If the ContextVar is not set yet, try to get the current loop from asyncio.get_running_loop() and store it in the ContextVar. If there is no loop, create a new one, set it as the current event loop for the current thread and store in the ContextVar. Finally return the found or newly created loop. This behavior mimics what asyncio.get_event_loop() did. It is also used by jupyter_core [2]. [1] https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.get_event_loop [2] jupyter/jupyter_core#387 Signed-off-by: Bastian Krause <[email protected]>
- Loading branch information