Exemple #1
0
# startup channel
pidfile = '/tmp/ipython.pid'
with open(pidfile, 'r') as f:
    cf = f.readline()
# remove trailing carriage-return
cf = cf[:-1]
try:
    # get real pid of ipython kernel
    cf = str(int(cf) + 1)
    cf = find_connection_file(cf)
except IOError:
    cf = str(int(cf) + 1)
    cf = find_connection_file(cf)
km = BlockingKernelManager()
km.connection_file = cf
km.load_connection_file()
km.start_channels()

def run_code(code):
    # execution is immediate and async, returning a UUID
    msg_id = km.shell_channel.execute(code)
    # get_meg can block for a reply
    reply = km.shell_channel.get_msg()

    if reply['content']['status'] == 'error':
        for line in reply['content']['traceback']:
            print line

# ZMQ server
context = zmq.Context()