-
-
Notifications
You must be signed in to change notification settings - Fork 369
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
Trio loop #479
Trio loop #479
Conversation
This flag causes Trio to run on the main thread, and puts ZMQ and other stuff on a background thread. When trio runs on the main thread, async cells are executed as tasks inside a global nursery. A global nursery named GLOBAL_NURSERY is exported in builtins so that background tasks can continue to run even after a cell finishes executing. Exceptions in background tasks are caught and displayed in the current cell. This implicitly runs `%autoawait trio` when the kernel starts and then disables the %autoawait magic so that users can't switch to a different loop while in Trio loop mode. Co-authored-by: Brian Mackintosh <bcmackintosh@gmail.com>
This minimizes the amount of change in kernelapp.py. Co-authored-by: Brian Mackintosh <bcmackintosh@gmail.com>
The trio runner didn't support kernel interrupts: it would just continue to hang. I'm not sure why, but kernelapp.py ignores SIGINT. So in this commit, the Trio runner registers a new SIGINT handler that uses Trio cancel scopes to cancel the currently executing cell. The downside to this approach is that if a cell hangs in a loop that never reaches a Trio checkpoint, then the cancellation will have no effect. We could have sent SIGINT to the main thread (i.e. SIG_DFL), but that would have the effect of potentially raising SIGINT on one of the background tasks, which would likely force you to restart the kernel.
Matching PR is in IPython and has been released as IPython 7.12. |
Firstly thank you for this work, I am trying to use trio and derivative classes in a ipython/ipykernel running within Hydrogen in Atom IDE. (version details below) Your Untitled17 example above works but I am getting some issues. I will write the 2nd issue in a followup comment. Important to note the issue does not appear when I run in Jupyter notebook 1)In AtomIDE with Hydrogen on the first run of a kernel previously stopped. Executing
Retrying the Versions:ipykernel : origin https://github.com/HyperionGray/ipykernel.git -branch: trio-loop commit 3bb1bf5 31/1/2020 |
Warnings from This is a 2nd Issue to #479 (comment) ,same version details apply |
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.
Thanks!
For people who read this thread in the future, I made a new package |
Nice, thanks for sharing @mehaase! |
This PR adds support for running a long-lived Trio event loop on the foreground thread and pushes the ZMQ/other kernel stuff into a background thread. When Trio runs on the main thread, all cells are executed as tasks inside a global nursery. The global nursery is also injected into
builtins
asGLOBAL_NURSERY
so that notebook users can run tasks in the background even after a cell finishes executing. Exceptions in background tasks are caught and displayed in the current cell.This requires a matching PR on IPython itself!!
The loop is activated by passing a kernel flag, so by default this PR doesn't affect any current users—not even existing
%autoawait trio
users. It's completely opt-in. The following example configuration shows how to start a kernel in Trio loop mode:(Save this file as
.../share/jupyter/python-trio/kernel.json
. The leading...
part will vary depending on whether you're installing system-wide, per-user, or within a Python venv.)In Trio loop mode, the kernel implicitly runs
%autoawait trio
at startup. It also disables the%autoawait
magic to prevent users from trying to switch loops, which would certainly fail.Here's an example notebooks session running in this mode:
We know the code isn't pretty, and we're seeking feedback on some of the implementation decisions, including (but not limited to):
builtins
is pretty hacky, as is hard-coding the nameGLOBAL_NURSERY
.TrioRunner
created inkernelapp.py
into the shell instance, where it is needed to run cells on the main Trio loop.%autoawait
and how best to do so. If disabled, what's the best way to warn the user?