Last active
February 22, 2018 02:31
-
-
Save lesteve/5052267 to your computer and use it in GitHub Desktop.
Starting an ipython kernel outside the main thread
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
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