Skip to content

Commit 238314d

Browse files
committed
Wrap setting of kernel_id with method that can then be overridden in subclasses.
A recent requirement for Jupyter Enterprise Gateway is for clients to be able to specify the kernel_id for new kernels. Although `jupyter_client.start_kernel()` will honor client-provided kernel_ids, Notebook's override of `start_kernel()` changes the semantics of a non-null kernel_id in the argument list to mean an existing (persisted) kernel should be _started_. As a result, applications that derive from the kernel management infrastructure beyond Notebook cannot influence the derivation of the kernel's id via the existing argument list behavior. By introducing the `determine_kernel_id()` method, subclasses are able to derive the kernel's id however they wish. With the ability to know the kernel's id prior to its invocation, a number of things can be done that wouldn't be possible otherwise. For example, this provides the ability to setup a shared filesystem location possibly pre-populated with data relative to what the request (i.e., kernel) is going to need.
1 parent 4bfb5c4 commit 238314d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

jupyter_client/multikernelmanager.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def start_kernel(self, kernel_name=None, **kwargs):
9090
9191
The kernel ID for the newly started kernel is returned.
9292
"""
93-
kernel_id = kwargs.pop('kernel_id', unicode_type(uuid.uuid4()))
93+
kernel_id = self.determine_kernel_id(**kwargs)
9494
if kernel_id in self:
9595
raise DuplicateKernelError('Kernel already exists: %s' % kernel_id)
9696

@@ -315,3 +315,13 @@ def connect_hb(self, kernel_id, identity=None):
315315
=======
316316
stream : zmq Socket or ZMQStream
317317
"""
318+
319+
def determine_kernel_id(self, **kwargs):
320+
"""
321+
Returns the kernel_id to use for this request. If kernel_id is already in the arguments list,
322+
that value will be used. Otherwise, a newly generated uuid is used. Subclasses may override
323+
this method to substitute other sources of kernel ids.
324+
:param kwargs:
325+
:return: string-ized version 4 uuid
326+
"""
327+
return kwargs.pop('kernel_id', unicode_type(uuid.uuid4()))

0 commit comments

Comments
 (0)