@@ -162,9 +162,7 @@ def __init__(self, processes=None, initializer=None, initargs=(),
162
162
163
163
self ._worker_handler = threading .Thread (
164
164
target = Pool ._handle_workers ,
165
- args = (self ._cache , self ._processes , self ._pool , self .Process ,
166
- self ._inqueue , self ._outqueue , self ._initializer ,
167
- self ._initargs , self ._maxtasksperchild , self ._taskqueue )
165
+ args = (self , )
168
166
)
169
167
self ._worker_handler .daemon = True
170
168
self ._worker_handler ._state = RUN
@@ -196,56 +194,42 @@ def __init__(self, processes=None, initializer=None, initargs=(),
196
194
exitpriority = 15
197
195
)
198
196
199
- @staticmethod
200
- def _join_exited_workers (pool ):
197
+ def _join_exited_workers (self ):
201
198
"""Cleanup after any worker processes which have exited due to reaching
202
199
their specified lifetime. Returns True if any workers were cleaned up.
203
200
"""
204
201
cleaned = False
205
- for i in reversed (range (len (pool ))):
206
- worker = pool [i ]
202
+ for i in reversed (range (len (self . _pool ))):
203
+ worker = self . _pool [i ]
207
204
if worker .exitcode is not None :
208
205
# worker exited
209
206
debug ('cleaning up worker %d' % i )
210
207
worker .join ()
211
208
cleaned = True
212
- del pool [i ]
209
+ del self . _pool [i ]
213
210
return cleaned
214
211
215
212
def _repopulate_pool (self ):
216
- return self ._repopulate_pool_static (self ._processes , self ._pool ,
217
- self .Process , self ._inqueue ,
218
- self ._outqueue , self ._initializer ,
219
- self ._initargs ,
220
- self ._maxtasksperchild )
221
-
222
- @staticmethod
223
- def _repopulate_pool_static (processes , pool , Process , inqueue , outqueue ,
224
- initializer , initargs , maxtasksperchild ):
225
213
"""Bring the number of pool processes up to the specified number,
226
214
for use after reaping workers which have exited.
227
215
"""
228
- for i in range (processes - len (pool )):
229
- w = Process (target = worker ,
230
- args = (inqueue , outqueue ,
231
- initializer ,
232
- initargs , maxtasksperchild )
233
- )
234
- pool .append (w )
216
+ for i in range (self . _processes - len (self . _pool )):
217
+ w = self . Process (target = worker ,
218
+ args = (self . _inqueue , self . _outqueue ,
219
+ self . _initializer ,
220
+ self . _initargs , self . _maxtasksperchild )
221
+ )
222
+ self . _pool .append (w )
235
223
w .name = w .name .replace ('Process' , 'PoolWorker' )
236
224
w .daemon = True
237
225
w .start ()
238
226
debug ('added worker' )
239
227
240
- @staticmethod
241
- def _maintain_pool (processes , pool , Process , inqueue , outqueue ,
242
- initializer , initargs , maxtasksperchild ):
228
+ def _maintain_pool (self ):
243
229
"""Clean up any exited workers and start replacements for them.
244
230
"""
245
- if Pool ._join_exited_workers (pool ):
246
- Pool ._repopulate_pool_static (processes , pool , Process , inqueue ,
247
- outqueue , initializer , initargs ,
248
- maxtasksperchild )
231
+ if self ._join_exited_workers ():
232
+ self ._repopulate_pool ()
249
233
250
234
def _setup_queues (self ):
251
235
from .queues import SimpleQueue
@@ -335,18 +319,16 @@ def map_async(self, func, iterable, chunksize=None, callback=None):
335
319
return result
336
320
337
321
@staticmethod
338
- def _handle_workers (cache , processes , pool , Process , inqueue , outqueue ,
339
- initializer , initargs , maxtasksperchild , taskqueue ):
322
+ def _handle_workers (pool ):
340
323
thread = threading .current_thread ()
341
324
342
325
# Keep maintaining workers until the cache gets drained, unless the pool
343
326
# is terminated.
344
- while thread ._state == RUN or (cache and thread ._state != TERMINATE ):
345
- Pool ._maintain_pool (processes , pool , Process , inqueue , outqueue ,
346
- initializer , initargs , maxtasksperchild )
327
+ while thread ._state == RUN or (pool ._cache and thread ._state != TERMINATE ):
328
+ pool ._maintain_pool ()
347
329
time .sleep (0.1 )
348
330
# send sentinel to stop workers
349
- taskqueue .put (None )
331
+ pool . _taskqueue .put (None )
350
332
debug ('worker handler exiting' )
351
333
352
334
@staticmethod
0 commit comments