Uh oh!
There was an error while loading. Please reload this page.
Fix eventloop integration with anyio - #1265
Conversation
ianthomas23
commented
Aug 9, 2024
Minimum version tests are failing because in |
ianthomas23
commented
Aug 15, 2024
I have bumped the minimum anyio version from 4.0.0 (August 2023) to 4.2.0 (December 2023). Now the only test failures are the same as those occurring on the |
ianthomas23
commented
Aug 16, 2024
Thanks @blink1073 |
jdranczewski
commented
Aug 21, 2024
Hey, thanks so much for implementing these changes! I noticed this would be a problem back in #1079 (comment) (and #1079 (comment) to a lesser extent), but couldn't figure out a way to make the sync-async communication work within anyio. A small problem with this implementation is that AnyIO events are single-use (https://anyio.readthedocs.io/en/stable/synchronization.html#events) - so when previous versions of ipykernel allowed entering and exiting eventloops using the This feels like it could be an incremental change to this PR, so I didn’t know if I should open a new issue. Not sure how best to solve it - perhaps a loop in On a different note, the tk eventloop seems to be broken in Notebooks on Windows in my testing. Perhaps because it uses a nested asyncio loop? Or it could be some problem with |
ianthomas23
commented
Sep 2, 2024
@jdranczewski My apologies, I didn't realise Spyder supported re-entering event loops. The fix isn't quite as simple as looping in I'd personally prefer two new issues as then we have better traceability between problem and solution. I can create them but then they will probably be Do you have a simple workflow to reproduce the re-entrant problem in Spyder? I've tried this: In [1]: %matplotlibqtIn [2]: importmatplotlib.pyplotaspltIn [3]: plt.plot([1,3,2])
<checkplotwindowisdisplayedandisinteractive, thencloseit>In [4]: %guiIn [5]: %guiqtIn [6]: plt.plot([2,3,1])
<checkplotwindowisdisplayedandininteractive>to confirm it used to work but is currently broken, but you may have something better. |
Fixes#1235.
This fixes GUI event loop integration so that it works following the recent switch to using anyio, so that Matplotlib output correctly displays in separate Windows and both the kernel and plot respond to subsequent input. Such functionality has never been explicitly tested in ipykernel, so I have tested it manually on Linux, macOS and Windows with various combinations of
qt,tkandosxMatplotlib backends and Jupyterlab,console,qtconsoleandspyder.Here's a screenshot:
The plot can be panned and zoomed for example, which cannot be shown in such a screenshot.
Summary of changes:
shell_streamtoshell_socketas the former is no longer used.shell_stream.flush(limit=1)toshell_socket.get(zmq.EVENTS) & zmq.POLLIN) > 0.enable_gui) after the kernel has started, a newanyio.Eventis used to trigger callingenter_eventloop. This allows the synchronousenable_guito trigger the asynchronousenter_eventloop.advance_eventloopandschedule_nextare replaced with a simple async loop.These changes are intentionally minimal to reduce the danger of breaking downstream code.
I have disabled
test_trio_loopas the othertrio-based tests are disabled and we don't appear to support it yet, although as it is one of the async backends supported byanyiothis shouldn't be too onerous. That is separate work to this though. I also noted thatTrioRunnerstill exists but is not used any where.