当前位置: 首页>>代码示例>>Python>>正文


Python KernelManager.shutdown_kernel方法代码示例

本文整理汇总了Python中IPython.kernel.KernelManager.shutdown_kernel方法的典型用法代码示例。如果您正苦于以下问题:Python KernelManager.shutdown_kernel方法的具体用法?Python KernelManager.shutdown_kernel怎么用?Python KernelManager.shutdown_kernel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IPython.kernel.KernelManager的用法示例。


在下文中一共展示了KernelManager.shutdown_kernel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: run_notebook

# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import shutdown_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
开发者ID:hbcbh1999,项目名称:stats-lecture-notes,代码行数:62,代码来源:nbtools.py

示例2: test_notebook

# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import shutdown_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
开发者ID:arsenovic,项目名称:hftools,代码行数:62,代码来源:ipnbdoctest.py

示例3: run_notebook

# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import shutdown_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
开发者ID:LucasGandel,项目名称:TubeTK,代码行数:60,代码来源:EvaluateIPythonNotebook.py

示例4: run_notebook

# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import shutdown_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
开发者ID:Arthurkorn,项目名称:nilearn_course,代码行数:57,代码来源:ipynbhelper.py

示例5: run_notebook

# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import shutdown_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
开发者ID:Biodun,项目名称:parallel_ml_tutorial,代码行数:57,代码来源:ipynbhelper.py

示例6: run_notebook

# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import shutdown_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
开发者ID:meduz,项目名称:run_ipynbs,代码行数:56,代码来源:run_ipynbs.py

示例7: run_notebook

# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import shutdown_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
开发者ID:jbn,项目名称:BatchNotebook,代码行数:51,代码来源:__init__.py

示例8: run_nb_offline

# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import shutdown_kernel [as 别名]
def run_nb_offline(nb_path):
    """ Read notebook from filepath and execute it; report errors in code cells.
    """
    if not os.path.isfile(nb_path):
        raise Exception("Invalid path: %s" % nb_path)
    with open(nb_path) as f:
        nb = reads(f.read(), "json")

    logging.info("Running notebook %s" % nb.metadata.name)
    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")
    shell.get_msg()

    cells = 0
    failures = 0
    for ws in nb.worksheets:
        for cell in ws.cells:
            if cell.cell_type != "code":  # ONLY RUN CODE CELLS
                continue
            shell.execute(cell.input)
            # wait for finish, maximum TIMEOUT
            reply = shell.get_msg(timeout=MAX_TIMEOUT)["content"]
            if reply["status"] == "error":
                failures += 1
                logging.info("\nNotebook FAILURE:")
                logging.info(cell.input)
                logging.info("-----")
                logging.info("raised:")
                logging.info("\n".join(reply["traceback"]))
            cells += 1
    #            sys.stdout.write('.')

    logging.info("Finished running notebook")
    logging.info("    ran %3i cells" % cells)
    if failures:
        logging.warning("    %3i cells raised exceptions" % failures)
    kc.stop_channels()
    km.shutdown_kernel()
    del km
开发者ID:hainm,项目名称:scikit-spectra,代码行数:50,代码来源:nbutils.py

示例9: RunningKernel

# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import shutdown_kernel [as 别名]
class RunningKernel(object):
    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()

    def restart(self):
        self.km.restart_kernel(now=True)

    def stop(self):
        self.kc.stop_channels()
        self.km.shutdown_kernel()
        del self.km
开发者ID:takluyver,项目名称:pytest_validate_nb,代码行数:19,代码来源:original_test.py

示例10: run_notebook

# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import shutdown_kernel [as 别名]
def run_notebook(nb):
    km = KernelManager()
    km.start_kernel(extra_arguments=['--profile', 'stats'])#, 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")
    shell.get_msg()
    
    shell.execute("datadir = '%s'" % os.path.abspath(os.path.join( \
                os.path.abspath(os.path.dirname(__file__)), '..', 'data')))

    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
            msg = shell.get_msg(timeout=20)

            reply = msg['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 "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
开发者ID:hbcbh1999,项目名称:stats-lecture-notes,代码行数:47,代码来源:checkipnb.py

示例11: run_notebook

# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import shutdown_kernel [as 别名]
def run_notebook(nb):
    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")
    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']))
            print(cell)
            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
开发者ID:Calama-Consulting,项目名称:perrin-academy,代码行数:43,代码来源:checkipnb.py

示例12: run_notebook

# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import shutdown_kernel [as 别名]
def run_notebook(nb):
    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:
        for cell in ws.cells:
            if cell.cell_type != 'code':
                continue

            outputs, failed = run_cell(shell, iopub, cell)
            cell.outputs = outputs
            cell['prompt_number'] = cells
            failures += failed
            cells += 1
            sys.stdout.write('.')
            sys.stdout.flush()

    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
开发者ID:CheMcCandless,项目名称:parallel_ml_tutorial,代码行数:43,代码来源:ipynbhelper.py

示例13: test_notebook

# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import shutdown_kernel [as 别名]
def test_notebook(nb):
    km = KernelManager()
    km.start_kernel(extra_arguments=[], stderr=open(os.devnull, 'w'))
    kc = km.client()
    kc.start_channels()
    iopub = kc.iopub_channel
    shell = kc.shell_channel
    shell.kernel_info()

    while True:
        try:
            kc.iopub_channel.get_msg(timeout=1)
        except Empty:
            break

    errors = 0
    cells = 0
    for ws in nb.worksheets:
        for cell in ws.cells:
            if cell.cell_type != 'code':
                continue
            cells += 1
            try:
                outs = run_cell(shell, iopub, cell)
            except Exception as e:
                print("failed to run cell:", repr(e))
                print(cell.input)
                errors += 1
                continue
            cell.outputs = outs

    if errors:
        print("    %3i cells failed to complete" % errors)
    if cells:
        print("%i code cells from notebook %s" % (cells, nb.metadata.name))

    kc.stop_channels()
    km.shutdown_kernel()
    del km
开发者ID:Afey,项目名称:bokeh,代码行数:41,代码来源:nbexecuter.py

示例14: new_kernel

# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import shutdown_kernel [as 别名]
def new_kernel():
    """start a kernel in a subprocess, and wait for it to be ready
    
    Returns
    -------
    kernel_manager: connected KernelManager instance
    """
    KM = KernelManager()

    KM.start_kernel(stdout=PIPE, stderr=PIPE)
    KC = KM.client()
    KC.start_channels()
    
    # wait for kernel to be ready
    KC.shell_channel.execute("import sys")
    KC.shell_channel.get_msg(block=True, timeout=STARTUP_TIMEOUT)
    flush_channels(KC)
    try:
        yield KC
    finally:
        KC.stop_channels()
        KM.shutdown_kernel()
开发者ID:jonahkichwacoders,项目名称:ipython,代码行数:24,代码来源:test_kernel.py

示例15: execute_notebook

# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import shutdown_kernel [as 别名]
def execute_notebook(nb):
    km = KernelManager()
    km.start_kernel(extra_arguments=["--pylab=inline", "--profile=stats"], 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:
        for cell in ws.cells:
            cell.prompt_number = prompt_number
            if cell.cell_type != "code":
                continue
            run_cell(kc, cell)
            try:
                outs = run_cell(kc, cell)
            except Exception as e:
                print "failed to run cell:", repr(e)
                print cell.input
                errors += 1
                continue

            sys.stdout.write(".")
            cell.outputs = outs
            prompt_number += 1
    km.shutdown_kernel()
    del km
    return nb
开发者ID:ldfaiztt,项目名称:stats-lecture-notes,代码行数:40,代码来源:execute_and_save.py


注:本文中的IPython.kernel.KernelManager.shutdown_kernel方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。