Skip to content

Commit ab25bd6

Browse files
committed
Refactor BlockingKernelManager/AsyncKernelManager
1 parent b4d35ee commit ab25bd6

9 files changed

+160
-374
lines changed

jupyter_client/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .connect import *
55
from .launcher import *
66
from .client import KernelClient
7-
from .manager import KernelManager, AsyncKernelManager, run_kernel
7+
from .manager import KernelManager, BlockingKernelManager, AsyncKernelManager, run_kernel
88
from .blocking import BlockingKernelClient
99
from .asynchronous import AsyncKernelClient
1010
from .multikernelmanager import MultiKernelManager, AsyncMultiKernelManager

jupyter_client/client.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
# Copyright (c) Jupyter Development Team.
44
# Distributed under the terms of the Modified BSD License.
55

6+
import typing as t
7+
68
from jupyter_client.channels import major_protocol_version
79

810
import zmq
911

10-
from traitlets import (
12+
from traitlets import ( # type: ignore
1113
Any, Instance, Type,
1214
)
1315

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

22-
def validate_string_dict(dct):
24+
def validate_string_dict(
25+
dct: t.Dict[str, str]
26+
) -> None:
2327
"""Validate that the input is a dict with string keys and values.
2428
2529
Raises ValueError if not."""
26-
for k,v in dct.items():
30+
for k, v in dct.items():
2731
if not isinstance(k, str):
2832
raise ValueError('key %r in dict must be a string' % k)
2933
if not isinstance(v, str):
@@ -49,7 +53,7 @@ class KernelClient(ConnectionFileMixin):
4953

5054
# The PyZMQ Context to use for communication with the kernel.
5155
context = Instance(zmq.Context)
52-
def _context_default(self):
56+
def _context_default(self) -> zmq.Context:
5357
return zmq.Context()
5458

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

6973
# flag for whether execute requests should be allowed to call raw_input:
70-
allow_stdin = True
74+
allow_stdin: bool = True
7175

7276
#--------------------------------------------------------------------------
7377
# Channel proxy methods
7478
#--------------------------------------------------------------------------
7579

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

jupyter_client/consoleapp.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from .blocking import BlockingKernelClient
2727
from .restarter import KernelRestarter
28-
from . import KernelManager, tunnel_to_kernel, find_connection_file, connect
28+
from . import BlockingKernelManager, tunnel_to_kernel, find_connection_file, connect
2929
from .kernelspec import NoSuchKernel
3030
from .session import Session
3131

@@ -86,7 +86,7 @@
8686
# Classes
8787
#-----------------------------------------------------------------------------
8888

89-
classes = [KernelManager, KernelRestarter, Session]
89+
classes = [BlockingKernelManager, KernelRestarter, Session]
9090

9191
class JupyterConsoleApp(ConnectionFileMixin):
9292
name = 'jupyter-console-mixin'
@@ -112,7 +112,7 @@ class JupyterConsoleApp(ConnectionFileMixin):
112112
flags = Dict(flags)
113113
aliases = Dict(aliases)
114114
kernel_manager_class = Type(
115-
default_value=KernelManager,
115+
default_value=BlockingKernelManager,
116116
config=True,
117117
help='The kernel manager class to use.'
118118
)

jupyter_client/kernelapp.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
from . import __version__
1010
from .kernelspec import KernelSpecManager, NATIVE_KERNEL_NAME
11-
from .manager import KernelManager
11+
from .manager import BlockingKernelManager
1212

1313
class KernelApp(JupyterApp):
1414
"""Launch a kernel by name in a local subprocess.
1515
"""
1616
version = __version__
1717
description = "Run a kernel locally in a subprocess"
1818

19-
classes = [KernelManager, KernelSpecManager]
19+
classes = [BlockingKernelManager, KernelSpecManager]
2020

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

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

3939
self.loop = IOLoop.current()
4040
self.loop.add_callback(self._record_started)

0 commit comments

Comments
 (0)