daemon_pool.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """Shared daemon-thread ThreadPoolExecutor.
  2. Stdlib workers are non-daemon AND registered in ``_threads_queues``, whose atexit
  3. hook joins every worker even after ``shutdown(wait=False)`` — one wedged worker
  4. (tool blocked on network I/O, hung provider, stuck subagent) blocks interpreter
  5. exit forever. This variant spawns daemon workers and skips that registration.
  6. Use it for best-effort/interruptible work that must never hold the process open;
  7. NOT for work that must complete before exit (durable writes belong on foreground
  8. threads with explicit bounded joins).
  9. """
  10. from __future__ import annotations
  11. import threading
  12. import weakref
  13. from concurrent.futures import ThreadPoolExecutor
  14. from concurrent.futures.thread import _worker
  15. from contextvars import copy_context
  16. __all__ = ["DaemonThreadPoolExecutor"]
  17. class DaemonThreadPoolExecutor(ThreadPoolExecutor):
  18. """ThreadPoolExecutor variant whose workers do not block process exit."""
  19. def submit(self, fn, /, *args, **kwargs):
  20. """Keep each task in its caller's profile scope, even on a reused worker.
  21. Thread-start context cannot track later submissions from other profiles
  22. (#54937). The stdlib worker context manages initialization, not contextvars.
  23. """
  24. ctx = copy_context()
  25. def _run_with_context(*call_args, **call_kwargs):
  26. return ctx.run(fn, *call_args, **call_kwargs)
  27. return super().submit(_run_with_context, *args, **kwargs)
  28. def _adjust_thread_count(self) -> None:
  29. # Mirrors CPython's implementation with two changes:
  30. # daemon=True and no _threads_queues registration.
  31. if self._idle_semaphore.acquire(timeout=0):
  32. return
  33. def weakref_cb(_, q=self._work_queue):
  34. q.put(None)
  35. num_threads = len(self._threads)
  36. if num_threads < self._max_workers:
  37. thread_name = f'{self._thread_name_prefix or self}_{num_threads:d}'
  38. executor_ref = weakref.ref(self, weakref_cb)
  39. if hasattr(self, "_create_worker_context"):
  40. # Python 3.14 replaced _initializer/_initargs with a factory
  41. # that supplies the worker's initializer context.
  42. worker_args = (
  43. executor_ref,
  44. self._create_worker_context(),
  45. self._work_queue,
  46. )
  47. else:
  48. worker_args = (
  49. executor_ref,
  50. self._work_queue,
  51. self._initializer,
  52. self._initargs,
  53. )
  54. t = threading.Thread(
  55. name=thread_name, target=_worker, daemon=True,
  56. args=worker_args,
  57. )
  58. t.start()
  59. self._threads.add(t)