Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1 | # Copyright 2009 Brian Quinlan. All Rights Reserved. |
| 2 | # Licensed to PSF under a Contributor Agreement. |
| 3 | |
| 4 | """Implements ThreadPoolExecutor.""" |
| 5 | |
| 6 | __author__ = 'Brian Quinlan (brian@sweetapp.com)' |
| 7 | |
| 8 | import atexit |
| 9 | from concurrent.futures import _base |
Gregory P. Smith | a3d91b4 | 2017-06-21 23:41:13 -0700 | [diff] [blame] | 10 | import itertools |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 11 | import queue |
| 12 | import threading |
| 13 | import weakref |
Guido van Rossum | cfd4661 | 2014-09-02 10:39:18 -0700 | [diff] [blame] | 14 | import os |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 15 | |
| 16 | # Workers are created as daemon threads. This is done to allow the interpreter |
| 17 | # to exit when there are still idle threads in a ThreadPoolExecutor's thread |
| 18 | # pool (i.e. shutdown() was not called). However, allowing workers to die with |
| 19 | # the interpreter has two undesirable properties: |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 20 | # - The workers would still be running during interpreter shutdown, |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 21 | # meaning that they would fail in unpredictable ways. |
| 22 | # - The workers could be killed while evaluating a work item, which could |
| 23 | # be bad if the callable being evaluated has external side-effects e.g. |
| 24 | # writing to a file. |
| 25 | # |
| 26 | # To work around this problem, an exit handler is installed which tells the |
| 27 | # workers to exit when their work queues are empty and then waits until the |
| 28 | # threads finish. |
| 29 | |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 30 | _threads_queues = weakref.WeakKeyDictionary() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 31 | _shutdown = False |
| 32 | |
| 33 | def _python_exit(): |
| 34 | global _shutdown |
| 35 | _shutdown = True |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 36 | items = list(_threads_queues.items()) |
| 37 | for t, q in items: |
| 38 | q.put(None) |
| 39 | for t, q in items: |
| 40 | t.join() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 41 | |
| 42 | atexit.register(_python_exit) |
| 43 | |
| 44 | class _WorkItem(object): |
| 45 | def __init__(self, future, fn, args, kwargs): |
| 46 | self.future = future |
| 47 | self.fn = fn |
| 48 | self.args = args |
| 49 | self.kwargs = kwargs |
| 50 | |
| 51 | def run(self): |
| 52 | if not self.future.set_running_or_notify_cancel(): |
| 53 | return |
| 54 | |
| 55 | try: |
| 56 | result = self.fn(*self.args, **self.kwargs) |
Victor Stinner | bc61315 | 2017-08-22 16:50:42 +0200 | [diff] [blame] | 57 | except BaseException as exc: |
| 58 | self.future.set_exception(exc) |
| 59 | # Break a reference cycle with the exception 'exc' |
| 60 | self = None |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 61 | else: |
| 62 | self.future.set_result(result) |
| 63 | |
| 64 | def _worker(executor_reference, work_queue): |
| 65 | try: |
| 66 | while True: |
Antoine Pitrou | 27be5da | 2011-04-12 17:48:46 +0200 | [diff] [blame] | 67 | work_item = work_queue.get(block=True) |
| 68 | if work_item is not None: |
| 69 | work_item.run() |
Andrew Svetlov | 6b97374 | 2012-11-03 15:36:01 +0200 | [diff] [blame] | 70 | # Delete references to object. See issue16284 |
| 71 | del work_item |
Antoine Pitrou | 27be5da | 2011-04-12 17:48:46 +0200 | [diff] [blame] | 72 | continue |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 73 | executor = executor_reference() |
| 74 | # Exit if: |
| 75 | # - The interpreter is shutting down OR |
| 76 | # - The executor that owns the worker has been collected OR |
| 77 | # - The executor that owns the worker has been shutdown. |
| 78 | if _shutdown or executor is None or executor._shutdown: |
| 79 | # Notice other workers |
| 80 | work_queue.put(None) |
| 81 | return |
| 82 | del executor |
Florent Xicluna | 04842a8 | 2011-11-11 20:05:50 +0100 | [diff] [blame] | 83 | except BaseException: |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 84 | _base.LOGGER.critical('Exception in worker', exc_info=True) |
| 85 | |
| 86 | class ThreadPoolExecutor(_base.Executor): |
Gregory P. Smith | a3d91b4 | 2017-06-21 23:41:13 -0700 | [diff] [blame] | 87 | |
| 88 | # Used to assign unique thread names when thread_name_prefix is not supplied. |
| 89 | _counter = itertools.count().__next__ |
| 90 | |
Gregory P. Smith | 50abe87 | 2016-08-07 10:19:20 -0700 | [diff] [blame] | 91 | def __init__(self, max_workers=None, thread_name_prefix=''): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 92 | """Initializes a new ThreadPoolExecutor instance. |
| 93 | |
| 94 | Args: |
| 95 | max_workers: The maximum number of threads that can be used to |
| 96 | execute the given calls. |
Gregory P. Smith | 50abe87 | 2016-08-07 10:19:20 -0700 | [diff] [blame] | 97 | thread_name_prefix: An optional name prefix to give our threads. |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 98 | """ |
Guido van Rossum | cfd4661 | 2014-09-02 10:39:18 -0700 | [diff] [blame] | 99 | if max_workers is None: |
| 100 | # Use this number because ThreadPoolExecutor is often |
| 101 | # used to overlap I/O instead of CPU work. |
| 102 | max_workers = (os.cpu_count() or 1) * 5 |
Brian Quinlan | 20efceb | 2014-05-17 13:51:10 -0700 | [diff] [blame] | 103 | if max_workers <= 0: |
| 104 | raise ValueError("max_workers must be greater than 0") |
| 105 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 106 | self._max_workers = max_workers |
| 107 | self._work_queue = queue.Queue() |
| 108 | self._threads = set() |
| 109 | self._shutdown = False |
| 110 | self._shutdown_lock = threading.Lock() |
Gregory P. Smith | a3d91b4 | 2017-06-21 23:41:13 -0700 | [diff] [blame] | 111 | self._thread_name_prefix = (thread_name_prefix or |
| 112 | ("ThreadPoolExecutor-%d" % self._counter())) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 113 | |
| 114 | def submit(self, fn, *args, **kwargs): |
| 115 | with self._shutdown_lock: |
| 116 | if self._shutdown: |
| 117 | raise RuntimeError('cannot schedule new futures after shutdown') |
| 118 | |
| 119 | f = _base.Future() |
| 120 | w = _WorkItem(f, fn, args, kwargs) |
| 121 | |
| 122 | self._work_queue.put(w) |
| 123 | self._adjust_thread_count() |
| 124 | return f |
| 125 | submit.__doc__ = _base.Executor.submit.__doc__ |
| 126 | |
| 127 | def _adjust_thread_count(self): |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 128 | # When the executor gets lost, the weakref callback will wake up |
| 129 | # the worker threads. |
| 130 | def weakref_cb(_, q=self._work_queue): |
| 131 | q.put(None) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 132 | # TODO(bquinlan): Should avoid creating new threads if there are more |
| 133 | # idle threads than items in the work queue. |
Gregory P. Smith | 50abe87 | 2016-08-07 10:19:20 -0700 | [diff] [blame] | 134 | num_threads = len(self._threads) |
| 135 | if num_threads < self._max_workers: |
| 136 | thread_name = '%s_%d' % (self._thread_name_prefix or self, |
| 137 | num_threads) |
| 138 | t = threading.Thread(name=thread_name, target=_worker, |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 139 | args=(weakref.ref(self, weakref_cb), |
| 140 | self._work_queue)) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 141 | t.daemon = True |
| 142 | t.start() |
| 143 | self._threads.add(t) |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 144 | _threads_queues[t] = self._work_queue |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 145 | |
| 146 | def shutdown(self, wait=True): |
| 147 | with self._shutdown_lock: |
| 148 | self._shutdown = True |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 149 | self._work_queue.put(None) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 150 | if wait: |
| 151 | for t in self._threads: |
| 152 | t.join() |
| 153 | shutdown.__doc__ = _base.Executor.shutdown.__doc__ |