Exemplo n.º 1
0
def knit_chunk(chunk_lines, param_eggs):
    output = []
    for line in chunk_lines:
        code = line
        with run_kernel() as kc:
            msg_id = kc.execute(code)
            reply = kc.get_shell_msg(msg_id)
            print(reply['content'])
            print()

            n = 0
            while True:
                n += 1
                try:
                    io_msg = kc.get_iopub_msg(timeout=1)
                    if 'content' not in io_msg:
                        continue
                    content = io_msg['content']
                    print("{:d}:\t{}".format(n, content))
                    if 'text' in content:
                        output.append(content['text'])
                    if 'data' in content:
                        data = content['data']
                    if 'execution_state' in content:
                        state = content['execution_state']
                except queue.Empty:
                    print('timeout kc.get_iopub_msg')
                    break
    return output
Exemplo n.º 2
0
def new_kernel(argv=None):
    """Context manager for a new kernel in a subprocess

    Should only be used for tests where the kernel must not be re-used.

    Returns
    -------
    kernel_client: connected KernelClient instance
    """
    stdout = getattr(nose, 'iptest_stdstreams_fileno', open(os.devnull))
    kwargs = dict(stdout=stdout, stderr=STDOUT)
    if argv is not None:
        kwargs['extra_arguments'] = argv
    return manager.run_kernel(**kwargs)
Exemplo n.º 3
0
def new_kernel(argv=None):
    """Context manager for a new kernel in a subprocess

    Should only be used for tests where the kernel must not be re-used.

    Returns
    -------
    kernel_client: connected KernelClient instance
    """
    kwargs = dict(stdout=nose.iptest_stdstreams_fileno(),
                  stderr=STDOUT,
                  startup_timeout=STARTUP_TIMEOUT)
    if argv is not None:
        kwargs['extra_arguments'] = argv
    return manager.run_kernel(**kwargs)
Exemplo n.º 4
0
def new_kernel(argv=None):
    """Context manager for a new kernel in a subprocess

    Should only be used for tests where the kernel must not be re-used.

    Returns
    -------
    kernel_client: connected KernelClient instance
    """
    kwargs = dict(
        stdout=nose.iptest_stdstreams_fileno(), stderr=STDOUT,
                  startup_timeout=STARTUP_TIMEOUT)
    if argv is not None:
        kwargs['extra_arguments'] = argv
    return manager.run_kernel(**kwargs)
Exemplo n.º 5
0
    def run_code(self, code):
        print("executing code: " + code)

        with run_kernel() as kc:
            msg_id = kc.execute("print(1+2)")
            reply = kc.get_shell_msg(msg_id)
            print(reply['content'])
            print()

            while True:
                try:
                    io_msg = kc.get_iopub_msg(timeout=1)
                    print(io_msg['content'])
                except queue.Empty:
                    print('timeout kc.get_iopub_msg')
                    break
Exemplo n.º 6
0
def new_kernel(argv=None):
    """Context manager for a new kernel in a subprocess

    Should only be used for tests where the kernel must not be re-used.

    Returns
    -------
    kernel_client: connected KernelClient instance
    """
    kwargs = {'stderr': STDOUT}
    try:
        kwargs['stdout'] = nose.iptest_stdstreams_fileno()
    except AttributeError:
        pass
    if argv is not None:
        kwargs['extra_arguments'] = argv
    return manager.run_kernel(**kwargs)
Exemplo n.º 7
0
import logging
from jupyter_client.manager import run_kernel

logging.basicConfig(level=logging.DEBUG)

codes = [
    "x = 1 + 1",
    "print('hello', x)",
]

with run_kernel(kernel_name="python") as c:
    for code in codes:
        # https://jupyter-client.readthedocs.io/en/stable/messaging.html?highlight=idle#kernel-status
        # state = busy, idle, starting
        state = "busy"

        msg_id = c.execute(code)
        while state != "idle" and c.is_alive():
            msg = c.get_iopub_msg(timeout=1)
            content = msg.get('content')
            if content is None:
                continue

            if 'execution_state' in content:
                state = content['execution_state']
            print(f"{state}> content: {content}")
Exemplo n.º 8
0
import logging
from jupyter_client.manager import run_kernel

logging.basicConfig(level=logging.DEBUG)

codes = [
    "x = 1 + 1",
    "print('hello', x)",
]

with run_kernel(kernel_name="python") as c:
    for code in codes:
        # https://jupyter-client.readthedocs.io/en/stable/messaging.html?highlight=idle#kernel-status
        # state = busy, idle, starting
        state = "busy"

        msg_id = c.execute(code)
        while state != "idle" and c.is_alive():
            msg = c.get_iopub_msg(timeout=1)
            content = msg.get('content')
            if content is None:
                continue

            if 'execution_state' in content:
                state = content['execution_state']
            print(f"{state}> content: {content}")

"""
DEBUG:traitlets:Starting kernel: ['VENV/bin/python', '-m', 'ipykernel_launcher', '-f', '/tmp/tmp0btfwzuf.json']
DEBUG:traitlets:Connecting to: tcp://127.0.0.1:40589
DEBUG:traitlets:connecting shell channel to tcp://127.0.0.1:47581