Skip to content

Commit 1834b58

Browse files
committed
Replace _IOPubThread with BaseThread
1 parent 615ec12 commit 1834b58

File tree

3 files changed

+19
-39
lines changed

3 files changed

+19
-39
lines changed

ipykernel/inprocess/socket.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,8 @@ async def poll(self, timeout=0):
6565
return statistics.current_buffer_used != 0
6666

6767
def close(self):
68-
pass
68+
if self.is_shell:
69+
self.in_send_stream.close()
70+
self.in_receive_stream.close()
71+
self.out_send_stream.close()
72+
self.out_receive_stream.close()

ipykernel/iostream.py

+9-37
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
from binascii import b2a_hex
1717
from collections import defaultdict, deque
1818
from io import StringIO, TextIOBase
19-
from threading import Event, Thread, local
19+
from threading import local
2020
from typing import Any, Callable
2121

2222
import zmq
2323
import zmq_anyio
24-
from anyio import create_task_group, run, sleep, to_thread
24+
from anyio import sleep
2525
from jupyter_client.session import extract_header
2626

27+
from .thread import BaseThread
28+
2729
# -----------------------------------------------------------------------------
2830
# Globals
2931
# -----------------------------------------------------------------------------
@@ -38,38 +40,6 @@
3840
# -----------------------------------------------------------------------------
3941

4042

41-
class _IOPubThread(Thread):
42-
"""A thread for a IOPub."""
43-
44-
def __init__(self, tasks, **kwargs):
45-
"""Initialize the thread."""
46-
super().__init__(name="IOPub", **kwargs)
47-
self._tasks = tasks
48-
self.pydev_do_not_trace = True
49-
self.is_pydev_daemon_thread = True
50-
self.daemon = True
51-
self.__stop = Event()
52-
53-
def run(self):
54-
"""Run the thread."""
55-
self.name = "IOPub"
56-
run(self._main)
57-
58-
async def _main(self):
59-
async with create_task_group() as self._task_group:
60-
for task in self._tasks:
61-
self._task_group.start_soon(task)
62-
await to_thread.run_sync(self.__stop.wait)
63-
self._task_group.cancel_scope.cancel()
64-
65-
def stop(self):
66-
"""Stop the thread.
67-
68-
This method is threadsafe.
69-
"""
70-
self.__stop.set()
71-
72-
7343
class IOPubThread:
7444
"""An object for sending IOPub messages in a background thread
7545
@@ -109,7 +79,9 @@ def __init__(self, socket: zmq_anyio.Socket, pipe=False):
10979
tasks = [self._handle_event, self._run_event_pipe_gc, self.socket.start]
11080
if pipe:
11181
tasks.append(self._handle_pipe_msgs)
112-
self.thread = _IOPubThread(tasks)
82+
self.thread = BaseThread(name="IOPub", daemon=True)
83+
for task in tasks:
84+
self.thread.start_soon(task)
11385

11486
def _setup_event_pipe(self):
11587
"""Create the PULL socket listening for events that should fire in this thread."""
@@ -179,7 +151,7 @@ async def _handle_event(self):
179151
event_f = self._events.popleft()
180152
event_f()
181153
except Exception:
182-
if self.thread.__stop.is_set():
154+
if self.thread.stopped.is_set():
183155
return
184156
raise
185157

@@ -211,7 +183,7 @@ async def _handle_pipe_msgs(self):
211183
while True:
212184
await self._handle_pipe_msg()
213185
except Exception:
214-
if self.thread.__stop.is_set():
186+
if self.thread.stopped.is_set():
215187
return
216188
raise
217189

ipykernel/thread.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from collections.abc import Awaitable
55
from queue import Queue
6-
from threading import Thread
6+
from threading import Event, Thread
77
from typing import Callable
88

99
from anyio import create_task_group, run, to_thread
@@ -18,6 +18,8 @@ class BaseThread(Thread):
1818
def __init__(self, **kwargs):
1919
"""Initialize the thread."""
2020
super().__init__(**kwargs)
21+
self.started = Event()
22+
self.stopped = Event()
2123
self.pydev_do_not_trace = True
2224
self.is_pydev_daemon_thread = True
2325
self._tasks: Queue[Callable[[], Awaitable[None]] | None] = Queue()
@@ -31,6 +33,7 @@ def run(self) -> None:
3133

3234
async def _main(self) -> None:
3335
async with create_task_group() as tg:
36+
self.started.set()
3437
while True:
3538
task = await to_thread.run_sync(self._tasks.get)
3639
if task is None:
@@ -44,3 +47,4 @@ def stop(self) -> None:
4447
This method is threadsafe.
4548
"""
4649
self._tasks.put(None)
50+
self.stopped.set()

0 commit comments

Comments
 (0)