Skip to content

Commit

Permalink
Start IOPubThread at creation
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Feb 23, 2022
1 parent a32a71c commit f516a39
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 14 deletions.
1 change: 0 additions & 1 deletion ipykernel/inprocess/ipkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class InProcessKernel(IPythonKernel):
@default('iopub_thread')
def _default_iopub_thread(self):
thread = IOPubThread(self._underlying_iopub_socket)
thread.start()
return thread

iopub_socket = Instance(BackgroundSocket)
Expand Down
14 changes: 4 additions & 10 deletions ipykernel/iostream.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class IOPubThread:
"""

def __init__(self, socket, pipe=False):
"""Create IOPub thread
"""Create IOPub thread and start it
Parameters
----------
Expand Down Expand Up @@ -73,6 +73,9 @@ def __init__(self, socket, pipe=False):
self._events = deque()
self._event_pipes = WeakSet()
self._setup_event_pipe()
# make sure we don't prevent process exit
# I'm not sure why setting daemon=True above isn't enough, but it doesn't appear to be.
atexit.register(self.stop)

def _thread_main(self):
"""The inner loop that's actually run in a thread"""
Expand Down Expand Up @@ -177,14 +180,6 @@ def _check_mp_mode(self):
else:
return CHILD

def start(self):
"""Start the IOPub thread"""
self.thread.name = "IOPub"
self.thread.start()
# make sure we don't prevent process exit
# I'm not sure why setting daemon=True above isn't enough, but it doesn't appear to be.
atexit.register(self.stop)

def stop(self):
"""Stop the IOPub thread"""
if not self.thread.is_alive():
Expand Down Expand Up @@ -369,7 +364,6 @@ def __init__(
stacklevel=2,
)
pub_thread = IOPubThread(pub_thread)
pub_thread.start()
self.pub_thread = pub_thread
self.name = name
self.topic = b"stream." + name.encode()
Expand Down
2 changes: 1 addition & 1 deletion ipykernel/ipkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def set_sigint_result():
if sigint_future.cancelled() or sigint_future.done():
return
sigint_future.set_result(1)
# use call_soon_threadsage for thread safety
# use call_soon_threadsafe for thread safety
self.io_loop.call_soon_threadsafe(set_sigint_result)

# set the custom sigint hander during this context
Expand Down
2 changes: 0 additions & 2 deletions ipykernel/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def test_io_api():
ctx = zmq.Context()
pub = ctx.socket(zmq.PUB)
thread = IOPubThread(pub)
thread.start()

stream = OutStream(session, thread, 'stdout')

Expand Down Expand Up @@ -47,7 +46,6 @@ def test_io_isatty():
ctx = zmq.Context()
pub = ctx.socket(zmq.PUB)
thread = IOPubThread(pub)
thread.start()

stream = OutStream(session, thread, 'stdout', isatty=True)
assert stream.isatty()

0 comments on commit f516a39

Please sign in to comment.