thread.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # Copyright 2009 Brian Quinlan. All Rights Reserved.
  2. # Licensed to PSF under a Contributor Agreement.
  3. """Implements ThreadPoolExecutor."""
  4. __author__ = 'Brian Quinlan (brian@sweetapp.com)'
  5. from concurrent.futures import _base
  6. import itertools
  7. import queue
  8. import threading
  9. import types
  10. import weakref
  11. import os
  12. _threads_queues = weakref.WeakKeyDictionary()
  13. _shutdown = False
  14. # Lock that ensures that new workers are not created while the interpreter is
  15. # shutting down. Must be held while mutating _threads_queues and _shutdown.
  16. _global_shutdown_lock = threading.Lock()
  17. def _python_exit():
  18. global _shutdown
  19. with _global_shutdown_lock:
  20. _shutdown = True
  21. items = list(_threads_queues.items())
  22. for t, q in items:
  23. q.put(None)
  24. for t, q in items:
  25. t.join()
  26. # Register for `_python_exit()` to be called just before joining all
  27. # non-daemon threads. This is used instead of `atexit.register()` for
  28. # compatibility with subinterpreters, which no longer support daemon threads.
  29. # See bpo-39812 for context.
  30. threading._register_atexit(_python_exit)
  31. # At fork, reinitialize the `_global_shutdown_lock` lock in the child process
  32. if hasattr(os, 'register_at_fork'):
  33. os.register_at_fork(before=_global_shutdown_lock.acquire,
  34. after_in_child=_global_shutdown_lock._at_fork_reinit,
  35. after_in_parent=_global_shutdown_lock.release)
  36. os.register_at_fork(after_in_child=_threads_queues.clear)
  37. class _WorkItem:
  38. def __init__(self, future, fn, args, kwargs):
  39. self.future = future
  40. self.fn = fn
  41. self.args = args
  42. self.kwargs = kwargs
  43. def run(self):
  44. if not self.future.set_running_or_notify_cancel():
  45. return
  46. try:
  47. result = self.fn(*self.args, **self.kwargs)
  48. except BaseException as exc:
  49. self.future.set_exception(exc)
  50. # Break a reference cycle with the exception 'exc'
  51. self = None
  52. else:
  53. self.future.set_result(result)
  54. __class_getitem__ = classmethod(types.GenericAlias)
  55. def _worker(executor_reference, work_queue, initializer, initargs):
  56. if initializer is not None:
  57. try:
  58. initializer(*initargs)
  59. except BaseException:
  60. _base.LOGGER.critical('Exception in initializer:', exc_info=True)
  61. executor = executor_reference()
  62. if executor is not None:
  63. executor._initializer_failed()
  64. return
  65. try:
  66. while True:
  67. try:
  68. work_item = work_queue.get_nowait()
  69. except queue.Empty:
  70. # attempt to increment idle count if queue is empty
  71. executor = executor_reference()
  72. if executor is not None:
  73. executor._idle_semaphore.release()
  74. del executor
  75. work_item = work_queue.get(block=True)
  76. if work_item is not None:
  77. work_item.run()
  78. # Delete references to object. See GH-60488
  79. del work_item
  80. continue
  81. executor = executor_reference()
  82. # Exit if:
  83. # - The interpreter is shutting down OR
  84. # - The executor that owns the worker has been collected OR
  85. # - The executor that owns the worker has been shutdown.
  86. if _shutdown or executor is None or executor._shutdown:
  87. # Flag the executor as shutting down as early as possible if it
  88. # is not gc-ed yet.
  89. if executor is not None:
  90. executor._shutdown = True
  91. # Notice other workers
  92. work_queue.put(None)
  93. return
  94. del executor
  95. except BaseException:
  96. _base.LOGGER.critical('Exception in worker', exc_info=True)
  97. class BrokenThreadPool(_base.BrokenExecutor):
  98. """
  99. Raised when a worker thread in a ThreadPoolExecutor failed initializing.
  100. """
  101. class ThreadPoolExecutor(_base.Executor):
  102. # Used to assign unique thread names when thread_name_prefix is not supplied.
  103. _counter = itertools.count().__next__
  104. def __init__(self, max_workers=None, thread_name_prefix='',
  105. initializer=None, initargs=()):
  106. """Initializes a new ThreadPoolExecutor instance.
  107. Args:
  108. max_workers: The maximum number of threads that can be used to
  109. execute the given calls.
  110. thread_name_prefix: An optional name prefix to give our threads.
  111. initializer: A callable used to initialize worker threads.
  112. initargs: A tuple of arguments to pass to the initializer.
  113. """
  114. if max_workers is None:
  115. # ThreadPoolExecutor is often used to:
  116. # * CPU bound task which releases GIL
  117. # * I/O bound task (which releases GIL, of course)
  118. #
  119. # We use cpu_count + 4 for both types of tasks.
  120. # But we limit it to 32 to avoid consuming surprisingly large resource
  121. # on many core machine.
  122. max_workers = min(32, (os.cpu_count() or 1) + 4)
  123. if max_workers <= 0:
  124. raise ValueError("max_workers must be greater than 0")
  125. if initializer is not None and not callable(initializer):
  126. raise TypeError("initializer must be a callable")
  127. self._max_workers = max_workers
  128. self._work_queue = queue.SimpleQueue()
  129. self._idle_semaphore = threading.Semaphore(0)
  130. self._threads = set()
  131. self._broken = False
  132. self._shutdown = False
  133. self._shutdown_lock = threading.Lock()
  134. self._thread_name_prefix = (thread_name_prefix or
  135. ("ThreadPoolExecutor-%d" % self._counter()))
  136. self._initializer = initializer
  137. self._initargs = initargs
  138. def submit(self, fn, /, *args, **kwargs):
  139. with self._shutdown_lock, _global_shutdown_lock:
  140. if self._broken:
  141. raise BrokenThreadPool(self._broken)
  142. if self._shutdown:
  143. raise RuntimeError('cannot schedule new futures after shutdown')
  144. if _shutdown:
  145. raise RuntimeError('cannot schedule new futures after '
  146. 'interpreter shutdown')
  147. f = _base.Future()
  148. w = _WorkItem(f, fn, args, kwargs)
  149. self._work_queue.put(w)
  150. self._adjust_thread_count()
  151. return f
  152. submit.__doc__ = _base.Executor.submit.__doc__
  153. def _adjust_thread_count(self):
  154. # if idle threads are available, don't spin new threads
  155. if self._idle_semaphore.acquire(timeout=0):
  156. return
  157. # When the executor gets lost, the weakref callback will wake up
  158. # the worker threads.
  159. def weakref_cb(_, q=self._work_queue):
  160. q.put(None)
  161. num_threads = len(self._threads)
  162. if num_threads < self._max_workers:
  163. thread_name = '%s_%d' % (self._thread_name_prefix or self,
  164. num_threads)
  165. t = threading.Thread(name=thread_name, target=_worker,
  166. args=(weakref.ref(self, weakref_cb),
  167. self._work_queue,
  168. self._initializer,
  169. self._initargs))
  170. t.start()
  171. self._threads.add(t)
  172. _threads_queues[t] = self._work_queue
  173. def _initializer_failed(self):
  174. with self._shutdown_lock:
  175. self._broken = ('A thread initializer failed, the thread pool '
  176. 'is not usable anymore')
  177. # Drain work queue and mark pending futures failed
  178. while True:
  179. try:
  180. work_item = self._work_queue.get_nowait()
  181. except queue.Empty:
  182. break
  183. if work_item is not None:
  184. work_item.future.set_exception(BrokenThreadPool(self._broken))
  185. def shutdown(self, wait=True, *, cancel_futures=False):
  186. with self._shutdown_lock:
  187. self._shutdown = True
  188. if cancel_futures:
  189. # Drain all work items from the queue, and then cancel their
  190. # associated futures.
  191. while True:
  192. try:
  193. work_item = self._work_queue.get_nowait()
  194. except queue.Empty:
  195. break
  196. if work_item is not None:
  197. work_item.future.cancel()
  198. # Send a wake-up to prevent threads calling
  199. # _work_queue.get(block=True) from permanently blocking.
  200. self._work_queue.put(None)
  201. if wait:
  202. for t in self._threads:
  203. t.join()
  204. shutdown.__doc__ = _base.Executor.shutdown.__doc__