diff --git a/ipykernel/control.py b/ipykernel/control.py index a4e515a58..ac66dc8cb 100644 --- a/ipykernel/control.py +++ b/ipykernel/control.py @@ -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" diff --git a/ipykernel/inprocess/ipkernel.py b/ipykernel/inprocess/ipkernel.py index bc17236d6..515fc06ac 100644 --- a/ipykernel/inprocess/ipkernel.py +++ b/ipykernel/inprocess/ipkernel.py @@ -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) diff --git a/ipykernel/iostream.py b/ipykernel/iostream.py index 1463a9eca..221c036eb 100644 --- a/ipykernel/iostream.py +++ b/ipykernel/iostream.py @@ -45,7 +45,7 @@ class IOPubThread: """ def __init__(self, socket, pipe=False): - """Create IOPub thread + """Create IOPub thread and start it Parameters ---------- @@ -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""" @@ -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(): @@ -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() diff --git a/ipykernel/ipkernel.py b/ipykernel/ipkernel.py index 3b8092f48..7411ac36c 100644 --- a/ipykernel/ipkernel.py +++ b/ipykernel/ipkernel.py @@ -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 diff --git a/ipykernel/kernelapp.py b/ipykernel/kernelapp.py index 59940e720..cd5a38f1b 100644 --- a/ipykernel/kernelapp.py +++ b/ipykernel/kernelapp.py @@ -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, diff --git a/ipykernel/kernelbase.py b/ipykernel/kernelbase.py index e0970ccdf..e3b00f29b 100644 --- a/ipykernel/kernelbase.py +++ b/ipykernel/kernelbase.py @@ -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 diff --git a/ipykernel/tests/test_io.py b/ipykernel/tests/test_io.py index cad617879..616871417 100644 --- a/ipykernel/tests/test_io.py +++ b/ipykernel/tests/test_io.py @@ -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') @@ -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()