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 |
| 10 | import queue |
| 11 | import threading |
| 12 | import weakref |
| 13 | |
| 14 | # Workers are created as daemon threads. This is done to allow the interpreter |
| 15 | # to exit when there are still idle threads in a ThreadPoolExecutor's thread |
| 16 | # pool (i.e. shutdown() was not called). However, allowing workers to die with |
| 17 | # the interpreter has two undesirable properties: |
| 18 | # - The workers would still be running during interpretor shutdown, |
| 19 | # meaning that they would fail in unpredictable ways. |
| 20 | # - The workers could be killed while evaluating a work item, which could |
| 21 | # be bad if the callable being evaluated has external side-effects e.g. |
| 22 | # writing to a file. |
| 23 | # |
| 24 | # To work around this problem, an exit handler is installed which tells the |
| 25 | # workers to exit when their work queues are empty and then waits until the |
| 26 | # threads finish. |
| 27 | |
| 28 | _thread_references = set() |
| 29 | _shutdown = False |
| 30 | |
| 31 | def _python_exit(): |
| 32 | global _shutdown |
| 33 | _shutdown = True |
| 34 | for thread_reference in _thread_references: |
| 35 | thread = thread_reference() |
| 36 | if thread is not None: |
| 37 | thread.join() |
| 38 | |
| 39 | def _remove_dead_thread_references(): |
| 40 | """Remove inactive threads from _thread_references. |
| 41 | |
| 42 | Should be called periodically to prevent memory leaks in scenarios such as: |
| 43 | >>> while True: |
| 44 | ... t = ThreadPoolExecutor(max_workers=5) |
| 45 | ... t.map(int, ['1', '2', '3', '4', '5']) |
| 46 | """ |
| 47 | for thread_reference in set(_thread_references): |
| 48 | if thread_reference() is None: |
| 49 | _thread_references.discard(thread_reference) |
| 50 | |
| 51 | atexit.register(_python_exit) |
| 52 | |
| 53 | class _WorkItem(object): |
| 54 | def __init__(self, future, fn, args, kwargs): |
| 55 | self.future = future |
| 56 | self.fn = fn |
| 57 | self.args = args |
| 58 | self.kwargs = kwargs |
| 59 | |
| 60 | def run(self): |
| 61 | if not self.future.set_running_or_notify_cancel(): |
| 62 | return |
| 63 | |
| 64 | try: |
| 65 | result = self.fn(*self.args, **self.kwargs) |
| 66 | except BaseException as e: |
| 67 | self.future.set_exception(e) |
| 68 | else: |
| 69 | self.future.set_result(result) |
| 70 | |
| 71 | def _worker(executor_reference, work_queue): |
| 72 | try: |
| 73 | while True: |
| 74 | try: |
| 75 | work_item = work_queue.get(block=True, timeout=0.1) |
| 76 | except queue.Empty: |
| 77 | executor = executor_reference() |
| 78 | # Exit if: |
| 79 | # - The interpreter is shutting down OR |
| 80 | # - The executor that owns the worker has been collected OR |
| 81 | # - The executor that owns the worker has been shutdown. |
| 82 | if _shutdown or executor is None or executor._shutdown: |
| 83 | return |
| 84 | del executor |
| 85 | else: |
| 86 | work_item.run() |
| 87 | except BaseException as e: |
| 88 | _base.LOGGER.critical('Exception in worker', exc_info=True) |
| 89 | |
| 90 | class ThreadPoolExecutor(_base.Executor): |
| 91 | def __init__(self, max_workers): |
| 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. |
| 97 | """ |
| 98 | _remove_dead_thread_references() |
| 99 | |
| 100 | self._max_workers = max_workers |
| 101 | self._work_queue = queue.Queue() |
| 102 | self._threads = set() |
| 103 | self._shutdown = False |
| 104 | self._shutdown_lock = threading.Lock() |
| 105 | |
| 106 | def submit(self, fn, *args, **kwargs): |
| 107 | with self._shutdown_lock: |
| 108 | if self._shutdown: |
| 109 | raise RuntimeError('cannot schedule new futures after shutdown') |
| 110 | |
| 111 | f = _base.Future() |
| 112 | w = _WorkItem(f, fn, args, kwargs) |
| 113 | |
| 114 | self._work_queue.put(w) |
| 115 | self._adjust_thread_count() |
| 116 | return f |
| 117 | submit.__doc__ = _base.Executor.submit.__doc__ |
| 118 | |
| 119 | def _adjust_thread_count(self): |
| 120 | # TODO(bquinlan): Should avoid creating new threads if there are more |
| 121 | # idle threads than items in the work queue. |
| 122 | if len(self._threads) < self._max_workers: |
| 123 | t = threading.Thread(target=_worker, |
| 124 | args=(weakref.ref(self), self._work_queue)) |
| 125 | t.daemon = True |
| 126 | t.start() |
| 127 | self._threads.add(t) |
| 128 | _thread_references.add(weakref.ref(t)) |
| 129 | |
| 130 | def shutdown(self, wait=True): |
| 131 | with self._shutdown_lock: |
| 132 | self._shutdown = True |
| 133 | if wait: |
| 134 | for t in self._threads: |
| 135 | t.join() |
| 136 | shutdown.__doc__ = _base.Executor.shutdown.__doc__ |