Skip to content
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

Introduce ServerKernelManager class #1101

Merged
merged 5 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions jupyter_server/services/kernels/kernelmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
# Distributed under the terms of the Modified BSD License.
import asyncio
import os
import warnings
from collections import defaultdict
from datetime import datetime, timedelta
from functools import partial

from jupyter_client.ioloop.manager import AsyncIOLoopKernelManager
from jupyter_client.multikernelmanager import (
AsyncMultiKernelManager,
MultiKernelManager,
Expand All @@ -36,7 +38,7 @@

from jupyter_server._tz import isoformat, utcnow
from jupyter_server.prometheus.metrics import KERNEL_CURRENTLY_RUNNING_TOTAL
from jupyter_server.utils import ensure_async, to_os_path
from jupyter_server.utils import ensure_async, import_item, to_os_path


class MappingKernelManager(MultiKernelManager):
Expand Down Expand Up @@ -656,10 +658,34 @@ async def cull_kernel_if_idle(self, kernel_id):
class AsyncMappingKernelManager(MappingKernelManager, AsyncMultiKernelManager):
@default("kernel_manager_class")
def _default_kernel_manager_class(self):
return "jupyter_client.ioloop.AsyncIOLoopKernelManager"
return "jupyter_server.services.kernels.kernelmanager.ServerKernelManager"

@validate("kernel_manager_class")
def _validate_kernel_manager_class(self, proposal):
km_class_value = proposal.value
km_class = import_item(km_class_value)
if not issubclass(km_class, ServerKernelManager):
warnings.warn(
f"KernelManager class '{km_class}' is not a subclass of 'ServerKernelManager'. Custom "
"KernelManager classes should derive from 'ServerKernelManager' beginning with jupyter-server 2.0 "
"or risk missing functionality. Continuing...",
FutureWarning,
stacklevel=3,
)
return km_class_value

def __init__(self, **kwargs):
self.pinned_superclass = MultiKernelManager
self._pending_kernel_tasks = {}
self.pinned_superclass.__init__(self, **kwargs)
self.last_kernel_activity = utcnow()


class ServerKernelManager(AsyncIOLoopKernelManager):

# Define activity-related attributes:
execution_state = Unicode(
None, allow_none=True, help="The current execution state of the kernel"
)
reason = Unicode("", help="The reason for the last failure against the kernel")
last_activity = Instance(datetime, help="The last activity on the kernel")
8 changes: 8 additions & 0 deletions tests/services/kernels/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ def test_async_kernel_manager(jp_configurable_serverapp):
]
app = jp_configurable_serverapp(argv=argv)
assert isinstance(app.kernel_manager, AsyncMappingKernelManager)


def test_not_server_kernel_manager(jp_configurable_serverapp):
argv = [
"--AsyncMappingKernelManager.kernel_manager_class=jupyter_client.ioloop.manager.AsyncIOLoopKernelManager"
]
with pytest.warns(FutureWarning, match="is not a subclass of 'ServerKernelManager'"):
jp_configurable_serverapp(argv=argv)