public
Last active

Starting an ipython kernel outside the main thread

  • Download Gist
ipkernelapp_outside_main_thread.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.