本文整理汇总了Python中IPython.kernel.KernelManager类的典型用法代码示例。如果您正苦于以下问题:Python KernelManager类的具体用法?Python KernelManager怎么用?Python KernelManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了KernelManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setup_client
def setup_client():
manager = KernelManager()
#manager.kernel_name = "spread_kernel"
manager.start_kernel()
#Setup the client
client = manager.client()
client.start_channels()
#Hijack the IOPub channel
new_io = MyIOpubChannel(client.context,client.session, client._make_url('iopub'))
client._iopub_channel = new_io
#Set up said channel
client.iopub_channel.start()
for method in client.iopub_channel.proxy_methods:
setattr(client, method, getattr(client.iopub_channel, method))
#Hijack the shell channel
#old_io_2 = client._shell_channel
new_shell = MyShellChannel(client.context,client.session, client._make_url('shell'))
client._shell_channel = new_shell
#Set up said channel
client.shell_channel.start()
for method in client.shell_channel.proxy_methods:
setattr(client, method, getattr(client.shell_channel, method))
return (manager,client)
示例2: __init__
def __init__(self, notebook_dir, extra_args=None, profile=None,
timeout=90):
self.notebook_dir = os.path.abspath(notebook_dir)
self.profile = profile
self.timeout = timeout
km = KernelManager()
if extra_args is None:
extra_args = []
if profile is not None:
extra_args += ["--profile=%s" % profile]
km.start_kernel(stderr=open(os.devnull, 'w'),
extra_arguments=extra_args)
try:
kc = km.client()
kc.start_channels()
iopub = kc.iopub_channel
except AttributeError: # still on 0.13
kc = km
kc.start_channels()
iopub = kc.sub_channel
shell = kc.shell_channel
# make sure it's working
shell.execute("pass")
shell.get_msg()
# all of these should be run pylab inline
shell.execute("%pylab inline")
shell.get_msg()
self.kc = kc
self.km = km
self.iopub = iopub
示例3: start_new_kernel
def start_new_kernel():
"""start a new kernel, and return its Manager and Client"""
km = KernelManager()
km.start_kernel(stdout=PIPE, stderr=PIPE)
kc = km.client()
kc.start_channels()
msg_id = kc.kernel_info()
kc.get_shell_msg(block=True, timeout=STARTUP_TIMEOUT)
flush_channels(kc)
return km, kc
示例4: setup
def setup():
global KM, KC
KM = KernelManager()
KM.start_kernel(stdout=PIPE, stderr=PIPE)
KC = KM.client()
KC.start_channels()
# wait for kernel to be ready
KC.execute("pass")
KC.get_shell_msg(block=True, timeout=5)
flush_channels()
示例5: start_new_kernel
def start_new_kernel(argv=None):
"""start a new kernel, and return its Manager and Client"""
km = KernelManager()
kwargs = dict(stdout=nose.iptest_stdstreams_fileno(), stderr=STDOUT)
if argv:
kwargs['extra_arguments'] = argv
km.start_kernel(**kwargs)
kc = km.client()
kc.start_channels()
msg_id = kc.kernel_info()
kc.get_shell_msg(block=True, timeout=STARTUP_TIMEOUT)
flush_channels(kc)
return km, kc
示例6: km_from_string
def km_from_string(s=''):
"""create kernel manager from IPKernelApp string
such as '--shell=47378 --iopub=39859 --stdin=36778 --hb=52668' for IPython 0.11
or just 'kernel-12345.json' for IPython 0.12
"""
global km, kc, send
from os.path import join as pjoin
from IPython.config.loader import KeyValueConfigLoader
try:
from IPython.kernel import (
KernelManager,
find_connection_file,
)
except ImportError:
from IPython.zmq.blockingkernelmanager import BlockingKernelManager as KernelManager
from IPython.zmq.kernelapp import kernel_aliases
from IPython.lib.kernel import find_connection_file
s = s.replace('--existing', '')
if '--profile' in s:
k,p = s.split('--profile')
k = k.lstrip().rstrip() # kernel part of the string
p = p.lstrip().rstrip() # profile part of the string
fullpath = find_connection_file(k,p)
else:
fullpath = find_connection_file(s.lstrip().rstrip())
km = KernelManager(connection_file = fullpath)
km.load_connection_file()
km.start_channels()
send = km.shell_channel.execute # not sure if this should be changed as well
respond(None, "python.client.error.ipython-version", None)
return
s = s.replace('--existing', '')
if '--profile' in s:
k,p = s.split('--profile')
k = k.lstrip().rstrip() # kernel part of the string
p = p.lstrip().rstrip() # profile part of the string
fullpath = find_connection_file(k,p)
else:
fullpath = find_connection_file(s.lstrip().rstrip())
km = KernelManager(connection_file = fullpath)
km.load_connection_file()
kc = km.client()
kc.start_channels()
send = kc.execute
return km
示例7: execute_kernel
def execute_kernel(nb, t, tshell):
"""
Load Kernel stuff
iopub may be necessary to run through the cell
"""
km = KernelManager()
km.start_kernel(extra_arguments=['--matplotlib=inline'],
stderr=open(os.devnull, 'w'))
try:
kc = km.client()
kc.start_channels()
iopub = kc.iopub_channel
except AttributeError:
# IPython 0.13
kc = km
kc.start_channels()
iopub = kc.sub_channel
shell = kc.shell_channel
"""
This part needs revision
"""
# run %pylab inline, because some notebooks assume this
# even though they shouldn't
shell.execute("pass")
shell.get_msg()
while True:
try:
iopub.get_msg(timeout=1)
except Empty:
break
"""
Try to print cell by cell
"""
# Only one worksheet in the current ipython nbs structure
for cell in nb.worksheets[0].cells:
# If the cell is code, move to next cell
if cell.cell_type != 'code':
continue
# Otherwise the cell is an output cell, run it!
try:
outs = run_cell(shell, iopub, cell, t, tshell)
print outs
except Exception as e:
print "failed to run cell:", repr(e)
print cell.input
示例8: setup
def setup():
global KM, KC
KM = KernelManager()
KM.start_kernel(stdout=PIPE, stderr=PIPE)
KC = KM.client()
KC.start_channels()
# wait for kernel to be ready
try:
msg = KC.iopub_channel.get_msg(block=True, timeout=STARTUP_TIMEOUT)
except Empty:
pass
msg_id = KC.kernel_info()
KC.get_shell_msg(block=True, timeout=STARTUP_TIMEOUT)
flush_channels()
示例9: new_ipy
def new_ipy(s=''):
"""Create a new IPython kernel (optionally with extra arguments)
XXX: Allow passing of profile information here
Examples
--------
new_ipy()
"""
from IPython.kernel import KernelManager
km = KernelManager()
km.start_kernel()
return km_from_string(km.connection_file)
示例10: __enter__
def __enter__(self):
self.km = KernelManager()
self.km.start_kernel(extra_arguments=self.extra_arguments, stderr=open(os.devnull, 'w'))
try:
self.kc = self.km.client()
self.kc.start_channels()
self.iopub = self.kc.iopub_channel
except AttributeError:
# IPython 0.13
self.kc = self.km
self.kc.start_channels()
self.iopub = self.kc.sub_channel
self.shell = self.kc.shell_channel
# run %pylab inline, because some notebooks assume this
# even though they shouldn't
self.shell.execute("pass")
self.shell.get_msg()
while True:
try:
self.iopub.get_msg(timeout=1)
except Empty:
break
return self
示例11: __init__
def __init__(self, **kw):
"""Initializes the notebook runner.
Requires config rcloud_python_lib_path -- or raises exception"""
self.km = KernelManager()
self.km.kernel_cmd = make_ipkernel_cmd("""
import sys;
sys.path.extend("{RCPATH}".split(":"));
from rcloud_kernel import main;
main()""".format(RCPATH=kw["rcloud_python_lib_path"]), **kw)
del kw["rcloud_python_lib_path"]
self.km.client_factory = MyClient
self.km.start_kernel(**kw)
if platform.system() == 'Darwin':
# There is sometimes a race condition where the first
# execute command hits the kernel before it's ready.
# It appears to happen only on Darwin (Mac OS) and an
# easy (but clumsy) way to mitigate it is to sleep
# for a second.
sleep(1)
self.kc = self.km.client()
self.kc.start_channels()
self.shell = self.kc.shell_channel
self.iopub = self.kc.iopub_channel
示例12: __init__
def __init__(self, nb_in=None, pylab=False, mpl_inline=False, nb=None):
self.km = KernelManager()
if pylab:
self.km.start_kernel(extra_arguments=['--pylab=inline'])
elif mpl_inline:
self.km.start_kernel(extra_arguments=['--matplotlib=inline'])
else:
self.km.start_kernel()
if platform.system() == 'Darwin':
# There is sometimes a race condition where the first
# execute command hits the kernel before it's ready.
# It appears to happen only on Darwin (Mac OS) and an
# easy (but clumsy) way to mitigate it is to sleep
# for a second.
sleep(1)
self.kc = self.km.client()
self.kc.start_channels()
self.shell = self.kc.shell_channel
self.iopub = self.kc.iopub_channel
logging.info('Reading notebook %s', nb_in)
self.nb = nb
if not self.nb:
self.nb = read(open(nb_in), 'json')
示例13: __init__
def __init__(self, **kw):
"""Initializes the notebook runner.
Requires config rcloud_python_lib_path -- or raises exception"""
self.km = KernelManager()
self.km.kernel_cmd = make_ipkernel_cmd("""
import sys;
import time; # to test for slow staring kernels, add a delay here
sys.path.extend("{RCPATH}".split(":"));
from rcloud_kernel import main;
main()""".format(RCPATH=kw["rcloud_python_lib_path"]), **kw)
del kw["rcloud_python_lib_path"]
self.km.client_factory = MyClient
self.km.start_kernel(**kw) # This is a non-blocking call to launch the process
self.completer = completer.Completer()
self.completer.limit_to__all__ = True
# There is a possible race condition if the system is slow to load
# the modules for the kernel and we issue commands to the kernel rightaway.
# We saw these on CentOS and OSX. So, issue an empty command to the kernel and wait for return
self.kc = self.km.client()
self.kc.start_channels()
self.shell = self.kc.shell_channel
self.shell.execute("")
_ = self.shell.get_msgs()
self.iopub = self.kc.iopub_channel
示例14: __init__
def __init__(self):
self.km = KernelManager()
self.km.start_kernel(stderr=open(os.devnull, 'w'))
self.kc = self.km.client()
self.kc.start_channels()
self.shell = self.kc.shell_channel
self.shell.execute("pass")
self.shell.get_msg()
示例15: __init__
def __init__(
self,
nb,
pylab=False,
mpl_inline=False,
profile_dir=None,
working_dir=None):
self.first_cell = True
self.km = KernelManager()
args = []
if pylab:
args.append('--pylab=inline')
logging.warn(
'--pylab is deprecated and will be removed in a future version'
)
elif mpl_inline:
args.append('--matplotlib=inline')
logging.warn(
'--matplotlib is deprecated and' +
' will be removed in a future version'
)
if profile_dir:
args.append('--profile-dir=%s' % os.path.abspath(profile_dir))
cwd = os.getcwd()
if working_dir:
os.chdir(working_dir)
self.km.start_kernel(extra_arguments=args)
os.chdir(cwd)
if platform.system() == 'Darwin':
# There is sometimes a race condition where the first
# execute command hits the kernel before it's ready.
# It appears to happen only on Darwin (Mac OS) and an
# easy (but clumsy) way to mitigate it is to sleep
# for a second.
sleep(1)
self.kc = self.km.client()
self.kc.start_channels()
try:
self.kc.wait_for_ready()
except AttributeError:
# IPython < 3
self._wait_for_ready_backport()
self.nb = nb