Skip to content

Commit 1e60a97

Browse files
committed
Start IOPubThread and ControlThread at creation
1 parent a32a71c commit 1e60a97

File tree

7 files changed

+7
-17
lines changed

7 files changed

+7
-17
lines changed

ipykernel/control.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def __init__(self, **kwargs):
1010
self.is_pydev_daemon_thread = True
1111
self.io_loop = None
1212
self.loop_ready = Event()
13+
self.start()
1314

1415
def run(self):
1516
self.name = "Control"

ipykernel/inprocess/ipkernel.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class InProcessKernel(IPythonKernel):
5757
@default('iopub_thread')
5858
def _default_iopub_thread(self):
5959
thread = IOPubThread(self._underlying_iopub_socket)
60-
thread.start()
6160
return thread
6261

6362
iopub_socket = Instance(BackgroundSocket)

ipykernel/iostream.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class IOPubThread:
4545
"""
4646

4747
def __init__(self, socket, pipe=False):
48-
"""Create IOPub thread
48+
"""Create IOPub thread and start it
4949
5050
Parameters
5151
----------
@@ -73,6 +73,9 @@ def __init__(self, socket, pipe=False):
7373
self._events = deque()
7474
self._event_pipes = WeakSet()
7575
self._setup_event_pipe()
76+
# make sure we don't prevent process exit
77+
# I'm not sure why setting daemon=True above isn't enough, but it doesn't appear to be.
78+
atexit.register(self.stop)
7679

7780
def _thread_main(self):
7881
"""The inner loop that's actually run in a thread"""
@@ -177,14 +180,6 @@ def _check_mp_mode(self):
177180
else:
178181
return CHILD
179182

180-
def start(self):
181-
"""Start the IOPub thread"""
182-
self.thread.name = "IOPub"
183-
self.thread.start()
184-
# make sure we don't prevent process exit
185-
# I'm not sure why setting daemon=True above isn't enough, but it doesn't appear to be.
186-
atexit.register(self.stop)
187-
188183
def stop(self):
189184
"""Stop the IOPub thread"""
190185
if not self.thread.is_alive():
@@ -369,7 +364,6 @@ def __init__(
369364
stacklevel=2,
370365
)
371366
pub_thread = IOPubThread(pub_thread)
372-
pub_thread.start()
373367
self.pub_thread = pub_thread
374368
self.name = name
375369
self.topic = b"stream." + name.encode()

ipykernel/ipkernel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def set_sigint_result():
282282
if sigint_future.cancelled() or sigint_future.done():
283283
return
284284
sigint_future.set_result(1)
285-
# use call_soon_threadsage for thread safety
285+
# use call_soon_threadsafe for thread safety
286286
self.io_loop.call_soon_threadsafe(set_sigint_result)
287287

288288
# set the custom sigint hander during this context

ipykernel/kernelapp.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,6 @@ def init_kernel(self):
491491
shell_stream = ZMQStream(self.shell_socket)
492492
control_stream = ZMQStream(self.control_socket, self.control_thread)
493493
debugpy_stream = ZMQStream(self.debugpy_socket, self.control_thread)
494-
self.control_thread.start()
495494
kernel_factory = self.kernel_class.instance
496495

497496
kernel = kernel_factory(parent=self, session=self.session,

ipykernel/kernelbase.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,8 +834,7 @@ async def shutdown_request(self, stream, ident, parent):
834834

835835
self.log.debug('Stopping control ioloop')
836836
control_io_loop = self.control_stream.io_loop
837-
if control_io_loop:
838-
control_io_loop.call_soon_threadsafe(control_io_loop.stop)
837+
control_io_loop.call_soon_threadsafe(control_io_loop.stop)
839838

840839
self.log.debug('Stopping shell ioloop')
841840
shell_io_loop = self.shell_stream.io_loop

ipykernel/tests/test_io.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def test_io_api():
1616
ctx = zmq.Context()
1717
pub = ctx.socket(zmq.PUB)
1818
thread = IOPubThread(pub)
19-
thread.start()
2019

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

@@ -47,7 +46,6 @@ def test_io_isatty():
4746
ctx = zmq.Context()
4847
pub = ctx.socket(zmq.PUB)
4948
thread = IOPubThread(pub)
50-
thread.start()
5149

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

0 commit comments

Comments
 (0)