def km_from_string(s = PMX_IPYTHON_CONNECTION): """create kernel manager from IPKernelApp string such as '--shell=47378 --iopub=39859 --stdin=36778 --hb=52668' for IPython 0.11 or just 'kernel-12345.json' for IPython 0.12 """ from os.path import join as pjoin from IPython.zmq.blockingkernelmanager import BlockingKernelManager from IPython.config.loader import KeyValueConfigLoader from IPython.zmq.kernelapp import kernel_aliases s = s.replace('--existing', '') if 'connection_file' in BlockingKernelManager.class_trait_names(): from IPython.lib.kernel import find_connection_file # 0.12 uses files instead of a collection of ports # include default IPython search path # filefind also allows for absolute paths, in which case the search # is ignored try: # XXX: the following approach will be brittle, depending on what # connection strings will end up looking like in the future, and # whether or not they are allowed to have spaces. I'll have to sync # up with the IPython team to address these issues -pi if '--profile' in s: k,p = s.split('--profile') k = k.lstrip().rstrip() # kernel part of the string p = p.lstrip().rstrip() # profile part of the string fullpath = find_connection_file(k,p) else: fullpath = find_connection_file(s.lstrip().rstrip()) except IOError,e: print ":IPython " + s + " failed", "Info" print "^-- failed '" + s + "' not found", "Error" return km = BlockingKernelManager(connection_file = fullpath) km.load_connection_file()
def _launch_kernel(cmd): """start an embedded kernel in a subprocess, and wait for it to be ready Returns ------- kernel, kernel_manager: Popen instance and connected KernelManager """ 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 + 5: time.sleep(0.1) if not os.path.exists(connection_file): if kernel.poll() is None: kernel.terminate() raise IOError("Connection file %r never arrived" % connection_file) if kernel.poll() is not None: raise IOError("Kernel failed to start") km = BlockingKernelManager(connection_file=connection_file) km.load_connection_file() km.start_channels() return kernel, km
def __init__(self, *args, **kwargs): connection_file = find_connection_file(kwargs.pop("connection_file")) km = BlockingKernelManager(connection_file=connection_file) km.load_connection_file() heartbeat = True km.start_channels(hb=heartbeat) atexit.register(km.cleanup_connection_file) super(IPythonConsoleShell, self).__init__(kernel_manager = km) self.km = km
def __init__(self, *args, **kwargs): connection_file = find_connection_file(kwargs.pop("connection_file")) km = BlockingKernelManager(connection_file=connection_file) km.load_connection_file() heartbeat = True km.start_channels(hb=heartbeat) atexit.register(km.cleanup_connection_file) super(IPythonConsoleShell, self).__init__(kernel_manager=km) self.km = km
class IPythonConnection(object): """ connects to a running IPython Kernel and can execute code there example usage: 1. start the IPython Kernel by typing:: ipython kernel [IPKernelApp] To connect another client to this kernel, use: [IPKernelApp] --existing kernel-4933.json or even more useful to see the output:: ipython qtconsol [IPKernelApp] To connect another client to this kernel, use: [IPKernelApp] --existing kernel-4933.json in a shell which will start the IPython Kernel and gives you information on how to connect to it, you will need that immedeately. 2. create a IPythonConnection with the value from before:: ipc = IPythonConnection(4933) 3. now you can execute code like:: ipc.run_cell('print("Hello World")') """ def __init__(self,connection): self.cf = find_connection_file(connection) self.km = BlockingKernelManager(connection_file=self.cf) self.km.load_connection_file() self.km.start_channels() def run_cell(self,code): #self.shell = self.km.shell_channel self.shell.execute(code)
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 + 10: 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) km = BlockingKernelManager(connection_file=connection_file) km.load_connection_file() km.start_channels() try: yield km finally: km.stop_channels() kernel.terminate()
# 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() socket = context.socket(zmq.REP)
" not specified", "Error") return km.start_channels() send = km.shell_channel.execute return km def get_child_msg(msg_id): # XXX: message handling should be split into its own process in the future while True: # get_msg will raise with Empty exception if no messages arrive in 1 second m = km.shell_channel.get_msg(timeout=1) if m['parent_header']['msg_id'] == msg_id: break else: #got a message, but not the one we were looking for print 'skipping a message on shell_channel', 'WarningMsg' return m if __name__ == "__main__": from IPython.lib.kernel import find_connection_file from IPython.zmq.blockingkernelmanager import BlockingKernelManager connection_file = os.environ["PMX_IPYTHON_CONNECTION"] print connection_file km = BlockingKernelManager(connection_file=connection_file) km.load_connection_file() km.start_channels() msg_id = km.shell_channel.execute("""a = 10""") print get_child_msg(msg_id)