Skip to content

Commit 5103959

Browse files
committed
Changes necessary to get async kernel startup working
Inspired by #402, but needing support for python 2.7, these changes essentially apply the same model used in Notebook for supporting coroutines with appropriately placed yield statements in order to start (and restart) multiple kernels simultaneously within the same server instance.
1 parent e813086 commit 5103959

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

jupyter_client/manager.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import time
1414

1515
import zmq
16-
16+
from tornado import gen
1717
from ipython_genutils.importstring import import_item
1818
from .localinterfaces import is_local_ip, local_ips
1919
from traitlets import (
@@ -205,6 +205,7 @@ def _close_control_socket(self):
205205
self._control_socket.close()
206206
self._control_socket = None
207207

208+
@gen.coroutine
208209
def start_kernel(self, **kw):
209210
"""Starts a kernel on this host in a separate process.
210211
@@ -246,8 +247,7 @@ def start_kernel(self, **kw):
246247

247248
# launch the kernel subprocess
248249
self.log.debug("Starting kernel: %s", kernel_cmd)
249-
self.kernel = self._launch_kernel(kernel_cmd, env=env,
250-
**kw)
250+
self.kernel = yield gen.maybe_future(self._launch_kernel(kernel_cmd, env=env, **kw))
251251
self.start_restarter()
252252
self._connect_control_socket()
253253

@@ -324,6 +324,7 @@ def shutdown_kernel(self, now=False, restart=False):
324324

325325
self.cleanup(connection_file=not restart)
326326

327+
@gen.coroutine
327328
def restart_kernel(self, now=False, newports=False, **kw):
328329
"""Restarts a kernel with the arguments that were used to launch it.
329330
@@ -361,7 +362,7 @@ def restart_kernel(self, now=False, newports=False, **kw):
361362

362363
# Start new kernel.
363364
self._launch_args.update(kw)
364-
self.start_kernel(**self._launch_args)
365+
yield gen.maybe_future(self.start_kernel(**self._launch_args))
365366

366367
@property
367368
def has_kernel(self):

jupyter_client/multikernelmanager.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
import os
99
import uuid
10-
1110
import zmq
1211

12+
from tornado import gen
1313
from traitlets.config.configurable import LoggingConfigurable
1414
from ipython_genutils.importstring import import_item
1515
from traitlets import (
@@ -82,6 +82,7 @@ def __len__(self):
8282
def __contains__(self, kernel_id):
8383
return kernel_id in self._kernels
8484

85+
@gen.coroutine
8586
def start_kernel(self, kernel_name=None, **kwargs):
8687
"""Start a new kernel.
8788
@@ -107,9 +108,9 @@ def start_kernel(self, kernel_name=None, **kwargs):
107108
parent=self, log=self.log, kernel_name=kernel_name,
108109
**constructor_kwargs
109110
)
110-
km.start_kernel(**kwargs)
111+
yield gen.maybe_future(km.start_kernel(**kwargs))
111112
self._kernels[kernel_id] = km
112-
return kernel_id
113+
raise gen.Return(kernel_id)
113114

114115
@kernel_method
115116
def shutdown_kernel(self, kernel_id, now=False, restart=False):
@@ -186,15 +187,25 @@ def signal_kernel(self, kernel_id, signum):
186187
"""
187188
self.log.info("Signaled Kernel %s with %s" % (kernel_id, signum))
188189

189-
@kernel_method
190+
@gen.coroutine
190191
def restart_kernel(self, kernel_id, now=False):
191192
"""Restart a kernel by its uuid, keeping the same ports.
192193
193194
Parameters
194195
==========
195196
kernel_id : uuid
196-
The id of the kernel to interrupt.
197+
The id of the kernel to restart.
198+
199+
now : bool, optional
200+
If True, the kernel is forcefully restarted *immediately*, without
201+
having a chance to do any cleanup action. Otherwise the kernel is
202+
given 1s to clean up before a forceful restart is issued.
203+
204+
In all cases the kernel is restarted, the only difference is whether
205+
it is given a chance to perform a clean shutdown or not.
197206
"""
207+
km = self.get_kernel(kernel_id)
208+
yield gen.maybe_future(km.restart_kernel(now))
198209
self.log.info("Kernel restarted: %s" % kernel_id)
199210

200211
@kernel_method

0 commit comments

Comments
 (0)