-
Notifications
You must be signed in to change notification settings - Fork 29
feat(debug): make it possible to connect a remote ipython shell #900
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Copyright 2024 Hathor Labs | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from logging import getLogger | ||
from typing import TYPE_CHECKING, Any, Optional | ||
|
||
from ipykernel.kernelapp import IPKernelApp as OriginalIPKernelApp | ||
|
||
if TYPE_CHECKING: | ||
from hathor.manager import HathorManager | ||
|
||
|
||
class IPKernelApp(OriginalIPKernelApp): | ||
def __init__(self, runtime_dir: Optional[str] = None): | ||
msbrogli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
super().__init__() | ||
# https://traitlets.readthedocs.io/en/stable/config-api.html#traitlets.config.Application.logging_config | ||
self.logging_config: dict[str, Any] = {} # empty out logging config | ||
# https://traitlets.readthedocs.io/en/stable/config-api.html#traitlets.config.LoggingConfigurable.log | ||
self.log = getLogger('hathor.ipykernel') # use custom name for the logging adapter | ||
if runtime_dir is not None: | ||
# https://ipykernel.readthedocs.io/en/stable/api/ipykernel.html#ipykernel.kernelapp.IPKernelApp.connection_dir | ||
# https://github.com/ipython/ipykernel/blob/main/ipykernel/kernelapp.py#L301-L320 | ||
# if not defined now, when init_connection_file is called it will be set to 'kernel-<PID>.json', it is | ||
# defined now because it's more convinient to have a fixed path that doesn't depend on the PID of the | ||
# running process, which doesn't benefit us anyway since the data dir | ||
self.connection_dir = runtime_dir | ||
self.connection_file = 'kernel.json' | ||
# https://ipykernel.readthedocs.io/en/stable/api/ipykernel.html#ipykernel.kernelapp.IPKernelApp.no_stderr | ||
self.no_stderr = True # disable forwarding of stderr (because we use it for logging) | ||
|
||
# https://traitlets.readthedocs.io/en/stable/config-api.html#traitlets.config.Application.get_default_logging_config | ||
def get_default_logging_config(self) -> dict[str, Any]: | ||
# XXX: disable original logging setup | ||
return {"version": 1, "disable_existing_loggers": False} | ||
|
||
# https://ipykernel.readthedocs.io/en/stable/api/ipykernel.html#ipykernel.kernelapp.IPKernelApp.init_signal | ||
def init_signal(self) -> None: | ||
# XXX: ignore registering of signals | ||
pass | ||
|
||
# https://ipykernel.readthedocs.io/en/stable/api/ipykernel.html#ipykernel.kernelapp.IPKernelApp.log_connection_info | ||
def log_connection_info(self) -> None: | ||
# XXX: this method is only used to log this info, we can customize it freely | ||
self.log.info(f'ipykernel connection enabled at {self.abs_connection_file}') | ||
|
||
# https://ipykernel.readthedocs.io/en/stable/api/ipykernel.html#ipykernel.kernelapp.IPKernelApp.configure_tornado_logger | ||
def configure_tornado_logger(self) -> None: | ||
# XXX: we already setup tornago logging on hathor.cli.util.setup_logging prevent this class from overriding it | ||
pass | ||
msbrogli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# https://ipykernel.readthedocs.io/en/stable/api/ipykernel.html#ipykernel.kernelapp.IPKernelApp.start | ||
def start(self): | ||
# XXX: custom start to prevent it from running an event loop and capturing KeyboardInterrupt | ||
self.kernel.start() | ||
|
||
|
||
# https://ipykernel.readthedocs.io/en/stable/api/ipykernel.html#ipykernel.embed.embed_kernel | ||
def embed_kernel(manager: 'HathorManager', *, | ||
runtime_dir: Optional[str] = None, extra_ns: dict[str, Any] = {}) -> None: | ||
""" Customized version of ipykernel.embed.embed_kernel that takes parameters specific to this project. | ||
|
||
In theory this method could be called multiple times, like the original ipykernel.embed.embed_kernel. | ||
""" | ||
# get the app if it exists, or set it up if it doesn't | ||
if IPKernelApp.initialized(): | ||
msbrogli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
app = IPKernelApp.instance() | ||
else: | ||
app = IPKernelApp.instance(runtime_dir=runtime_dir) | ||
app.initialize([]) | ||
app.kernel.user_ns = dict(manager=manager) | extra_ns | ||
app.shell.set_completer_frame() | ||
app.start() |
Oops, something went wrong.
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.
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.
Are we changing it in this PR?
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.
No, but in a future PR. On my initial conversation with @msbrogli we thought it would be better to do it in sysctl, allowing it to be turned on/off. However since turning it in/off requires somewhat more careful management of the IPKernelApp, and since there's a dependency on the asyncio-reactor which is easier to make explicit as a CLI parameter. I went with the current implementation as the initial proof of concept of this feature.
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.
Ok! Maybe we should update this comment, then? Just to prevent future confusion