本文整理汇总了Python中IPython.kernel.KernelManager.start_kernel方法的典型用法代码示例。如果您正苦于以下问题:Python KernelManager.start_kernel方法的具体用法?Python KernelManager.start_kernel怎么用?Python KernelManager.start_kernel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IPython.kernel.KernelManager
的用法示例。
在下文中一共展示了KernelManager.start_kernel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_notebook
# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import start_kernel [as 别名]
def test_notebook(nb):
km = KernelManager()
km.start_kernel(extra_arguments=['--pylab=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
# 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
successes = 0
failures = 0
errors = 0
for ws in nb.worksheets:
for cell in ws.cells:
if cell.cell_type != 'code':
continue
try:
outs = run_cell(shell, iopub, cell)
except Exception as e:
print "failed to run cell:", repr(e)
print cell.input
errors += 1
continue
failed = False
for out, ref in zip(outs, cell.outputs):
if not compare_outputs(out, ref):
failed = True
if failed:
failures += 1
else:
successes += 1
sys.stdout.write('.')
print
print "tested notebook %s" % nb.metadata.name
print " %3i cells successfully replicated" % successes
if failures:
print " %3i cells mismatched output" % failures
if errors:
print " %3i cells failed to complete" % errors
kc.stop_channels()
km.shutdown_kernel()
del km
示例2: __init__
# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import start_kernel [as 别名]
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: setup_client
# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import start_kernel [as 别名]
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)
示例4: run_notebook
# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import start_kernel [as 别名]
def run_notebook(nb, cell_filter = lambda cell: cell,
extra_arguments=['--pylab=inline', '--profile=stats'],
modify_outputs=True,
run_cells=True,
timeout=10):
"""
Take a notebook and send all its cells to a kernel.
Takes an optional filter to modify the results of the
cell after being run and having its
output set by `run_cell` if modify_outputs is True.
"""
km = KernelManager()
km.start_kernel(extra_arguments=extra_arguments,
stderr=open(os.devnull, 'w'))
try:
kc = km.client()
except AttributeError:
# 0.13
kc = km
kc.start_channels()
shell = kc.shell_channel
shell.execute("pass")
shell.get_msg()
successes = 0
failures = 0
errors = 0
prompt_number = 1
for ws in nb.worksheets:
new_cells = []
for cell in ws.cells:
cell.prompt_number = prompt_number
if cell['cell_type'] != 'code':
new_cells.append(cell)
continue
if run_cells:
try:
outs = run_cell(kc, cell,
collect_outputs=modify_outputs,
timeout=timeout)
except Exception as e:
sys.stdout.write("failed to run cell:" + repr(e))
errors += 1
continue
sys.stdout.write('.')
if modify_outputs:
cell.outputs = outs
new_cell = cell_filter(cell)
if new_cell is not None:
new_cells.append(new_cell)
prompt_number += 1
sys.stdout.write('\n')
ws.cells = new_cells
km.shutdown_kernel()
del km
return nb
示例5: run_notebook
# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import start_kernel [as 别名]
def run_notebook(nb):
"""Run IPython Notebook.
Paramters:
----------
nb : IPython Notebook in JSON format.
Returns:
--------
ret : int
Return value; 0 in case of no failure, 1 otherwise
"""
km = KernelManager()
km.start_kernel(stderr=open(os.devnull, 'w'))
try:
kc = km.client()
except AttributeError:
# 0.13
kc = km
kc.start_channels()
shell = kc.shell_channel
# simple ping:
shell.execute("pass")
reply = shell.get_msg()
cells = 0
failures = 0
for ws in nb.worksheets:
for cell in ws.cells:
if cell.cell_type != 'code':
continue
shell.execute(cell.input)
# wait for finish, maximum 20s
reply = shell.get_msg(timeout=20)['content']
if reply['status'] == 'error':
failures += 1
print "\nFAILURE:"
print cell.input
print '-----'
print "raised:"
print '\n'.join(reply['traceback'])
cells += 1
sys.stdout.write('.')
print
print "ran notebook %s" % nb.metadata.name
print " ran %3i cells" % cells
if failures:
print " %3i cells raised exceptions" % failures
kc.stop_channels()
km.shutdown_kernel()
del km
if failures:
return 1
return 0
示例6: run_notebook
# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import start_kernel [as 别名]
def run_notebook(nb, save_output=False):
km = KernelManager()
km.start_kernel(stderr=open(os.devnull, 'w'))
if hasattr(km, 'client'):
kc = km.client()
kc.start_channels()
iopub = kc.iopub_channel
else:
# IPython 0.13 compat
kc = km
kc.start_channels()
iopub = kc.sub_channel
shell = kc.shell_channel
# simple ping:
shell.execute("pass")
shell.get_msg()
cells = 0
failures = 0
for ws in nb.worksheets:
rendered_cells = list()
for cell in ws.cells:
rendered_cells.append(cell)
if cell.cell_type != 'code':
continue
outputs, failed, payload = run_cell(shell, iopub, cell)
cell.outputs = outputs
cell['prompt_number'] = cells
failures += failed
cells += 1
sys.stdout.write('.')
# Very hacky code to execute the loaded Python code
if payload and payload[0]['source'] == 'set_next_input':
new_cell = cell.copy()
new_cell["input"] = payload[0]["text"]
outputs, failed, _ = run_cell(shell, iopub, new_cell)
new_cell.outputs = outputs
new_cell['prompt_number'] = cells
failures += failed
cells += 1
sys.stdout.write('.')
rendered_cells.append(new_cell)
if save_output:
ws.cells = rendered_cells
print()
print("ran notebook %s" % nb.metadata.name)
print(" ran %3i cells" % cells)
if failures:
print(" %3i cells raised exceptions" % failures)
kc.stop_channels()
km.shutdown_kernel()
del km
示例7: run_notebook
# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import start_kernel [as 别名]
def run_notebook(nb):
km = KernelManager()
km.start_kernel(stderr=open(os.devnull, 'w'))
kc = km.client()
kc.start_channels()
try:
kc.wait_for_ready()
except AttributeError:
# IPython < 3
kc.kernel_info()
while True:
msg = kc.get_shell_msg(block=True, timeout=30)
if msg['msg_type'] == 'kernel_info_reply':
break
# Flush IOPub channel
while True:
try:
msg = kc.get_iopub_msg(block=True, timeout=0.2)
except Empty:
break
# simple ping:
kc.execute("pass")
kc.get_shell_msg()
cells = 0
failures = 0
if hasattr(nb, 'worksheets'):
# nobody uses more than 1 worksheet
ws = nb.worksheets[0]
else:
# no more worksheet level in new format
ws = nb
for cell in ws.cells:
if cell.cell_type != 'code':
continue
outputs, failed = run_cell(kc, cell)
cell.outputs = outputs
cell['prompt_number'] = cells
failures += failed
cells += 1
sys.stdout.write('.')
sys.stdout.flush()
print()
print("ran %3i cells" % cells)
if failures:
print(" %3i cells raised exceptions" % failures)
kc.stop_channels()
km.shutdown_kernel()
del km
示例8: setup
# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import start_kernel [as 别名]
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()
示例9: run_notebook
# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import start_kernel [as 别名]
def run_notebook(nb, output=False):
"""
"""
km = KernelManager()
km.start_kernel(extra_arguments=['--pylab=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
# 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
cells = 0
failures = 0
for ws in nb.worksheets:
for cell in ws.cells:
if cell.cell_type != 'code':
continue
log.info('Run cell #%i' % cells)
cells += 1
outs = run_cell(shell, iopub, cell, output=output)
if outs:
for i in range(len(outs)):
if outs[i]['output_type'] == "pyerr":
log.error('Fail to execute cell #%i\n' % cells + '\n'.join(outs[i]['traceback']))
failures += 1
continue
log.info('Done')
log.info("%i cells runned with %i cells failed" % (cells, failures))
kc.stop_channels()
km.shutdown_kernel()
del km
示例10: start_new_kernel
# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import start_kernel [as 别名]
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
示例11: start_new_kernel
# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import start_kernel [as 别名]
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
示例12: execute_kernel
# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import start_kernel [as 别名]
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
示例13: run_notebook
# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import start_kernel [as 别名]
def run_notebook(nb, pylab_inline=True, timeout=20):
"""
Run the notebook, populating the output cells with appropriate content.
Params
------
nb : the contents of a notebook as a string
pylab_inline : i.e. should the command be executed as if it was flagged with
--paylab=inline
timeout : the length of time in seconds to wait before the script is
considered timed out. I set this to a big value for some
data heavy scripts
"""
# Start the kernel.
km = KernelManager()
args = {}
if pylab_inline:
args['extra_arguments'] = ['--pylab=inline']
km.start_kernel(**args)
# Get our client.
try:
kc = km.client()
except AttributeError:
kc = km
kc.start_channels()
shell = kc.shell_channel
# Ping the kernel.
shell.execute('pass')
shell.get_msg()
# Run all the cells.
cells_executed, cells_failed = 0, 0
for ws in nb.worksheets:
for cell in ws.cells:
cell.prompt_number = cells_executed + 1
if cell.cell_type != 'code':
continue
cells_executed += 1
run_cell(kc, cell, timeout)
# Clean up resources. (Hopefully?)
kc.stop_channels()
km.shutdown_kernel()
del km
return cells_failed
示例14: setup
# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import start_kernel [as 别名]
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()
示例15: new_ipy
# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import start_kernel [as 别名]
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)