Create a gist now

Instantly share code, notes, and snippets.

Starting an ipython kernel outside the main thread
import threading
import sys
import signal
# this is the heavy monkey-patching that actually works
# i.e. you can start the kernel fine and connect to it e.g. via
# ipython console --existing
# signal.signal = lambda *args, **kw: None
from IPython.zmq.ipkernel import IPKernelApp
app = IPKernelApp.instance()
def target(app):
_stdout, _stderr = sys.stdout, sys.stderr
# uncomment the next line and you can start the kernel fine
# outside of the main thread although trying to connect to it,
# e.g. via ipython console --existing get you
# ValueError: signal only works in main thread
# app.init_signal = lambda *args, **kw: None
except Exception as e:
sys.stdout, sys.stderr = _stdout, _stderr
import traceback
t = threading.Thread(target=target, args=(app,))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment