-
-
Notifications
You must be signed in to change notification settings - Fork 367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix eventloop integration with anyio #1265
Conversation
Minimum version tests are failing because in |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Thanks @blink1073 |
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 |
@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]: %matplotlib qt
In [2]: import matplotlib.pyplot as plt
In [3]: plt.plot([1,3,2])
<check plot window is displayed and is interactive, then close it>
In [4]: %gui
In [5]: %gui qt
In [6]: plt.plot([2,3,1])
<check plot window is displayed and in interactive> 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
,tk
andosx
Matplotlib backends and Jupyterlab
,console
,qtconsole
andspyder
.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_stream
toshell_socket
as 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.Event
is used to trigger callingenter_eventloop
. This allows the synchronousenable_gui
to trigger the asynchronousenter_eventloop
.advance_eventloop
andschedule_next
are replaced with a simple async loop.These changes are intentionally minimal to reduce the danger of breaking downstream code.
I have disabled
test_trio_loop
as 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 byanyio
this shouldn't be too onerous. That is separate work to this though. I also noted thatTrioRunner
still exists but is not used any where.