thread.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # Copyright 2009 Brian Quinlan. All Rights Reserved.
  2. # Licensed to PSF under a Contributor Agreement.
  3. """Implements ThreadPoolExecutor."""
  4. import atexit
  5. from concurrent.futures import _base
  6. import itertools
  7. import Queue as queue
  8. import threading
  9. import weakref
  10. import sys
  11. try:
  12. from multiprocessing import cpu_count
  13. except ImportError:
  14. # some platforms don't have multiprocessing
  15. def cpu_count():
  16. return None
  17. __author__ = 'Brian Quinlan (brian@sweetapp.com)'
  18. # Workers are created as daemon threads. This is done to allow the interpreter
  19. # to exit when there are still idle threads in a ThreadPoolExecutor's thread
  20. # pool (i.e. shutdown() was not called). However, allowing workers to die with
  21. # the interpreter has two undesirable properties:
  22. # - The workers would still be running during interpreter shutdown,
  23. # meaning that they would fail in unpredictable ways.
  24. # - The workers could be killed while evaluating a work item, which could
  25. # be bad if the callable being evaluated has external side-effects e.g.
  26. # writing to a file.
  27. #
  28. # To work around this problem, an exit handler is installed which tells the
  29. # workers to exit when their work queues are empty and then waits until the
  30. # threads finish.
  31. _threads_queues = weakref.WeakKeyDictionary()
  32. _shutdown = False
  33. def _python_exit():
  34. global _shutdown
  35. _shutdown = True
  36. items = list(_threads_queues.items()) if _threads_queues else ()
  37. for t, q in items:
  38. q.put(None)
  39. for t, q in items:
  40. t.join(sys.maxint)
  41. atexit.register(_python_exit)
  42. class _WorkItem(object):
  43. def __init__(self, future, fn, args, kwargs):
  44. self.future = future
  45. self.fn = fn
  46. self.args = args
  47. self.kwargs = kwargs
  48. def run(self):
  49. if not self.future.set_running_or_notify_cancel():
  50. return
  51. try:
  52. result = self.fn(*self.args, **self.kwargs)
  53. except:
  54. e, tb = sys.exc_info()[1:]
  55. self.future.set_exception_info(e, tb)
  56. else:
  57. self.future.set_result(result)
  58. def _worker(executor_reference, work_queue, initializer, initargs):
  59. if initializer is not None:
  60. try:
  61. initializer(*initargs)
  62. except BaseException:
  63. _base.LOGGER.critical('Exception in initializer:', exc_info=True)
  64. executor = executor_reference()
  65. if executor is not None:
  66. executor._initializer_failed()
  67. return
  68. try:
  69. while True:
  70. work_item = work_queue.get(block=True)
  71. if work_item is not None:
  72. work_item.run()
  73. # Delete references to object. See issue16284
  74. del work_item
  75. # attempt to increment idle count
  76. executor = executor_reference()
  77. if executor is not None:
  78. executor._idle_semaphore.release()
  79. del executor
  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. # Notice other workers
  88. work_queue.put(None)
  89. return
  90. del executor
  91. except:
  92. _base.LOGGER.critical('Exception in worker', exc_info=True)
  93. class BrokenThreadPool(_base.BrokenExecutor):
  94. """
  95. Raised when a worker thread in a ThreadPoolExecutor failed initializing.
  96. """
  97. class ThreadPoolExecutor(_base.Executor):
  98. # Used to assign unique thread names when thread_name_prefix is not supplied.
  99. _counter = itertools.count().next
  100. def __init__(self, max_workers=None, thread_name_prefix='', initializer=None, initargs=()):
  101. """Initializes a new ThreadPoolExecutor instance.
  102. Args:
  103. max_workers: The maximum number of threads that can be used to
  104. execute the given calls.
  105. thread_name_prefix: An optional name prefix to give our threads.
  106. """
  107. if max_workers is None:
  108. # Use this number because ThreadPoolExecutor is often
  109. # used to overlap I/O instead of CPU work.
  110. max_workers = (cpu_count() or 1) * 5
  111. if max_workers <= 0:
  112. raise ValueError("max_workers must be greater than 0")
  113. self._max_workers = max_workers
  114. self._initializer = initializer
  115. self._initargs = initargs
  116. self._work_queue = queue.Queue()
  117. self._idle_semaphore = threading.Semaphore(0)
  118. self._threads = set()
  119. self._broken = False
  120. self._shutdown = False
  121. self._shutdown_lock = threading.Lock()
  122. self._thread_name_prefix = (thread_name_prefix or
  123. ("ThreadPoolExecutor-%d" % self._counter()))
  124. def submit(self, fn, *args, **kwargs):
  125. with self._shutdown_lock:
  126. if self._broken:
  127. raise BrokenThreadPool(self._broken)
  128. if self._shutdown:
  129. raise RuntimeError('cannot schedule new futures after shutdown')
  130. f = _base.Future()
  131. w = _WorkItem(f, fn, args, kwargs)
  132. self._work_queue.put(w)
  133. self._adjust_thread_count()
  134. return f
  135. submit.__doc__ = _base.Executor.submit.__doc__
  136. def _adjust_thread_count(self):
  137. # if idle threads are available, don't spin new threads
  138. if self._idle_semaphore.acquire(False):
  139. return
  140. # When the executor gets lost, the weakref callback will wake up
  141. # the worker threads.
  142. def weakref_cb(_, q=self._work_queue):
  143. q.put(None)
  144. num_threads = len(self._threads)
  145. if num_threads < self._max_workers:
  146. thread_name = '%s_%d' % (self._thread_name_prefix or self,
  147. num_threads)
  148. t = threading.Thread(name=thread_name, target=_worker,
  149. args=(weakref.ref(self, weakref_cb),
  150. self._work_queue, self._initializer, self._initargs))
  151. t.daemon = True
  152. t.start()
  153. self._threads.add(t)
  154. _threads_queues[t] = self._work_queue
  155. def _initializer_failed(self):
  156. with self._shutdown_lock:
  157. self._broken = ('A thread initializer failed, the thread pool '
  158. 'is not usable anymore')
  159. # Drain work queue and mark pending futures failed
  160. while True:
  161. try:
  162. work_item = self._work_queue.get_nowait()
  163. except queue.Empty:
  164. break
  165. if work_item is not None:
  166. work_item.future.set_exception(BrokenThreadPool(self._broken))
  167. def shutdown(self, wait=True):
  168. with self._shutdown_lock:
  169. self._shutdown = True
  170. self._work_queue.put(None)
  171. if wait:
  172. for t in self._threads:
  173. t.join(sys.maxint)
  174. shutdown.__doc__ = _base.Executor.shutdown.__doc__