Defer debugpy and psutil imports until they are actually needed - #1543
Draft
Carreau wants to merge 1 commit into
Draft
Defer debugpy and psutil imports until they are actually needed#1543Carreau wants to merge 1 commit into
Carreau wants to merge 1 commit into
Conversation
Both imports were paid for on every kernel startup even though most sessions never debug or ask for usage information. * `IPythonKernel.debugger` is now a lazily-created property; the debugger (and the debugpy import) is only built on the first debug request. `poll_stopped_queue` is scheduled at that point rather than in `start()`. * `debugger_class` was a `Type` trait, which traitlets resolves — and therefore imports — as soon as the kernel is instantiated. It is replaced by a plain `debugger_class_name` string; `debugger_class` remains as a deprecated property, and subclasses still overriding it keep working with a DeprecationWarning. * `psutil` is imported through `_get_psutil()`, which caches the (possibly None) result on first use. On a local test (where I have optimisation in IPython and traitlets as well), this brings the startup time from 220ms to 170ms
Carreau
force-pushed
the
lazy-debug-and-psutil
branch
from
August 4, 2026 16:44
722618c to
ac6ca05
Compare
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.
Both imports were paid for on every kernel startup even though most sessions never debug or ask for usage information.
IPythonKernel.debuggeris now a lazily-created property; the debugger (and the debugpy import) is only built on the first debug request.poll_stopped_queueis scheduled at that point rather than instart().debugger_classwas aTypetrait, which traitlets resolves — and therefore imports — as soon as the kernel is instantiated. It is replaced by a plaindebugger_class_namestring;debugger_classremains as a deprecated property, and subclasses still overriding it keep working with a DeprecationWarning.psutilis imported through_get_psutil(), which caches the (possibly None) result on first use.On a local test (where I have optimisation in IPython and traitlets as well), this brings the startup time from 220ms to 170ms