def write_connection_file(self): """write connection info to JSON file""" cf = self.abs_connection_file self.log.debug("Writing connection file: %s", cf) write_connection_file(cf, ip=self.ip, key=self.session.key, transport=self.transport, shell_port=self.shell_port, stdin_port=self.stdin_port, hb_port=self.hb_port, iopub_port=self.iopub_port, control_port=self.control_port)
def write_connection_file(self): """Write connection info to JSON dict in self.connection_file.""" if self._connection_file_written: return self.connection_file,cfg = write_connection_file(self.connection_file, transport=self.transport, ip=self.ip, key=self.session.key, stdin_port=self.stdin_port, iopub_port=self.iopub_port, shell_port=self.shell_port, hb_port=self.hb_port) # write_connection_file also sets default ports: self.shell_port = cfg['shell_port'] self.stdin_port = cfg['stdin_port'] self.iopub_port = cfg['iopub_port'] self.hb_port = cfg['hb_port'] self._connection_file_written = True
hb_ctx = zmq.Context() heartbeat = Heartbeat(hb_ctx, (transport, ip, 0)) hb_port = heartbeat.port heartbeat.start() shell_stream = ZMQStream(shell_socket) control_stream = ZMQStream(control_socket) kernel = Kernel(session=session, shell_streams=[shell_stream, control_stream], iopub_socket=iopub_socket, log=logger) write_connection_file(connection_file, shell_port=shell_port, iopub_port=iopub_port, control_port=control_port, hb_port=hb_port, ip=ip) print "To connect another client to this IPython kernel, use:", \ "ipython console --existing %s" % connection_file except Exception, e: print "Exception while initializing IPython ZMQ kernel. %s" % e return def ipython_thread(): kernel.start() try: ioloop.IOLoop.instance().start() except KeyboardInterrupt: pass
def initIPythonKernel(): # You can remotely connect to this kernel. See the output on stdout. try: import IPython.kernel.zmq.ipkernel from IPython.kernel.zmq.ipkernel import Kernel from IPython.kernel.zmq.heartbeat import Heartbeat from IPython.kernel.zmq.session import Session from IPython.kernel import write_connection_file import zmq from zmq.eventloop import ioloop from zmq.eventloop.zmqstream import ZMQStream IPython.kernel.zmq.ipkernel.signal = lambda sig, f: None # Overwrite. except ImportError as e: print("IPython import error, cannot start IPython kernel. %s" % e) return import atexit import socket import logging import threading # Do in mainthread to avoid history sqlite DB errors at exit. # https://github.com/ipython/ipython/issues/680 assert isinstance(threading.currentThread(), threading._MainThread) try: ip = socket.gethostbyname(socket.gethostname()) connection_file = "ipython-kernel-%s-%s.json" % (ip, os.getpid()) def cleanup_connection_file(): try: os.remove(connection_file) except (IOError, OSError): pass atexit.register(cleanup_connection_file) logger = logging.Logger("IPython") logger.addHandler(logging.NullHandler()) session = Session(username=u'kernel') context = zmq.Context.instance() transport = "tcp" addr = "%s://%s" % (transport, ip) shell_socket = context.socket(zmq.ROUTER) shell_port = shell_socket.bind_to_random_port(addr) iopub_socket = context.socket(zmq.PUB) iopub_port = iopub_socket.bind_to_random_port(addr) control_socket = context.socket(zmq.ROUTER) control_port = control_socket.bind_to_random_port(addr) hb_ctx = zmq.Context() heartbeat = Heartbeat(hb_ctx, (transport, ip, 0)) hb_port = heartbeat.port heartbeat.start() shell_stream = ZMQStream(shell_socket) control_stream = ZMQStream(control_socket) kernel = Kernel(session=session, shell_streams=[shell_stream, control_stream], iopub_socket=iopub_socket, log=logger) write_connection_file(connection_file, shell_port=shell_port, iopub_port=iopub_port, control_port=control_port, hb_port=hb_port, ip=ip) #print "To connect another client to this IPython kernel, use:", \ # "ipython console --existing %s" % connection_file except Exception as e: print("Exception while initializing IPython ZMQ kernel. %s" % e) return def ipython_thread(): kernel.start() try: ioloop.IOLoop.instance().start() except KeyboardInterrupt: pass thread = threading.Thread(target=ipython_thread, name="IPython kernel") thread.daemon = True thread.start()
def init_ipython_kernel(): """ Runs IPython in some background kernel, where you can connect to. """ # You can remotely connect to this kernel. See the output on stdout. try: # noinspection PyPackageRequirements,PyUnresolvedReferences import IPython.kernel.zmq.ipkernel # noinspection PyPackageRequirements,PyUnresolvedReferences from IPython.kernel.zmq.ipkernel import Kernel # noinspection PyPackageRequirements,PyUnresolvedReferences from IPython.kernel.zmq.heartbeat import Heartbeat # noinspection PyPackageRequirements,PyUnresolvedReferences from IPython.kernel.zmq.session import Session # noinspection PyPackageRequirements,PyUnresolvedReferences from IPython.kernel import write_connection_file # noinspection PyPackageRequirements,PyUnresolvedReferences import zmq # noinspection PyPackageRequirements,PyUnresolvedReferences from zmq.eventloop import ioloop # noinspection PyPackageRequirements,PyUnresolvedReferences from zmq.eventloop.zmqstream import ZMQStream # noinspection PyPackageRequirements,PyUnresolvedReferences IPython.kernel.zmq.ipkernel.signal = lambda sig, f: None # Overwrite. except ImportError as e: print("IPython import error, cannot start IPython kernel. %s" % e) return import atexit import socket import logging import threading # Do in mainthread to avoid history sqlite DB errors at exit. # https://github.com/ipython/ipython/issues/680 # noinspection PyUnresolvedReferences,PyProtectedMember assert isinstance(threading.currentThread(), threading._MainThread) try: ip = socket.gethostbyname(socket.gethostname()) connection_file = "ipython-kernel-%s-%s.json" % (ip, os.getpid()) def cleanup_connection_file(): """ Cleanup. """ try: os.remove(connection_file) except (IOError, OSError): pass atexit.register(cleanup_connection_file) logger = logging.Logger("IPython") logger.addHandler(logging.NullHandler()) session = Session(username=u'kernel') context = zmq.Context.instance() transport = "tcp" addr = "%s://%s" % (transport, ip) shell_socket = context.socket(zmq.ROUTER) shell_port = shell_socket.bind_to_random_port(addr) iopub_socket = context.socket(zmq.PUB) iopub_port = iopub_socket.bind_to_random_port(addr) control_socket = context.socket(zmq.ROUTER) control_port = control_socket.bind_to_random_port(addr) hb_ctx = zmq.Context() heartbeat = Heartbeat(hb_ctx, (transport, ip, 0)) hb_port = heartbeat.port heartbeat.start() shell_stream = ZMQStream(shell_socket) control_stream = ZMQStream(control_socket) kernel = Kernel(session=session, shell_streams=[shell_stream, control_stream], iopub_socket=iopub_socket, log=logger) write_connection_file(connection_file, shell_port=shell_port, iopub_port=iopub_port, control_port=control_port, hb_port=hb_port, ip=ip) # print "To connect another client to this IPython kernel, use:", \ # "ipython console --existing %s" % connection_file except Exception as e: print("Exception while initializing IPython ZMQ kernel. %s" % e) return def ipython_thread(): """ IPython thread. """ kernel.start() try: ioloop.IOLoop.instance().start() except KeyboardInterrupt: pass thread_ = threading.Thread(target=ipython_thread, name="IPython kernel") thread_.daemon = True thread_.start()