IPython Documentation

Table Of Contents

Previous topic

Module: kernel.blocking.client

Next topic

Module: kernel.channelsabc

This Page

Note

This documentation is for a development version of IPython. There may be significant differences from the latest stable release.

Module: kernel.channels

Base classes to manage a Client’s interaction with a running kernel

6 Classes

class IPython.kernel.channels.InvalidPortNumber

Bases: exceptions.Exception

class IPython.kernel.channels.ZMQSocketChannel(context, session, address)

Bases: threading.Thread

The base class for the channels that use ZMQ sockets.

__init__(context, session, address)

Create a channel.

Parameters:

context : zmq.Context

The ZMQ context to use.

session : session.Session

The session to use.

address : zmq url

Standard (ip, port) tuple that the kernel is listening on.

address

Get the channel’s address as a zmq url string.

These URLS have the form: ‘tcp://127.0.0.1:5555‘.

stop()

Stop the channel’s event loop and join its thread.

This calls join() and returns when the thread terminates. RuntimeError will be raised if start() is called again.

class IPython.kernel.channels.ShellChannel(context, session, address)

Bases: IPython.kernel.channels.ZMQSocketChannel

The shell channel for issuing request/replies to the kernel.

__init__(context, session, address)
call_handlers(msg)

This method is called in the ioloop thread when a message arrives.

Subclasses should override this method to handle incoming messages. It is important to remember that this method is called in the thread so that some logic must be done to ensure that the application level handlers are called in the application thread.

complete(code, cursor_pos=None)

Tab complete text in the kernel’s namespace.

Parameters:

code : str

The context in which completion is requested. Can be anything between a variable name and an entire cell.

cursor_pos : int, optional

The position of the cursor in the block of code where the completion was requested. Default: len(code)

Returns:

The msg_id of the message sent. :

execute(code, silent=False, store_history=True, user_expressions=None, allow_stdin=None)

Execute code in the kernel.

Parameters:

code : str

A string of Python code.

silent : bool, optional (default False)

If set, the kernel will execute the code as quietly possible, and will force store_history to be False.

store_history : bool, optional (default True)

If set, the kernel will store command history. This is forced to be False if silent is True.

user_expressions : dict, optional

A dict mapping names to expressions to be evaluated in the user’s dict. The expression values are returned as strings formatted using repr().

allow_stdin : bool, optional (default self.allow_stdin)

Flag for whether the kernel can send stdin requests to frontends.

Some frontends (e.g. the Notebook) do not support stdin requests. If raw_input is called from code executed from such a frontend, a StdinNotImplementedError will be raised.

Returns:

The msg_id of the message sent. :

history(raw=True, output=False, hist_access_type='range', **kwargs)

Get entries from the kernel’s history list.

Parameters:

raw : bool

If True, return the raw input.

output : bool

If True, then return the output as well.

hist_access_type : str

‘range’ (fill in session, start and stop params), ‘tail’ (fill in n)

or ‘search’ (fill in pattern param).

session : int

For a range request, the session from which to get lines. Session numbers are positive integers; negative ones count back from the current session.

start : int

The first line number of a history range.

stop : int

The final (excluded) line number of a history range.

n : int

The number of lines of history to get for a tail request.

pattern : str

The glob-syntax pattern for a search request.

Returns:

The msg_id of the message sent. :

inspect(code, cursor_pos=None, detail_level=0)

Get metadata information about an object in the kernel’s namespace.

It is up to the kernel to determine the appropriate object to inspect.

Parameters:

code : str

The context in which info is requested. Can be anything between a variable name and an entire cell.

cursor_pos : int, optional

The position of the cursor in the block of code where the info was requested. Default: len(code)

detail_level : int, optional

The level of detail for the introspection (0-2)

Returns:

The msg_id of the message sent. :

kernel_info()

Request kernel info.

run()

The thread’s main activity. Call start() instead.

shutdown(restart=False)

Request an immediate kernel shutdown.

Upon receipt of the (empty) reply, client code can safely assume that the kernel has shut down and it’s safe to forcefully terminate it if it’s still alive.

The kernel will send the reply via a function registered with Python’s atexit module, ensuring it’s truly done as the kernel is done with all normal operation.

class IPython.kernel.channels.IOPubChannel(context, session, address)

Bases: IPython.kernel.channels.ZMQSocketChannel

The iopub channel which listens for messages that the kernel publishes.

This channel is where all output is published to frontends.

__init__(context, session, address)
call_handlers(msg)

This method is called in the ioloop thread when a message arrives.

Subclasses should override this method to handle incoming messages. It is important to remember that this method is called in the thread so that some logic must be done to ensure that the application leve handlers are called in the application thread.

flush(timeout=1.0)

Immediately processes all pending messages on the iopub channel.

Callers should use this method to ensure that call_handlers() has been called for all messages that have been received on the 0MQ SUB socket of this channel.

This method is thread safe.

Parameters:

timeout : float, optional

The maximum amount of time to spend flushing, in seconds. The default is one second.

run()

The thread’s main activity. Call start() instead.

class IPython.kernel.channels.StdInChannel(context, session, address)

Bases: IPython.kernel.channels.ZMQSocketChannel

The stdin channel to handle raw_input requests that the kernel makes.

__init__(context, session, address)
call_handlers(msg)

This method is called in the ioloop thread when a message arrives.

Subclasses should override this method to handle incoming messages. It is important to remember that this method is called in the thread so that some logic must be done to ensure that the application leve handlers are called in the application thread.

input(string)

Send a string of raw input to the kernel.

run()

The thread’s main activity. Call start() instead.

class IPython.kernel.channels.HBChannel(context, session, address)

Bases: IPython.kernel.channels.ZMQSocketChannel

The heartbeat channel which monitors the kernel heartbeat.

Note that the heartbeat channel is paused by default. As long as you start this channel, the kernel manager will ensure that it is paused and un-paused as appropriate.

__init__(context, session, address)
call_handlers(since_last_heartbeat)

This method is called in the ioloop thread when a message arrives.

Subclasses should override this method to handle incoming messages. It is important to remember that this method is called in the thread so that some logic must be done to ensure that the application level handlers are called in the application thread.

is_beating()

Is the heartbeat running and responsive (and not paused).

pause()

Pause the heartbeat.

run()

The thread’s main activity. Call start() instead.

stop()

Stop the channel’s event loop and join its thread.

unpause()

Unpause the heartbeat.

2 Functions

IPython.kernel.channels.validate_string_list(lst)

Validate that the input is a list of strings.

Raises ValueError if not.

IPython.kernel.channels.validate_string_dict(dct)

Validate that the input is a dict with string keys and values.

Raises ValueError if not.