jupyter / jupyter_core

Core Jupyter functionality
https://jupyter-core.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
194 stars 180 forks source link

Modernize event loop behavior #387

Closed blink1073 closed 6 months ago

blink1073 commented 6 months ago

Based on discussion in https://github.com/python/cpython/issues/100160, we should not be relying on asyncio.get_event_loop, since they are in fact going to make it an alias for asyncio.get_running_loop.

This ensures we install an event loop before launching our apps, so we should not see any more issues like https://github.com/jupyter/notebook/issues/6721.

Additionally, it adds a new class, JupyterAsyncApp that runs initialize_async() and start_async() in an event loop.

Based on results in https://github.com/ipython/ipykernel/pull/1184, we'll want to normally use the default event loop on Windows, unless we know we're running a tornado app. This PR adds a prefer_selector_loop option to ensure_event_loop so we can override _prefer_selector_loop in jupyter_server apps to prefer the selector loop.

Based on failures seen in #383, we add tests to ensure we are not interfering with existing event loop behavior.

blink1073 commented 6 months ago

Could you name it something other than ‘get_event_loop’, to avoid confusion with the asyncio function by that name? Maybe ‘ensure_event_loop’?

Done, thank you!