Merge pull request #2854 from minrk/2853b

Move kernel code into IPython.kernel

inprocess Kernel in kernel.inprocess
zmq Kernel in kernel.zmq
KernelManager stuff and ulities in top-level kernel

Main functional change: allow custom kernel Popen command

- [x] adds `KernelManager.kernel_cmd` configurable for launching a custom kernel
- [x] splits entry_point.base_launch_kernel into two steps: making the launch cmd and launching the subprocess
- [x] figure out where the entry_point functions belong, if it should be anywhere else
- [x] move IPython.zmq.kernelmanagerabc to IPython.kernel.kernelmanagerabc
- [x] move IPython.lib.kernel/IPython/zmq.entry_point to IPython.kernel.launcher / connect
- [x] move zmq.ipkernelapp.IPKernelApp to zmq.kernelapp  (I'll look at merging the classes, and see if it makes 
- [x] move IPython.zmq to IPython.kernel.zmq
- [x] move IPython.inprocess to IPython.kernel.inprocess
- [x] move embed_kernel from zmq.ipkernelapp to zmq.embed
- [x] move MultiKernelManager to IPython.kernel.multikernelmanager.
- [x] move IPython.zmq.blockingkernelmanager to IPython.kernel.blockingkernelmanager.
- [x] move IPython.zmq.kernelmanager to IPython.kernel.kernelmanager.
- [x] move IPython.ipkernel.Kernel to IPython.kernel.kernel.
pull/37/head
Min RK 13 years ago
commit fcb9430912

@ -36,7 +36,7 @@ from zmq.eventloop import ioloop
from zmq.utils import jsonapi
from IPython.external.decorator import decorator
from IPython.zmq.session import Session
from IPython.kernel.zmq.session import Session
from IPython.lib.security import passwd_check
from IPython.utils.jsonutil import date_default
from IPython.utils.path import filefind

@ -1,4 +1,4 @@
"""A kernel manager for multiple kernels.
"""A kernel manager relating notebooks and kernels
Authors:
@ -6,7 +6,7 @@ Authors:
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The IPython Development Team
# Copyright (C) 2013 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
@ -16,247 +16,16 @@ Authors:
# Imports
#-----------------------------------------------------------------------------
import os
import uuid
import zmq
from zmq.eventloop.zmqstream import ZMQStream
from tornado import web
from IPython.config.configurable import LoggingConfigurable
from IPython.utils.importstring import import_item
from IPython.kernel.multikernelmanager import MultiKernelManager
from IPython.utils.traitlets import (
Instance, Dict, List, Unicode, Float, Integer, Any, DottedObjectName,
Dict, List, Unicode, Float, Integer,
)
#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
class DuplicateKernelError(Exception):
pass
class MultiKernelManager(LoggingConfigurable):
"""A class for managing multiple kernels."""
kernel_manager_class = DottedObjectName(
"IPython.zmq.blockingkernelmanager.BlockingKernelManager", config=True,
help="""The kernel manager class. This is configurable to allow
subclassing of the KernelManager for customized behavior.
"""
)
def _kernel_manager_class_changed(self, name, old, new):
self.kernel_manager_factory = import_item(new)
kernel_manager_factory = Any(help="this is kernel_manager_class after import")
def _kernel_manager_factory_default(self):
return import_item(self.kernel_manager_class)
context = Instance('zmq.Context')
def _context_default(self):
return zmq.Context.instance()
connection_dir = Unicode('')
_kernels = Dict()
def list_kernel_ids(self):
"""Return a list of the kernel ids of the active kernels."""
# Create a copy so we can iterate over kernels in operations
# that delete keys.
return list(self._kernels.keys())
def __len__(self):
"""Return the number of running kernels."""
return len(self.list_kernel_ids())
def __contains__(self, kernel_id):
return kernel_id in self._kernels
def start_kernel(self, **kwargs):
"""Start a new kernel.
The caller can pick a kernel_id by passing one in as a keyword arg,
otherwise one will be picked using a uuid.
To silence the kernel's stdout/stderr, call this using::
km.start_kernel(stdout=PIPE, stderr=PIPE)
"""
kernel_id = kwargs.pop('kernel_id', unicode(uuid.uuid4()))
if kernel_id in self:
raise DuplicateKernelError('Kernel already exists: %s' % kernel_id)
# kernel_manager_factory is the constructor for the KernelManager
# subclass we are using. It can be configured as any Configurable,
# including things like its transport and ip.
km = self.kernel_manager_factory(connection_file=os.path.join(
self.connection_dir, "kernel-%s.json" % kernel_id),
config=self.config,
)
km.start_kernel(**kwargs)
# start just the shell channel, needed for graceful restart
km.start_channels(shell=True, iopub=False, stdin=False, hb=False)
self._kernels[kernel_id] = km
return kernel_id
def shutdown_kernel(self, kernel_id, now=False):
"""Shutdown a kernel by its kernel uuid.
Parameters
==========
kernel_id : uuid
The id of the kernel to shutdown.
now : bool
Should the kernel be shutdown forcibly using a signal.
"""
k = self.get_kernel(kernel_id)
k.shutdown_kernel(now=now)
k.shell_channel.stop()
del self._kernels[kernel_id]
def shutdown_all(self, now=False):
"""Shutdown all kernels."""
for kid in self.list_kernel_ids():
self.shutdown_kernel(kid, now=now)
def interrupt_kernel(self, kernel_id):
"""Interrupt (SIGINT) the kernel by its uuid.
Parameters
==========
kernel_id : uuid
The id of the kernel to interrupt.
"""
return self.get_kernel(kernel_id).interrupt_kernel()
def signal_kernel(self, kernel_id, signum):
"""Sends a signal to the kernel by its uuid.
Note that since only SIGTERM is supported on Windows, this function
is only useful on Unix systems.
Parameters
==========
kernel_id : uuid
The id of the kernel to signal.
"""
return self.get_kernel(kernel_id).signal_kernel(signum)
def restart_kernel(self, kernel_id):
"""Restart a kernel by its uuid, keeping the same ports.
Parameters
==========
kernel_id : uuid
The id of the kernel to interrupt.
"""
return self.get_kernel(kernel_id).restart_kernel()
def get_kernel(self, kernel_id):
"""Get the single KernelManager object for a kernel by its uuid.
Parameters
==========
kernel_id : uuid
The id of the kernel.
"""
km = self._kernels.get(kernel_id)
if km is not None:
return km
else:
raise KeyError("Kernel with id not found: %s" % kernel_id)
def get_connection_info(self, kernel_id):
"""Return a dictionary of connection data for a kernel.
Parameters
==========
kernel_id : uuid
The id of the kernel.
Returns
=======
connection_dict : dict
A dict of the information needed to connect to a kernel.
This includes the ip address and the integer port
numbers of the different channels (stdin_port, iopub_port,
shell_port, hb_port).
"""
km = self.get_kernel(kernel_id)
return dict(transport=km.transport,
ip=km.ip,
shell_port=km.shell_port,
iopub_port=km.iopub_port,
stdin_port=km.stdin_port,
hb_port=km.hb_port,
)
def _make_url(self, transport, ip, port):
"""Make a ZeroMQ URL for a given transport, ip and port."""
if transport == 'tcp':
return "tcp://%s:%i" % (ip, port)
else:
return "%s://%s-%s" % (transport, ip, port)
def _create_connected_stream(self, kernel_id, socket_type, channel):
"""Create a connected ZMQStream for a kernel."""
cinfo = self.get_connection_info(kernel_id)
url = self._make_url(cinfo['transport'], cinfo['ip'],
cinfo['%s_port' % channel]
)
sock = self.context.socket(socket_type)
self.log.info("Connecting to: %s" % url)
sock.connect(url)
return ZMQStream(sock)
def create_iopub_stream(self, kernel_id):
"""Return a ZMQStream object connected to the iopub channel.
Parameters
==========
kernel_id : uuid
The id of the kernel.
Returns
=======
stream : ZMQStream
"""
iopub_stream = self._create_connected_stream(kernel_id, zmq.SUB, 'iopub')
iopub_stream.socket.setsockopt(zmq.SUBSCRIBE, b'')
return iopub_stream
def create_shell_stream(self, kernel_id):
"""Return a ZMQStream object connected to the shell channel.
Parameters
==========
kernel_id : uuid
The id of the kernel.
Returns
=======
stream : ZMQStream
"""
shell_stream = self._create_connected_stream(kernel_id, zmq.DEALER, 'shell')
return shell_stream
def create_hb_stream(self, kernel_id):
"""Return a ZMQStream object connected to the hb channel.
Parameters
==========
kernel_id : uuid
The id of the kernel.
Returns
=======
stream : ZMQStream
"""
hb_stream = self._create_connected_stream(kernel_id, zmq.REQ, 'hb')
return hb_stream
class MappingKernelManager(MultiKernelManager):
"""A KernelManager that handles notebok mapping and HTTP error handling"""

@ -61,12 +61,12 @@ from IPython.config.application import catch_config_error, boolean_flag
from IPython.core.application import BaseIPythonApplication
from IPython.core.profiledir import ProfileDir
from IPython.frontend.consoleapp import IPythonConsoleApp
from IPython.lib.kernel import swallow_argv
from IPython.zmq.session import Session, default_secure
from IPython.zmq.zmqshell import ZMQInteractiveShell
from IPython.zmq.ipkernel import (
flags as ipkernel_flags,
aliases as ipkernel_aliases,
from IPython.kernel import swallow_argv
from IPython.kernel.zmq.session import Session, default_secure
from IPython.kernel.zmq.zmqshell import ZMQInteractiveShell
from IPython.kernel.zmq.kernelapp import (
kernel_flags,
kernel_aliases,
IPKernelApp
)
from IPython.utils.importstring import import_item
@ -195,7 +195,7 @@ class NotebookWebApplication(web.Application):
# Aliases and Flags
#-----------------------------------------------------------------------------
flags = dict(ipkernel_flags)
flags = dict(kernel_flags)
flags['no-browser']=(
{'NotebookApp' : {'open_browser' : False}},
"Don't open the notebook in a browser after startup."
@ -234,7 +234,7 @@ flags.update(boolean_flag('script', 'FileNotebookManager.save_script',
# or it will raise an error on unrecognized flags
notebook_flags = ['no-browser', 'no-mathjax', 'read-only', 'script', 'no-script']
aliases = dict(ipkernel_aliases)
aliases = dict(kernel_aliases)
aliases.update({
'ip': 'NotebookApp.ip',

@ -238,6 +238,9 @@ def make_exclude():
if not have['wx']:
exclusions.append(ipjoin('lib', 'inputhookwx'))
if 'IPython.kernel.inprocess' not in sys.argv:
exclusions.append(ipjoin('kernel', 'inprocess'))
# FIXME: temporarily disable autoreload tests, as they can produce
# spurious failures in subsequent tests (cythonmagic).
exclusions.append(ipjoin('extensions', 'autoreload'))
@ -246,7 +249,7 @@ def make_exclude():
# We do this unconditionally, so that the test suite doesn't import
# gtk, changing the default encoding and masking some unicode bugs.
exclusions.append(ipjoin('lib', 'inputhookgtk'))
exclusions.append(ipjoin('zmq', 'gui', 'gtkembed'))
exclusions.append(ipjoin('kernel', 'zmq', 'gui', 'gtkembed'))
# These have to be skipped on win32 because the use echo, rm, cd, etc.
# See ticket https://github.com/ipython/ipython/issues/87
@ -261,7 +264,7 @@ def make_exclude():
])
if not have['zmq']:
exclusions.append(ipjoin('zmq'))
exclusions.append(ipjoin('kernel'))
exclusions.append(ipjoin('frontend', 'qt'))
exclusions.append(ipjoin('frontend', 'html'))
exclusions.append(ipjoin('frontend', 'consoleapp.py'))
@ -277,7 +280,7 @@ def make_exclude():
if not have['matplotlib']:
exclusions.extend([ipjoin('core', 'pylabtools'),
ipjoin('core', 'tests', 'test_pylabtools'),
ipjoin('zmq', 'pylab'),
ipjoin('kernel', 'zmq', 'pylab'),
])
if not have['cython']:
@ -431,10 +434,11 @@ def make_runners(inc_slow=False):
# Packages to be tested via nose, that only depend on the stdlib
nose_pkg_names = ['config', 'core', 'extensions', 'frontend', 'lib',
'testing', 'utils', 'nbformat', 'inprocess' ]
'testing', 'utils', 'nbformat' ]
if have['zmq']:
nose_pkg_names.append('zmq')
nose_pkg_names.append('kernel')
nose_pkg_names.append('kernel.inprocess')
if inc_slow:
nose_pkg_names.append('parallel')
@ -502,7 +506,7 @@ def run_iptest():
# assumptions about what needs to be a singleton and what doesn't (app
# objects should, individual shells shouldn't). But for now, this
# workaround allows the test suite for the inprocess module to complete.
if not 'IPython.inprocess' in sys.argv:
if not 'IPython.kernel.inprocess' in sys.argv:
globalipapp.start_ipython()
# Now nose can run

Loading…
Cancel
Save