- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy paththread.py
217 lines (187 loc) · 7.81 KB
/
thread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# Copyright 2009 Brian Quinlan. All Rights Reserved.
# Licensed to PSF under a Contributor Agreement.
"""Implements ThreadPoolExecutor."""
__author__='Brian Quinlan (brian@sweetapp.com)'
importatexit
fromconcurrent.futuresimport_base
importitertools
importqueue
importthreading
importweakref
importos
# Workers are created as daemon threads. This is done to allow the interpreter
# to exit when there are still idle threads in a ThreadPoolExecutor's thread
# pool (i.e. shutdown() was not called). However, allowing workers to die with
# the interpreter has two undesirable properties:
# - The workers would still be running during interpreter shutdown,
# meaning that they would fail in unpredictable ways.
# - The workers could be killed while evaluating a work item, which could
# be bad if the callable being evaluated has external side-effects e.g.
# writing to a file.
#
# To work around this problem, an exit handler is installed which tells the
# workers to exit when their work queues are empty and then waits until the
# threads finish.
_threads_queues=weakref.WeakKeyDictionary()
_shutdown=False
def_python_exit():
global_shutdown
_shutdown=True
items=list(_threads_queues.items())
fort, qinitems:
q.put(None)
fort, qinitems:
t.join()
atexit.register(_python_exit)
class_WorkItem(object):
def__init__(self, future, fn, args, kwargs):
self.future=future
self.fn=fn
self.args=args
self.kwargs=kwargs
defrun(self):
ifnotself.future.set_running_or_notify_cancel():
return
try:
result=self.fn(*self.args, **self.kwargs)
exceptBaseExceptionasexc:
self.future.set_exception(exc)
# Break a reference cycle with the exception 'exc'
self=None
else:
self.future.set_result(result)
def_worker(executor_reference, work_queue, initializer, initargs):
ifinitializerisnotNone:
try:
initializer(*initargs)
exceptBaseException:
_base.LOGGER.critical('Exception in initializer:', exc_info=True)
executor=executor_reference()
ifexecutorisnotNone:
executor._initializer_failed()
return
try:
whileTrue:
work_item=work_queue.get(block=True)
ifwork_itemisnotNone:
work_item.run()
# Delete references to object. See issue16284
delwork_item
continue
executor=executor_reference()
# Exit if:
# - The interpreter is shutting down OR
# - The executor that owns the worker has been collected OR
# - The executor that owns the worker has been shutdown.
if_shutdownorexecutorisNoneorexecutor._shutdown:
# Flag the executor as shutting down as early as possible if it
# is not gc-ed yet.
ifexecutorisnotNone:
executor._shutdown=True
# Notice other workers
work_queue.put(None)
return
delexecutor
exceptBaseException:
_base.LOGGER.critical('Exception in worker', exc_info=True)
classBrokenThreadPool(_base.BrokenExecutor):
"""
Raised when a worker thread in a ThreadPoolExecutor failed initializing.
"""
classThreadPoolExecutor(_base.Executor):
# Used to assign unique thread names when thread_name_prefix is not supplied.
_counter=itertools.count().__next__
def__init__(self, max_workers=None, thread_name_prefix='',
initializer=None, initargs=()):
"""Initializes a new ThreadPoolExecutor instance.
Args:
max_workers: The maximum number of threads that can be used to
execute the given calls.
thread_name_prefix: An optional name prefix to give our threads.
initializer: A callable used to initialize worker threads.
initargs: A tuple of arguments to pass to the initializer.
"""
ifmax_workersisNone:
# Use this number because ThreadPoolExecutor is often
# used to overlap I/O instead of CPU work.
max_workers= (os.cpu_count() or1) *5
ifmax_workers<=0:
raiseValueError("max_workers must be greater than 0")
ifinitializerisnotNoneandnotcallable(initializer):
raiseTypeError("initializer must be a callable")
self._max_workers=max_workers
self._work_queue=queue.SimpleQueue()
self._threads=set()
self._broken=False
self._shutdown=False
self._shutdown_lock=threading.Lock()
self._thread_name_prefix= (thread_name_prefixor
("ThreadPoolExecutor-%d"%self._counter()))
self._initializer=initializer
self._initargs=initargs
defsubmit(*args, **kwargs):
iflen(args) >=2:
self, fn, *args=args
elifnotargs:
raiseTypeError("descriptor 'submit' of 'ThreadPoolExecutor' object "
"needs an argument")
elif'fn'inkwargs:
fn=kwargs.pop('fn')
self, *args=args
else:
raiseTypeError('submit expected at least 1 positional argument, '
'got %d'% (len(args)-1))
withself._shutdown_lock:
ifself._broken:
raiseBrokenThreadPool(self._broken)
ifself._shutdown:
raiseRuntimeError('cannot schedule new futures after shutdown')
if_shutdown:
raiseRuntimeError('cannot schedule new futures after '
'interpreter shutdown')
f=_base.Future()
w=_WorkItem(f, fn, args, kwargs)
self._work_queue.put(w)
self._adjust_thread_count()
returnf
submit.__doc__=_base.Executor.submit.__doc__
def_adjust_thread_count(self):
# When the executor gets lost, the weakref callback will wake up
# the worker threads.
defweakref_cb(_, q=self._work_queue):
q.put(None)
# TODO(bquinlan): Should avoid creating new threads if there are more
# idle threads than items in the work queue.
num_threads=len(self._threads)
ifnum_threads<self._max_workers:
thread_name='%s_%d'% (self._thread_name_prefixorself,
num_threads)
t=threading.Thread(name=thread_name, target=_worker,
args=(weakref.ref(self, weakref_cb),
self._work_queue,
self._initializer,
self._initargs))
t.daemon=True
t.start()
self._threads.add(t)
_threads_queues[t] =self._work_queue
def_initializer_failed(self):
withself._shutdown_lock:
self._broken= ('A thread initializer failed, the thread pool '
'is not usable anymore')
# Drain work queue and mark pending futures failed
whileTrue:
try:
work_item=self._work_queue.get_nowait()
exceptqueue.Empty:
break
ifwork_itemisnotNone:
work_item.future.set_exception(BrokenThreadPool(self._broken))
defshutdown(self, wait=True):
withself._shutdown_lock:
self._shutdown=True
self._work_queue.put(None)
ifwait:
fortinself._threads:
t.join()
shutdown.__doc__=_base.Executor.shutdown.__doc__