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

Only cache ports if the cache_ports flag is set to True #492

Merged
merged 1 commit into from
Oct 17, 2019
Merged
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
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