コード例 #1
0
def setup_kernel(cmd):
    """start an embedded kernel in a subprocess, and wait for it to be ready
    
    Returns
    -------
    kernel_manager: connected KernelManager instance
    """
    kernel = Popen([sys.executable, '-c', cmd], stdout=PIPE, stderr=PIPE, env=env)
    connection_file = os.path.join(IPYTHONDIR,
                                    'profile_default',
                                    'security',
                                    'kernel-%i.json' % kernel.pid
    )
    # wait for connection file to exist, timeout after 5s
    tic = time.time()
    while not os.path.exists(connection_file) \
        and kernel.poll() is None \
        and time.time() < tic + SETUP_TIMEOUT:
        time.sleep(0.1)
    
    if kernel.poll() is not None:
        o,e = kernel.communicate()
        e = py3compat.cast_unicode(e)
        raise IOError("Kernel failed to start:\n%s" % e)
    
    if not os.path.exists(connection_file):
        if kernel.poll() is None:
            kernel.terminate()
        raise IOError("Connection file %r never arrived" % connection_file)
    
    client = BlockingKernelClient(connection_file=connection_file)
    client.load_connection_file()
    client.start_channels()
    client.wait_for_ready()
    
    try:
        yield client
    finally:
        client.stop_channels()
        kernel.terminate()
コード例 #2
0
ファイル: reconnect.py プロジェクト: urbanows/notebook
from IPython.lib.kernel import find_connection_file
from IPython.kernel import BlockingKernelClient
import os

# <codecell>

cf = '/home/epilib/Envs/env1/.ipython/profile_default/security/kernel-c5bd6be3-7df6-4b6f-8289-4d54908e39db.json'

# <codecell>

cf = '/home/usgs/.ipython/profile_default/security/kernel-632e13a5-00f5-422c-9588-34b195bc2349.json'

# <codecell>

km = BlockingKernelClient(connection_file=cf)

# <codecell>

km.load_connection_file()

# <codecell>

km.start_channels()

# <codecell>

def run_cell(km, code):
    # now we can run code.  This is done on the shell channel
    shell = km.shell_channel
    print