Skip to content

Commit

Permalink
Merge pull request #492 from martinRenou/cache_ports
Browse files Browse the repository at this point in the history
Only cache ports if the cache_ports flag is set to True
  • Loading branch information
SylvainCorlay authored Oct 17, 2019
2 parents f6ceeaa + 54a6be5 commit fb91900
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
8 changes: 7 additions & 1 deletion jupyter_client/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .localinterfaces import is_local_ip, local_ips
from traitlets import (
Any, Float, Instance, Unicode, List, Bool, Type, DottedObjectName, Dict,
observe
default, observe
)
from jupyter_client import (
launch_kernel,
Expand Down Expand Up @@ -92,6 +92,12 @@ def kernel_spec(self):
help="""Extra environment variables to be set for the kernel."""
)

cache_ports = Bool(help='True if the MultiKernelManager should cache ports for this KernelManager instance')

@default('cache_ports')
def _default_cache_ports(self):
return self.transport == 'tcp'

@property
def ipykernel(self):
return self.kernel_name in {'python', 'python2', 'python3'}
Expand Down
14 changes: 7 additions & 7 deletions jupyter_client/multikernelmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from traitlets.config.configurable import LoggingConfigurable
from ipython_genutils.importstring import import_item
from traitlets import (
Instance, Dict, Unicode, Any, DottedObjectName, observe, default
Any, Bool, Dict, DottedObjectName, Instance, Unicode, default, observe
)
from ipython_genutils.py3compat import unicode_type

Expand Down Expand Up @@ -78,7 +78,7 @@ def _create_kernel_manager_factory(self):
def create_kernel_manager(*args, **kwargs):
km = kernel_manager_ctor(*args, **kwargs)

if km.transport == 'tcp':
if km.cache_ports:
km.shell_port = self._find_available_port(km.ip)
km.iopub_port = self._find_available_port(km.ip)
km.stdin_port = self._find_available_port(km.ip)
Expand Down Expand Up @@ -168,17 +168,17 @@ def shutdown_kernel(self, kernel_id, now=False, restart=False):
"""
self.log.info("Kernel shutdown: %s" % kernel_id)

kernel = self.get_kernel(kernel_id)
km = self.get_kernel(kernel_id)

ports = (
kernel.shell_port, kernel.iopub_port, kernel.stdin_port,
kernel.hb_port, kernel.control_port
km.shell_port, km.iopub_port, km.stdin_port,
km.hb_port, km.control_port
)

kernel.shutdown_kernel(now=now, restart=restart)
km.shutdown_kernel(now=now, restart=restart)
self.remove_kernel(kernel_id)

if not restart and kernel.transport == 'tcp':
if km.cache_ports and not restart:
for port in ports:
self.currently_used_ports.remove(port)

Expand Down

0 comments on commit fb91900

Please sign in to comment.