Created
August 15, 2013 01:47
-
-
Save robo-corg/6237530 to your computer and use it in GitHub Desktop.
Returns a function that can used to manually pump IPython events to the embedded kernel instead of starting a blocking event-loop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from IPython.kernel.zmq.kernelapp import IPKernelApp | |
from IPython.utils.frame import extract_module_locals | |
import sys | |
# Code lifted from IPython/kernel/zmq/embed.py | |
def embed_kernel_noloop(module=None, local_ns=None, **kwargs): | |
"""Embed and start an IPython kernel in a given scope | |
returning function that can be used to pump a single event | |
Parameters | |
---------- | |
module : ModuleType, optional | |
The module to load into IPython globals (default: caller) | |
local_ns : dict, optional | |
The namespace to load into IPython user namespace (default: caller) | |
kwargs : various, optional | |
Further keyword args are relayed to the IPKernelApp constructor, | |
allowing configuration of the Kernel. Will only have an effect | |
on the first embed_kernel call for a given process. | |
""" | |
# get the app if it exists, or set it up if it doesn't | |
if IPKernelApp.initialized(): | |
app = IPKernelApp.instance() | |
else: | |
app = IPKernelApp.instance(**kwargs) | |
app.initialize([]) | |
# Undo unnecessary sys module mangling from init_sys_modules. | |
# This would not be necessary if we could prevent it | |
# in the first place by using a different InteractiveShell | |
# subclass, as in the regular embed case. | |
main = app.kernel.shell._orig_sys_modules_main_mod | |
if main is not None: | |
sys.modules[app.kernel.shell._orig_sys_modules_main_name] = main | |
# load the calling scope if not given | |
(caller_module, caller_locals) = extract_module_locals(1) | |
if module is None: | |
module = caller_module | |
if local_ns is None: | |
local_ns = caller_locals | |
app.kernel.user_module = module | |
app.kernel.user_ns = local_ns | |
app.shell.set_completer_frame() | |
# Needed to setup zmq dispatchers | |
app.kernel.start() | |
# Simply return bound do_one_iteration method to simplify access pattern | |
return app.kernel.do_one_iteration | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment