@@ -149,8 +149,9 @@ class Pool(object):
149
149
'''
150
150
_wrap_exception = True
151
151
152
- def Process (self , * args , ** kwds ):
153
- return self ._ctx .Process (* args , ** kwds )
152
+ @staticmethod
153
+ def Process (ctx , * args , ** kwds ):
154
+ return ctx .Process (* args , ** kwds )
154
155
155
156
def __init__ (self , processes = None , initializer = None , initargs = (),
156
157
maxtasksperchild = None , context = None ):
@@ -177,13 +178,15 @@ def __init__(self, processes=None, initializer=None, initargs=(),
177
178
178
179
self ._worker_handler = threading .Thread (
179
180
target = Pool ._handle_workers ,
180
- args = (self , )
181
+ args = (self ._cache , self ._taskqueue , self ._ctx , self .Process ,
182
+ self ._processes , self ._pool , self ._inqueue , self ._outqueue ,
183
+ self ._initializer , self ._initargs , self ._maxtasksperchild ,
184
+ self ._wrap_exception )
181
185
)
182
186
self ._worker_handler .daemon = True
183
187
self ._worker_handler ._state = RUN
184
188
self ._worker_handler .start ()
185
189
186
-
187
190
self ._task_handler = threading .Thread (
188
191
target = Pool ._handle_tasks ,
189
192
args = (self ._taskqueue , self ._quick_put , self ._outqueue ,
@@ -209,43 +212,61 @@ def __init__(self, processes=None, initializer=None, initargs=(),
209
212
exitpriority = 15
210
213
)
211
214
212
- def _join_exited_workers (self ):
215
+ @staticmethod
216
+ def _join_exited_workers (pool ):
213
217
"""Cleanup after any worker processes which have exited due to reaching
214
218
their specified lifetime. Returns True if any workers were cleaned up.
215
219
"""
216
220
cleaned = False
217
- for i in reversed (range (len (self . _pool ))):
218
- worker = self . _pool [i ]
221
+ for i in reversed (range (len (pool ))):
222
+ worker = pool [i ]
219
223
if worker .exitcode is not None :
220
224
# worker exited
221
225
util .debug ('cleaning up worker %d' % i )
222
226
worker .join ()
223
227
cleaned = True
224
- del self . _pool [i ]
228
+ del pool [i ]
225
229
return cleaned
226
230
227
231
def _repopulate_pool (self ):
232
+ return self ._repopulate_pool_static (self ._ctx , self .Process ,
233
+ self ._processes ,
234
+ self ._pool , self ._inqueue ,
235
+ self ._outqueue , self ._initializer ,
236
+ self ._initargs ,
237
+ self ._maxtasksperchild ,
238
+ self ._wrap_exception )
239
+
240
+ @staticmethod
241
+ def _repopulate_pool_static (ctx , Process , processes , pool , inqueue ,
242
+ outqueue , initializer , initargs ,
243
+ maxtasksperchild , wrap_exception ):
228
244
"""Bring the number of pool processes up to the specified number,
229
245
for use after reaping workers which have exited.
230
246
"""
231
- for i in range (self . _processes - len (self . _pool )):
232
- w = self . Process (target = worker ,
233
- args = (self . _inqueue , self . _outqueue ,
234
- self . _initializer ,
235
- self . _initargs , self . _maxtasksperchild ,
236
- self . _wrap_exception )
237
- )
238
- self . _pool .append (w )
247
+ for i in range (processes - len (pool )):
248
+ w = Process (ctx , target = worker ,
249
+ args = (inqueue , outqueue ,
250
+ initializer ,
251
+ initargs , maxtasksperchild ,
252
+ wrap_exception )
253
+ )
254
+ pool .append (w )
239
255
w .name = w .name .replace ('Process' , 'PoolWorker' )
240
256
w .daemon = True
241
257
w .start ()
242
258
util .debug ('added worker' )
243
259
244
- def _maintain_pool (self ):
260
+ @classmethod
261
+ def _maintain_pool (cls , ctx , Process , processes , pool , inqueue , outqueue ,
262
+ initializer , initargs , maxtasksperchild ,
263
+ wrap_exception ):
245
264
"""Clean up any exited workers and start replacements for them.
246
265
"""
247
- if self ._join_exited_workers ():
248
- self ._repopulate_pool ()
266
+ if cls ._join_exited_workers (pool ):
267
+ cls ._repopulate_pool_static (ctx , Process , processes , pool , inqueue ,
268
+ outqueue , initializer , initargs ,
269
+ maxtasksperchild , wrap_exception )
249
270
250
271
def _setup_queues (self ):
251
272
self ._inqueue = self ._ctx .SimpleQueue ()
@@ -402,17 +423,21 @@ def _map_async(self, func, iterable, mapper, chunksize=None, callback=None,
402
423
)
403
424
return result
404
425
405
- @staticmethod
406
- def _handle_workers (pool ):
426
+ @classmethod
427
+ def _handle_workers (cls , cache , taskqueue , ctx , Process , processes , pool ,
428
+ inqueue , outqueue , initializer , initargs ,
429
+ maxtasksperchild , wrap_exception ):
407
430
thread = threading .current_thread ()
408
431
409
432
# Keep maintaining workers until the cache gets drained, unless the pool
410
433
# is terminated.
411
- while thread ._state == RUN or (pool ._cache and thread ._state != TERMINATE ):
412
- pool ._maintain_pool ()
434
+ while thread ._state == RUN or (cache and thread ._state != TERMINATE ):
435
+ cls ._maintain_pool (ctx , Process , processes , pool , inqueue ,
436
+ outqueue , initializer , initargs ,
437
+ maxtasksperchild , wrap_exception )
413
438
time .sleep (0.1 )
414
439
# send sentinel to stop workers
415
- pool . _taskqueue .put (None )
440
+ taskqueue .put (None )
416
441
util .debug ('worker handler exiting' )
417
442
418
443
@staticmethod
@@ -794,7 +819,7 @@ class ThreadPool(Pool):
794
819
_wrap_exception = False
795
820
796
821
@staticmethod
797
- def Process (* args , ** kwds ):
822
+ def Process (ctx , * args , ** kwds ):
798
823
from .dummy import Process
799
824
return Process (* args , ** kwds )
800
825
0 commit comments