Skip to content

Commit

Permalink
cherry-pick from @minrk work on sharing context by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Jalpan Randeri committed Jun 17, 2020
1 parent f0dbfa9 commit 2d5ba4b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions jupyter_client/multikernelmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ def _create_kernel_manager_factory(self):
kernel_manager_ctor = import_item(self.kernel_manager_class)

def create_kernel_manager(*args, **kwargs):
if self.shared_context:
if self.context.closed:
# recreate context if closed
self.context = self._context_default()
kwargs.setdefault("context", self.context)
km = kernel_manager_ctor(*args, **kwargs)

if km.cache_ports:
Expand Down Expand Up @@ -104,10 +109,28 @@ def _find_available_port(self, ip):

return port

shared_context = Bool(
True,
config=True,
help="Share a single zmq.Context to talk to all my kernels",
)

_created_context = Bool(False)

context = Instance('zmq.Context')

@default("context")
def _context_default(self):
self._created_context = True
return zmq.Context()

def __del__(self):
if self._created_context and self.context and not self.context.closed:
if self.log:
self.log.debug("Destroying zmq context for %s", self)
self.context.destroy()
super().__del__()

connection_dir = Unicode('')

_kernels = Dict()
Expand Down

0 comments on commit 2d5ba4b

Please sign in to comment.