Skip to content

Instantly share code, notes, and snippets.

@lesteve
Last active February 22, 2018 02:31
Show Gist options
  • Save lesteve/5052267 to your computer and use it in GitHub Desktop.
Save lesteve/5052267 to your computer and use it in GitHub Desktop.
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
try:
# 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
app.initialize()
app.start()
except Exception as e:
sys.stdout, sys.stderr = _stdout, _stderr
import traceback
traceback.print_exc()
t = threading.Thread(target=target, args=(app,))
t.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment