Skip to content

Commit

Permalink
Refactor BlockingKernelManager/AsyncKernelManager
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Mar 11, 2021
1 parent b4d35ee commit 38168da
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 371 deletions.
2 changes: 1 addition & 1 deletion jupyter_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .connect import *
from .launcher import *
from .client import KernelClient
from .manager import KernelManager, AsyncKernelManager, run_kernel
from .manager import KernelManager, BlockingKernelManager, AsyncKernelManager, run_kernel
from .blocking import BlockingKernelClient
from .asynchronous import AsyncKernelClient
from .multikernelmanager import MultiKernelManager, AsyncMultiKernelManager
16 changes: 10 additions & 6 deletions jupyter_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

import typing as t

from jupyter_client.channels import major_protocol_version

import zmq

from traitlets import (
from traitlets import ( # type: ignore
Any, Instance, Type,
)

Expand All @@ -19,11 +21,13 @@
# some utilities to validate message structure, these might get moved elsewhere
# if they prove to have more generic utility

def validate_string_dict(dct):
def validate_string_dict(
dct: t.Dict[str, str]
) -> None:
"""Validate that the input is a dict with string keys and values.
Raises ValueError if not."""
for k,v in dct.items():
for k, v in dct.items():
if not isinstance(k, str):
raise ValueError('key %r in dict must be a string' % k)
if not isinstance(v, str):
Expand All @@ -49,7 +53,7 @@ class KernelClient(ConnectionFileMixin):

# The PyZMQ Context to use for communication with the kernel.
context = Instance(zmq.Context)
def _context_default(self):
def _context_default(self) -> zmq.Context:
return zmq.Context()

# The classes to use for the various channels
Expand All @@ -67,13 +71,13 @@ def _context_default(self):
_control_channel = Any()

# flag for whether execute requests should be allowed to call raw_input:
allow_stdin = True
allow_stdin: bool = True

#--------------------------------------------------------------------------
# Channel proxy methods
#--------------------------------------------------------------------------

def get_shell_msg(self, *args, **kwargs):
def get_shell_msg(self, *args, **kwargs) -> None:
"""Get a message from the shell channel"""
return self.shell_channel.get_msg(*args, **kwargs)

Expand Down
6 changes: 3 additions & 3 deletions jupyter_client/consoleapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from .blocking import BlockingKernelClient
from .restarter import KernelRestarter
from . import KernelManager, tunnel_to_kernel, find_connection_file, connect
from . import BlockingKernelManager, tunnel_to_kernel, find_connection_file, connect
from .kernelspec import NoSuchKernel
from .session import Session

Expand Down Expand Up @@ -86,7 +86,7 @@
# Classes
#-----------------------------------------------------------------------------

classes = [KernelManager, KernelRestarter, Session]
classes = [BlockingKernelManager, KernelRestarter, Session]

class JupyterConsoleApp(ConnectionFileMixin):
name = 'jupyter-console-mixin'
Expand All @@ -112,7 +112,7 @@ class JupyterConsoleApp(ConnectionFileMixin):
flags = Dict(flags)
aliases = Dict(aliases)
kernel_manager_class = Type(
default_value=KernelManager,
default_value=BlockingKernelManager,
config=True,
help='The kernel manager class to use.'
)
Expand Down
8 changes: 4 additions & 4 deletions jupyter_client/kernelapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

from . import __version__
from .kernelspec import KernelSpecManager, NATIVE_KERNEL_NAME
from .manager import KernelManager
from .manager import BlockingKernelManager

class KernelApp(JupyterApp):
"""Launch a kernel by name in a local subprocess.
"""
version = __version__
description = "Run a kernel locally in a subprocess"

classes = [KernelManager, KernelSpecManager]
classes = [BlockingKernelManager, KernelSpecManager]

aliases = {
'kernel': 'KernelApp.kernel_name',
Expand All @@ -33,8 +33,8 @@ def initialize(self, argv=None):

cf_basename = 'kernel-%s.json' % uuid.uuid4()
self.config.setdefault('KernelManager', {}).setdefault('connection_file', os.path.join(self.runtime_dir, cf_basename))
self.km = KernelManager(kernel_name=self.kernel_name,
config=self.config)
self.km = BlockingKernelManager(kernel_name=self.kernel_name,
config=self.config)

self.loop = IOLoop.current()
self.loop.add_callback(self._record_started)
Expand Down
Loading

0 comments on commit 38168da

Please sign in to comment.