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

Start working on ability to have async_start_kernel in KernelManagers. #4126

Closed
wants to merge 6 commits into from
Closed
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
10 changes: 8 additions & 2 deletions notebook/services/kernels/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ class MainKernelHandler(APIHandler):
@gen.coroutine
def get(self):
km = self.kernel_manager
kernels = yield gen.maybe_future(km.list_kernels())
res = km.list_kernels()
if not isinstance(res, list):
kernels = res
else:
kernels = yield res
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these statements be reversed? Seems like a list is the end goal here and anything but that will likely be a future to yield. I apologize if my understanding is not correct and, if not, perhaps a comment would help.

self.finish(json.dumps(kernels, default=date_default))

@web.authenticated
Expand Down Expand Up @@ -82,7 +86,9 @@ def post(self, kernel_id, action):
if action == 'restart':

try:
yield gen.maybe_future(km.restart_kernel(kernel_id))
res = km.restart_kernel(kernel_id)
if res is not None:
yield from res
except Exception as e:
self.log.error("Exception restarting kernel", exc_info=True)
self.set_status(500)
Expand Down
31 changes: 27 additions & 4 deletions notebook/services/kernels/kernelmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

from notebook.prometheus.metrics import KERNEL_CURRENTLY_RUNNING_TOTAL

import asyncio


class MappingKernelManager(MultiKernelManager):
"""A KernelManager that handles notebook mapping and HTTP error handling"""
Expand Down Expand Up @@ -158,9 +160,21 @@ def start_kernel(self, kernel_id=None, path=None, **kwargs):
if kernel_id is None:
if path is not None:
kwargs['cwd'] = self.cwd_for_path(path)
kernel_id = yield gen.maybe_future(
super(MappingKernelManager, self).start_kernel(**kwargs)
)

sup = super(MappingKernelManager, self)
async_sk = getattr(sup, 'start_kernel_async', None)
if async_sk is not None:
self.log.debug('dispathcing start to async super')
res = super().start_kernel_async(**kwargs)
else:
self.log.debug('dispathcing start to sync super')
res = super().start_kernel(**kwargs)

if isinstance(res, str):
kernel_id = res
else:
kernel_id = yield res

self._kernel_connections[kernel_id] = 0
self.start_watching_activity(kernel_id)
self.log.info("Kernel started: %s" % kernel_id)
Expand Down Expand Up @@ -296,10 +310,19 @@ def shutdown_kernel(self, kernel_id, now=False):

return super(MappingKernelManager, self).shutdown_kernel(kernel_id, now=now)

@asyncio.coroutine
def restart_kernel(self, kernel_id):
"""Restart a kernel by kernel_id"""
self._check_kernel_id(kernel_id)
super(MappingKernelManager, self).restart_kernel(kernel_id)
sup = super(MappingKernelManager, self)

if hasattr(sup, 'restart_kernel_async'):
self.log.debug('dispatching restart_kernel to async super')
yield from sup.restart_kernel_async(kernel_id)
else:
self.log.debug('dispatching restart_kernel to sync super')
sup.restart_kernel(kernel_id)

kernel = self.get_kernel(kernel_id)
# return a Future that will resolve when the kernel has successfully restarted
channel = kernel.connect_shell()
Expand Down