Skip to content

Commit

Permalink
Start IOPubThread and ControlThread 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 1e60a97
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 17 deletions.
1 change: 1 addition & 0 deletions ipykernel/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def __init__(self, **kwargs):
self.is_pydev_daemon_thread = True
self.io_loop = None
self.loop_ready = Event()
self.start()

def run(self):
self.name = "Control"
Expand Down
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
1 change: 0 additions & 1 deletion ipykernel/kernelapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,6 @@ def init_kernel(self):
shell_stream = ZMQStream(self.shell_socket)
control_stream = ZMQStream(self.control_socket, self.control_thread)
debugpy_stream = ZMQStream(self.debugpy_socket, self.control_thread)
self.control_thread.start()
kernel_factory = self.kernel_class.instance

kernel = kernel_factory(parent=self, session=self.session,
Expand Down
3 changes: 1 addition & 2 deletions ipykernel/kernelbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,8 +834,7 @@ async def shutdown_request(self, stream, ident, parent):

self.log.debug('Stopping control ioloop')
control_io_loop = self.control_stream.io_loop
if control_io_loop:
control_io_loop.call_soon_threadsafe(control_io_loop.stop)
control_io_loop.call_soon_threadsafe(control_io_loop.stop)

self.log.debug('Stopping shell ioloop')
shell_io_loop = self.shell_stream.io_loop
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 1e60a97

Please sign in to comment.