Add schedule_display_output for pushing messages to frontends outside execution#382
Merged
blink1073 merged 2 commits intoCalysto:mainfrom Mar 15, 2026
Merged
Add schedule_display_output for pushing messages to frontends outside execution#382blink1073 merged 2 commits intoCalysto:mainfrom
blink1073 merged 2 commits intoCalysto:mainfrom
Conversation
…loses Calysto#198) Adds MetaKernel.schedule_display_output(callback) so kernel authors can safely send output to all connected frontends from background threads without an active cell execution. Uses Tornado's thread-safe add_callback when io_loop is available, falling back to a direct call otherwise. Includes four unit tests and documentation in new_kernel.md and new_magic.md.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #382 +/- ##
==========================================
+ Coverage 90.85% 90.86% +0.01%
==========================================
Files 51 51
Lines 2952 2957 +5
Branches 413 414 +1
==========================================
+ Hits 2682 2687 +5
Misses 190 190
Partials 80 80
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
References
closes #198
Description
Kernel authors sometimes need to push output to all connected frontends from a background thread — for example, when wrapping an application that emits periodic events or async notifications. Previously there was no supported way to do this.
This PR adds
MetaKernel.schedule_display_output(callback), which schedules a zero-argument callable on the kernel's main Tornado IO loop. Because ZMQ sockets are not thread-safe, callingPrint/Display/etc. directly from a background thread is unsafe; routing throughio_loop.add_callback(Tornado's thread-safe mechanism) ensures the message is delivered correctly to all frontends even when no cell execution is in progress. Whenio_loopis unavailable (e.g. in unit tests), the callback is invoked directly.Changes
metakernel/_metakernel.py: addschedule_display_output(callback)method; addCallableto typing importstests/test_metakernel.py: four new tests inTestScheduleDisplayOutputcovering the no-io_loop fallback, iopub message delivery, io_loop delegation, and a real background threaddocs/new_kernel.md: new "Pushing output from background threads" section with a complete exampledocs/new_magic.md: newschedule_display_outputentry in the kernel output helpers referenceBackwards-incompatible changes
None
Testing
New unit tests added. All existing tests pass (one pre-existing failure in
test_px_error_reports_exception_not_noisy_tracebackunrelated to this change).AI usage