Mercurial > public > mercurial-scm > hg
annotate mercurial/thirdparty/concurrent/futures/thread.py @ 37623:eb687c28a915
thirdparty: vendor futures 3.2.0
Python 3 has a concurrent.futures package in the standard library
for representing futures. The "futures" package on PyPI is a backport
of this package to work with Python 2.
The wire protocol code today has its own future concept for handling
of "batch" requests. The frame-based protocol will also want to
use futures.
I've heavily used the "futures" package on Python 2 in other projects
and it is pretty nice. It even has a built-in thread and process pool
for running functions in parallel. I've used this heavily for concurrent
I/O and other GIL-less activities.
The existing futures API in the wire protocol code is not as nice as
concurrent.futures. Since concurrent.futures is in the Python standard
library and will presumably be the long-term future for futures in our
code base, let's vendor the backport so we can use proper futures today.
# no-check-commit because of style violations
Differential Revision: https://phab.mercurial-scm.org/D3261
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Wed, 11 Apr 2018 14:48:24 -0700 |
parents | |
children | 0a9c0d3480b2 |
rev | line source |
---|---|
37623
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
1 # Copyright 2009 Brian Quinlan. All Rights Reserved. |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
2 # Licensed to PSF under a Contributor Agreement. |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
3 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
4 """Implements ThreadPoolExecutor.""" |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
5 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
6 import atexit |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
7 from concurrent.futures import _base |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
8 import itertools |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
9 import Queue as queue |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
10 import threading |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
11 import weakref |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
12 import sys |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
13 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
14 try: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
15 from multiprocessing import cpu_count |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
16 except ImportError: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
17 # some platforms don't have multiprocessing |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
18 def cpu_count(): |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
19 return None |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
20 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
21 __author__ = 'Brian Quinlan (brian@sweetapp.com)' |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
22 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
23 # Workers are created as daemon threads. This is done to allow the interpreter |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
24 # to exit when there are still idle threads in a ThreadPoolExecutor's thread |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
25 # pool (i.e. shutdown() was not called). However, allowing workers to die with |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
26 # the interpreter has two undesirable properties: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
27 # - The workers would still be running during interpretor shutdown, |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
28 # meaning that they would fail in unpredictable ways. |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
29 # - The workers could be killed while evaluating a work item, which could |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
30 # be bad if the callable being evaluated has external side-effects e.g. |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
31 # writing to a file. |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
32 # |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
33 # To work around this problem, an exit handler is installed which tells the |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
34 # workers to exit when their work queues are empty and then waits until the |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
35 # threads finish. |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
36 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
37 _threads_queues = weakref.WeakKeyDictionary() |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
38 _shutdown = False |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
39 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
40 def _python_exit(): |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
41 global _shutdown |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
42 _shutdown = True |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
43 items = list(_threads_queues.items()) if _threads_queues else () |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
44 for t, q in items: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
45 q.put(None) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
46 for t, q in items: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
47 t.join(sys.maxint) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
48 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
49 atexit.register(_python_exit) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
50 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
51 class _WorkItem(object): |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
52 def __init__(self, future, fn, args, kwargs): |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
53 self.future = future |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
54 self.fn = fn |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
55 self.args = args |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
56 self.kwargs = kwargs |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
57 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
58 def run(self): |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
59 if not self.future.set_running_or_notify_cancel(): |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
60 return |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
61 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
62 try: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
63 result = self.fn(*self.args, **self.kwargs) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
64 except: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
65 e, tb = sys.exc_info()[1:] |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
66 self.future.set_exception_info(e, tb) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
67 else: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
68 self.future.set_result(result) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
69 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
70 def _worker(executor_reference, work_queue): |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
71 try: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
72 while True: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
73 work_item = work_queue.get(block=True) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
74 if work_item is not None: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
75 work_item.run() |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
76 # Delete references to object. See issue16284 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
77 del work_item |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
78 continue |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
79 executor = executor_reference() |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
80 # Exit if: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
81 # - The interpreter is shutting down OR |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
82 # - The executor that owns the worker has been collected OR |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
83 # - The executor that owns the worker has been shutdown. |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
84 if _shutdown or executor is None or executor._shutdown: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
85 # Notice other workers |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
86 work_queue.put(None) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
87 return |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
88 del executor |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
89 except: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
90 _base.LOGGER.critical('Exception in worker', exc_info=True) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
91 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
92 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
93 class ThreadPoolExecutor(_base.Executor): |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
94 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
95 # Used to assign unique thread names when thread_name_prefix is not supplied. |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
96 _counter = itertools.count().next |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
97 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
98 def __init__(self, max_workers=None, thread_name_prefix=''): |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
99 """Initializes a new ThreadPoolExecutor instance. |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
100 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
101 Args: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
102 max_workers: The maximum number of threads that can be used to |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
103 execute the given calls. |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
104 thread_name_prefix: An optional name prefix to give our threads. |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
105 """ |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
106 if max_workers is None: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
107 # Use this number because ThreadPoolExecutor is often |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
108 # used to overlap I/O instead of CPU work. |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
109 max_workers = (cpu_count() or 1) * 5 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
110 if max_workers <= 0: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
111 raise ValueError("max_workers must be greater than 0") |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
112 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
113 self._max_workers = max_workers |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
114 self._work_queue = queue.Queue() |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
115 self._threads = set() |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
116 self._shutdown = False |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
117 self._shutdown_lock = threading.Lock() |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
118 self._thread_name_prefix = (thread_name_prefix or |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
119 ("ThreadPoolExecutor-%d" % self._counter())) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
120 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
121 def submit(self, fn, *args, **kwargs): |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
122 with self._shutdown_lock: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
123 if self._shutdown: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
124 raise RuntimeError('cannot schedule new futures after shutdown') |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
125 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
126 f = _base.Future() |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
127 w = _WorkItem(f, fn, args, kwargs) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
128 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
129 self._work_queue.put(w) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
130 self._adjust_thread_count() |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
131 return f |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
132 submit.__doc__ = _base.Executor.submit.__doc__ |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
133 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
134 def _adjust_thread_count(self): |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
135 # When the executor gets lost, the weakref callback will wake up |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
136 # the worker threads. |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
137 def weakref_cb(_, q=self._work_queue): |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
138 q.put(None) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
139 # TODO(bquinlan): Should avoid creating new threads if there are more |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
140 # idle threads than items in the work queue. |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
141 num_threads = len(self._threads) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
142 if num_threads < self._max_workers: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
143 thread_name = '%s_%d' % (self._thread_name_prefix or self, |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
144 num_threads) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
145 t = threading.Thread(name=thread_name, target=_worker, |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
146 args=(weakref.ref(self, weakref_cb), |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
147 self._work_queue)) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
148 t.daemon = True |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
149 t.start() |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
150 self._threads.add(t) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
151 _threads_queues[t] = self._work_queue |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
152 |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
153 def shutdown(self, wait=True): |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
154 with self._shutdown_lock: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
155 self._shutdown = True |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
156 self._work_queue.put(None) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
157 if wait: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
158 for t in self._threads: |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
159 t.join(sys.maxint) |
eb687c28a915
thirdparty: vendor futures 3.2.0
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
160 shutdown.__doc__ = _base.Executor.shutdown.__doc__ |