Skip to content

Instantly share code, notes, and snippets.

@jayendra13
Created January 16, 2017 14:33
Show Gist options
  • Save jayendra13/e553fafba5398e287107e947c16988df to your computer and use it in GitHub Desktop.
Save jayendra13/e553fafba5398e287107e947c16988df to your computer and use it in GitHub Desktop.
from zmq.eventloop import ioloop
ioloop.install()
from zmq.eventloop.ioloop import ZMQIOLoop
from zmq.eventloop.zmqstream import ZMQStream
from functools import partial
from jupyter_client import MultiKernelManager
from tornado import gen
from tornado.concurrent import Future
from pprint import pprint
km = MultiKernelManager()
reply_futures = {}
def reply_callback(session, stream, msg_list):
print("{}".format(stream.io_loop))
idents, msg_parts = session.feed_identities(msg_list)
reply = session.deserialize(msg_parts)
parent_id = reply['parent_header'].get('msg_id')
reply_future = reply_futures.get(parent_id)
#pprint(reply)
print(reply['msg_type'])
#print('\n')
if reply_future:
if "execute_reply" == reply["msg_type"]:
reply_future.set_result(reply)
kernel_id = km.start_kernel()
kernel_client = km.get_kernel(kernel_id).client()
kernel_client.start_channels()
shell_stream = ZMQStream(kernel_client.shell_channel.socket)
iopub_stream = ZMQStream(kernel_client.iopub_channel.socket)
shell_stream.on_recv_stream(partial(reply_callback, kernel_client.session))
iopub_stream.on_recv_stream(partial(reply_callback, kernel_client.session))
print("{} {}".format(shell_stream.io_loop, iopub_stream.io_loop))
@gen.coroutine
def execute(code):
msg_id = kernel_client.execute(code)
f = reply_futures[msg_id] = Future()
print(msg_id)
yield f
raise gen.Return(msg_id)
#loop = ZMQIOLoop()
#loop.run_sync(execute)
code_ = "print 'Hello'"
r = ioloop.IOLoop.current().run_sync(lambda: execute(code_))
print 'returned from ioloop {}'.format(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment