|
16 | 16 | from binascii import b2a_hex |
17 | 17 | from collections import defaultdict, deque |
18 | 18 | from io import StringIO, TextIOBase |
19 | | -from threading import Event, Thread, local |
| 19 | +from threading import local |
20 | 20 | from typing import Any, Callable |
21 | 21 |
|
22 | 22 | import zmq |
23 | 23 | import zmq_anyio |
24 | | -from anyio import create_task_group, run, sleep, to_thread |
| 24 | +from anyio import sleep |
25 | 25 | from jupyter_client.session import extract_header |
26 | 26 |
|
| 27 | +from .thread import BaseThread |
| 28 | + |
27 | 29 | # ----------------------------------------------------------------------------- |
28 | 30 | # Globals |
29 | 31 | # ----------------------------------------------------------------------------- |
|
38 | 40 | # ----------------------------------------------------------------------------- |
39 | 41 |
|
40 | 42 |
|
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 | | - |
73 | 43 | class IOPubThread: |
74 | 44 | """An object for sending IOPub messages in a background thread |
75 | 45 |
|
@@ -109,7 +79,9 @@ def __init__(self, socket: zmq_anyio.Socket, pipe=False): |
109 | 79 | tasks = [self._handle_event, self._run_event_pipe_gc, self.socket.start] |
110 | 80 | if pipe: |
111 | 81 | 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) |
113 | 85 |
|
114 | 86 | def _setup_event_pipe(self): |
115 | 87 | """Create the PULL socket listening for events that should fire in this thread.""" |
@@ -179,7 +151,7 @@ async def _handle_event(self): |
179 | 151 | event_f = self._events.popleft() |
180 | 152 | event_f() |
181 | 153 | except Exception: |
182 | | - if self.thread.__stop.is_set(): |
| 154 | + if self.thread.stopped.is_set(): |
183 | 155 | return |
184 | 156 | raise |
185 | 157 |
|
@@ -211,7 +183,7 @@ async def _handle_pipe_msgs(self): |
211 | 183 | while True: |
212 | 184 | await self._handle_pipe_msg() |
213 | 185 | except Exception: |
214 | | - if self.thread.__stop.is_set(): |
| 186 | + if self.thread.stopped.is_set(): |
215 | 187 | return |
216 | 188 | raise |
217 | 189 |
|
|
0 commit comments