Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions tools/daemon_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,19 @@

__all__ = ["DaemonThreadPoolExecutor"]

# CPython 3.14 reworked ThreadPoolExecutor internals: per-worker state
# (initializer/initargs) moved into a WorkerContext built by the new
# ``prepare_context()`` classmethod, and ``_worker`` now takes
# ``(executor_ref, ctx, work_queue)`` instead of
# ``(executor_ref, work_queue, initializer, initargs)``.
_HAS_WORKER_CONTEXT = hasattr(ThreadPoolExecutor, "prepare_context")


class DaemonThreadPoolExecutor(ThreadPoolExecutor):
"""ThreadPoolExecutor variant whose workers do not block process exit."""

def _adjust_thread_count(self) -> None:
# Mirrors CPython's implementation (3.8–3.13) with two changes:
# Mirrors CPython's implementation (3.8–3.14) with two changes:
# daemon=True and no _threads_queues registration.
if self._idle_semaphore.acquire(timeout=0):
return
Expand All @@ -49,15 +56,25 @@ def weakref_cb(_, q=self._work_queue):
num_threads = len(self._threads)
if num_threads < self._max_workers:
thread_name = "%s_%d" % (self._thread_name_prefix or self, num_threads)
t = threading.Thread(
name=thread_name,
target=_worker,
args=(
if _HAS_WORKER_CONTEXT:
# CPython 3.14+ worker signature.
worker_args = (
weakref.ref(self, weakref_cb),
self._create_worker_context(),
self._work_queue,
)
else:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a mocked regression test for this branch. CI currently supports Python <3.14, so the normal daemon-pool submission tests cannot execute this WorkerContext tuple.

# CPython 3.8–3.13 worker signature.
worker_args = (
weakref.ref(self, weakref_cb),
self._work_queue,
self._initializer,
self._initargs,
),
)
t = threading.Thread(
name=thread_name,
target=_worker,
args=worker_args,
daemon=True,
)
t.start()
Expand Down
Loading