blob: ff7c29c85b6538a21670e2e288873c5b0e5277a4 [file] [log] [blame]
Benjamin Petersone711caf2008-06-11 16:44:04 +00001#
2# Module providing the `Pool` class for managing a process pool
3#
4# multiprocessing/pool.py
5#
R. David Murray3fc969a2010-12-14 01:38:16 +00006# Copyright (c) 2006-2008, R Oudkerk
7# All rights reserved.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12#
13# 1. Redistributions of source code must retain the above copyright
14# notice, this list of conditions and the following disclaimer.
15# 2. Redistributions in binary form must reproduce the above copyright
16# notice, this list of conditions and the following disclaimer in the
17# documentation and/or other materials provided with the distribution.
18# 3. Neither the name of author nor the names of any contributors may be
19# used to endorse or promote products derived from this software
20# without specific prior written permission.
21#
22# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
23# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32# SUCH DAMAGE.
Benjamin Petersone711caf2008-06-11 16:44:04 +000033#
34
35__all__ = ['Pool']
36
37#
38# Imports
39#
40
41import threading
42import queue
43import itertools
44import collections
45import time
46
47from multiprocessing import Process, cpu_count, TimeoutError
48from multiprocessing.util import Finalize, debug
49
50#
51# Constants representing the state of a pool
52#
53
54RUN = 0
55CLOSE = 1
56TERMINATE = 2
57
58#
59# Miscellaneous
60#
61
62job_counter = itertools.count()
63
64def mapstar(args):
65 return list(map(*args))
66
67#
68# Code run by worker processes
69#
70
Ask Solem2afcbf22010-11-09 20:55:52 +000071class MaybeEncodingError(Exception):
72 """Wraps possible unpickleable errors, so they can be
73 safely sent through the socket."""
74
75 def __init__(self, exc, value):
76 self.exc = repr(exc)
77 self.value = repr(value)
78 super(MaybeEncodingError, self).__init__(self.exc, self.value)
79
80 def __str__(self):
81 return "Error sending result: '%s'. Reason: '%s'" % (self.value,
82 self.exc)
83
84 def __repr__(self):
85 return "<MaybeEncodingError: %s>" % str(self)
86
87
Jesse Noller1f0b6582010-01-27 03:36:01 +000088def worker(inqueue, outqueue, initializer=None, initargs=(), maxtasks=None):
89 assert maxtasks is None or (type(maxtasks) == int and maxtasks > 0)
Benjamin Petersone711caf2008-06-11 16:44:04 +000090 put = outqueue.put
91 get = inqueue.get
92 if hasattr(inqueue, '_writer'):
93 inqueue._writer.close()
94 outqueue._reader.close()
95
96 if initializer is not None:
97 initializer(*initargs)
98
Jesse Noller1f0b6582010-01-27 03:36:01 +000099 completed = 0
100 while maxtasks is None or (maxtasks and completed < maxtasks):
Benjamin Petersone711caf2008-06-11 16:44:04 +0000101 try:
102 task = get()
103 except (EOFError, IOError):
104 debug('worker got EOFError or IOError -- exiting')
105 break
106
107 if task is None:
108 debug('worker got sentinel -- exiting')
109 break
110
111 job, i, func, args, kwds = task
112 try:
113 result = (True, func(*args, **kwds))
114 except Exception as e:
115 result = (False, e)
Ask Solem2afcbf22010-11-09 20:55:52 +0000116 try:
117 put((job, i, result))
118 except Exception as e:
119 wrapped = MaybeEncodingError(e, result[1])
120 debug("Possible encoding error while sending result: %s" % (
121 wrapped))
122 put((job, i, (False, wrapped)))
Jesse Noller1f0b6582010-01-27 03:36:01 +0000123 completed += 1
124 debug('worker exiting after %d tasks' % completed)
Benjamin Petersone711caf2008-06-11 16:44:04 +0000125
126#
127# Class representing a process pool
128#
129
130class Pool(object):
131 '''
Georg Brandl92905032008-11-22 08:51:39 +0000132 Class which supports an async version of applying functions to arguments.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000133 '''
134 Process = Process
135
Jesse Noller1f0b6582010-01-27 03:36:01 +0000136 def __init__(self, processes=None, initializer=None, initargs=(),
137 maxtasksperchild=None):
Benjamin Petersone711caf2008-06-11 16:44:04 +0000138 self._setup_queues()
139 self._taskqueue = queue.Queue()
140 self._cache = {}
141 self._state = RUN
Jesse Noller1f0b6582010-01-27 03:36:01 +0000142 self._maxtasksperchild = maxtasksperchild
143 self._initializer = initializer
144 self._initargs = initargs
Benjamin Petersone711caf2008-06-11 16:44:04 +0000145
146 if processes is None:
147 try:
148 processes = cpu_count()
149 except NotImplementedError:
150 processes = 1
151
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000152 if initializer is not None and not hasattr(initializer, '__call__'):
153 raise TypeError('initializer must be a callable')
154
Jesse Noller1f0b6582010-01-27 03:36:01 +0000155 self._processes = processes
Benjamin Petersone711caf2008-06-11 16:44:04 +0000156 self._pool = []
Jesse Noller1f0b6582010-01-27 03:36:01 +0000157 self._repopulate_pool()
158
159 self._worker_handler = threading.Thread(
160 target=Pool._handle_workers,
161 args=(self, )
162 )
163 self._worker_handler.daemon = True
164 self._worker_handler._state = RUN
165 self._worker_handler.start()
166
Benjamin Petersone711caf2008-06-11 16:44:04 +0000167
168 self._task_handler = threading.Thread(
169 target=Pool._handle_tasks,
170 args=(self._taskqueue, self._quick_put, self._outqueue, self._pool)
171 )
Benjamin Petersonfae4c622008-08-18 18:40:08 +0000172 self._task_handler.daemon = True
Benjamin Petersone711caf2008-06-11 16:44:04 +0000173 self._task_handler._state = RUN
174 self._task_handler.start()
175
176 self._result_handler = threading.Thread(
177 target=Pool._handle_results,
178 args=(self._outqueue, self._quick_get, self._cache)
179 )
Benjamin Petersonfae4c622008-08-18 18:40:08 +0000180 self._result_handler.daemon = True
Benjamin Petersone711caf2008-06-11 16:44:04 +0000181 self._result_handler._state = RUN
182 self._result_handler.start()
183
184 self._terminate = Finalize(
185 self, self._terminate_pool,
186 args=(self._taskqueue, self._inqueue, self._outqueue, self._pool,
Jesse Noller1f0b6582010-01-27 03:36:01 +0000187 self._worker_handler, self._task_handler,
188 self._result_handler, self._cache),
Benjamin Petersone711caf2008-06-11 16:44:04 +0000189 exitpriority=15
190 )
191
Jesse Noller1f0b6582010-01-27 03:36:01 +0000192 def _join_exited_workers(self):
193 """Cleanup after any worker processes which have exited due to reaching
194 their specified lifetime. Returns True if any workers were cleaned up.
195 """
196 cleaned = False
197 for i in reversed(range(len(self._pool))):
198 worker = self._pool[i]
199 if worker.exitcode is not None:
200 # worker exited
201 debug('cleaning up worker %d' % i)
202 worker.join()
203 cleaned = True
204 del self._pool[i]
205 return cleaned
206
207 def _repopulate_pool(self):
208 """Bring the number of pool processes up to the specified number,
209 for use after reaping workers which have exited.
210 """
211 for i in range(self._processes - len(self._pool)):
212 w = self.Process(target=worker,
213 args=(self._inqueue, self._outqueue,
214 self._initializer,
215 self._initargs, self._maxtasksperchild)
216 )
217 self._pool.append(w)
218 w.name = w.name.replace('Process', 'PoolWorker')
219 w.daemon = True
220 w.start()
221 debug('added worker')
222
223 def _maintain_pool(self):
224 """Clean up any exited workers and start replacements for them.
225 """
226 if self._join_exited_workers():
227 self._repopulate_pool()
228
Benjamin Petersone711caf2008-06-11 16:44:04 +0000229 def _setup_queues(self):
230 from .queues import SimpleQueue
231 self._inqueue = SimpleQueue()
232 self._outqueue = SimpleQueue()
233 self._quick_put = self._inqueue._writer.send
234 self._quick_get = self._outqueue._reader.recv
235
236 def apply(self, func, args=(), kwds={}):
237 '''
Georg Brandl92905032008-11-22 08:51:39 +0000238 Equivalent of `func(*args, **kwds)`.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000239 '''
240 assert self._state == RUN
241 return self.apply_async(func, args, kwds).get()
242
243 def map(self, func, iterable, chunksize=None):
244 '''
Georg Brandl92905032008-11-22 08:51:39 +0000245 Apply `func` to each element in `iterable`, collecting the results
246 in a list that is returned.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000247 '''
248 assert self._state == RUN
249 return self.map_async(func, iterable, chunksize).get()
250
251 def imap(self, func, iterable, chunksize=1):
252 '''
Georg Brandl92905032008-11-22 08:51:39 +0000253 Equivalent of `map()` -- can be MUCH slower than `Pool.map()`.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000254 '''
255 assert self._state == RUN
256 if chunksize == 1:
257 result = IMapIterator(self._cache)
258 self._taskqueue.put((((result._job, i, func, (x,), {})
259 for i, x in enumerate(iterable)), result._set_length))
260 return result
261 else:
262 assert chunksize > 1
263 task_batches = Pool._get_tasks(func, iterable, chunksize)
264 result = IMapIterator(self._cache)
265 self._taskqueue.put((((result._job, i, mapstar, (x,), {})
266 for i, x in enumerate(task_batches)), result._set_length))
267 return (item for chunk in result for item in chunk)
268
269 def imap_unordered(self, func, iterable, chunksize=1):
270 '''
Georg Brandl92905032008-11-22 08:51:39 +0000271 Like `imap()` method but ordering of results is arbitrary.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000272 '''
273 assert self._state == RUN
274 if chunksize == 1:
275 result = IMapUnorderedIterator(self._cache)
276 self._taskqueue.put((((result._job, i, func, (x,), {})
277 for i, x in enumerate(iterable)), result._set_length))
278 return result
279 else:
280 assert chunksize > 1
281 task_batches = Pool._get_tasks(func, iterable, chunksize)
282 result = IMapUnorderedIterator(self._cache)
283 self._taskqueue.put((((result._job, i, mapstar, (x,), {})
284 for i, x in enumerate(task_batches)), result._set_length))
285 return (item for chunk in result for item in chunk)
286
Ask Solem2afcbf22010-11-09 20:55:52 +0000287 def apply_async(self, func, args=(), kwds={}, callback=None,
288 error_callback=None):
Benjamin Petersone711caf2008-06-11 16:44:04 +0000289 '''
Georg Brandl92905032008-11-22 08:51:39 +0000290 Asynchronous version of `apply()` method.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000291 '''
292 assert self._state == RUN
Ask Solem2afcbf22010-11-09 20:55:52 +0000293 result = ApplyResult(self._cache, callback, error_callback)
Benjamin Petersone711caf2008-06-11 16:44:04 +0000294 self._taskqueue.put(([(result._job, None, func, args, kwds)], None))
295 return result
296
Ask Solem2afcbf22010-11-09 20:55:52 +0000297 def map_async(self, func, iterable, chunksize=None, callback=None,
298 error_callback=None):
Benjamin Petersone711caf2008-06-11 16:44:04 +0000299 '''
Georg Brandl92905032008-11-22 08:51:39 +0000300 Asynchronous version of `map()` method.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000301 '''
302 assert self._state == RUN
303 if not hasattr(iterable, '__len__'):
304 iterable = list(iterable)
305
306 if chunksize is None:
307 chunksize, extra = divmod(len(iterable), len(self._pool) * 4)
308 if extra:
309 chunksize += 1
Alexandre Vassalottie52e3782009-07-17 09:18:18 +0000310 if len(iterable) == 0:
311 chunksize = 0
Benjamin Petersone711caf2008-06-11 16:44:04 +0000312
313 task_batches = Pool._get_tasks(func, iterable, chunksize)
Ask Solem2afcbf22010-11-09 20:55:52 +0000314 result = MapResult(self._cache, chunksize, len(iterable), callback,
315 error_callback=error_callback)
Benjamin Petersone711caf2008-06-11 16:44:04 +0000316 self._taskqueue.put((((result._job, i, mapstar, (x,), {})
317 for i, x in enumerate(task_batches)), None))
318 return result
319
320 @staticmethod
Jesse Noller1f0b6582010-01-27 03:36:01 +0000321 def _handle_workers(pool):
322 while pool._worker_handler._state == RUN and pool._state == RUN:
323 pool._maintain_pool()
324 time.sleep(0.1)
325 debug('worker handler exiting')
326
327 @staticmethod
Benjamin Petersone711caf2008-06-11 16:44:04 +0000328 def _handle_tasks(taskqueue, put, outqueue, pool):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000329 thread = threading.current_thread()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000330
331 for taskseq, set_length in iter(taskqueue.get, None):
332 i = -1
333 for i, task in enumerate(taskseq):
334 if thread._state:
335 debug('task handler found thread._state != RUN')
336 break
337 try:
338 put(task)
339 except IOError:
340 debug('could not put task on queue')
341 break
342 else:
343 if set_length:
344 debug('doing set_length()')
345 set_length(i+1)
346 continue
347 break
348 else:
349 debug('task handler got sentinel')
350
351
352 try:
353 # tell result handler to finish when cache is empty
354 debug('task handler sending sentinel to result handler')
355 outqueue.put(None)
356
357 # tell workers there is no more work
358 debug('task handler sending sentinel to workers')
359 for p in pool:
360 put(None)
361 except IOError:
362 debug('task handler got IOError when sending sentinels')
363
364 debug('task handler exiting')
365
366 @staticmethod
367 def _handle_results(outqueue, get, cache):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000368 thread = threading.current_thread()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000369
370 while 1:
371 try:
372 task = get()
373 except (IOError, EOFError):
374 debug('result handler got EOFError/IOError -- exiting')
375 return
376
377 if thread._state:
378 assert thread._state == TERMINATE
379 debug('result handler found thread._state=TERMINATE')
380 break
381
382 if task is None:
383 debug('result handler got sentinel')
384 break
385
386 job, i, obj = task
387 try:
388 cache[job]._set(i, obj)
389 except KeyError:
390 pass
391
392 while cache and thread._state != TERMINATE:
393 try:
394 task = get()
395 except (IOError, EOFError):
396 debug('result handler got EOFError/IOError -- exiting')
397 return
398
399 if task is None:
400 debug('result handler ignoring extra sentinel')
401 continue
402 job, i, obj = task
403 try:
404 cache[job]._set(i, obj)
405 except KeyError:
406 pass
407
408 if hasattr(outqueue, '_reader'):
409 debug('ensuring that outqueue is not full')
410 # If we don't make room available in outqueue then
411 # attempts to add the sentinel (None) to outqueue may
412 # block. There is guaranteed to be no more than 2 sentinels.
413 try:
414 for i in range(10):
415 if not outqueue._reader.poll():
416 break
417 get()
418 except (IOError, EOFError):
419 pass
420
421 debug('result handler exiting: len(cache)=%s, thread._state=%s',
422 len(cache), thread._state)
423
424 @staticmethod
425 def _get_tasks(func, it, size):
426 it = iter(it)
427 while 1:
428 x = tuple(itertools.islice(it, size))
429 if not x:
430 return
431 yield (func, x)
432
433 def __reduce__(self):
434 raise NotImplementedError(
435 'pool objects cannot be passed between processes or pickled'
436 )
437
438 def close(self):
439 debug('closing pool')
440 if self._state == RUN:
441 self._state = CLOSE
Jesse Noller1f0b6582010-01-27 03:36:01 +0000442 self._worker_handler._state = CLOSE
Benjamin Petersone711caf2008-06-11 16:44:04 +0000443 self._taskqueue.put(None)
444
445 def terminate(self):
446 debug('terminating pool')
447 self._state = TERMINATE
Jesse Noller1f0b6582010-01-27 03:36:01 +0000448 self._worker_handler._state = TERMINATE
Benjamin Petersone711caf2008-06-11 16:44:04 +0000449 self._terminate()
450
451 def join(self):
452 debug('joining pool')
453 assert self._state in (CLOSE, TERMINATE)
Jesse Noller1f0b6582010-01-27 03:36:01 +0000454 self._worker_handler.join()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000455 self._task_handler.join()
456 self._result_handler.join()
457 for p in self._pool:
458 p.join()
459
460 @staticmethod
461 def _help_stuff_finish(inqueue, task_handler, size):
462 # task_handler may be blocked trying to put items on inqueue
463 debug('removing tasks from inqueue until task handler finished')
464 inqueue._rlock.acquire()
Benjamin Peterson672b8032008-06-11 19:14:14 +0000465 while task_handler.is_alive() and inqueue._reader.poll():
Benjamin Petersone711caf2008-06-11 16:44:04 +0000466 inqueue._reader.recv()
467 time.sleep(0)
468
469 @classmethod
470 def _terminate_pool(cls, taskqueue, inqueue, outqueue, pool,
Jesse Noller1f0b6582010-01-27 03:36:01 +0000471 worker_handler, task_handler, result_handler, cache):
Benjamin Petersone711caf2008-06-11 16:44:04 +0000472 # this is guaranteed to only be called once
473 debug('finalizing pool')
474
Jesse Noller1f0b6582010-01-27 03:36:01 +0000475 worker_handler._state = TERMINATE
Benjamin Petersone711caf2008-06-11 16:44:04 +0000476 task_handler._state = TERMINATE
477 taskqueue.put(None) # sentinel
478
479 debug('helping task handler/workers to finish')
480 cls._help_stuff_finish(inqueue, task_handler, len(pool))
481
Benjamin Peterson672b8032008-06-11 19:14:14 +0000482 assert result_handler.is_alive() or len(cache) == 0
Benjamin Petersone711caf2008-06-11 16:44:04 +0000483
484 result_handler._state = TERMINATE
485 outqueue.put(None) # sentinel
486
Jesse Noller1f0b6582010-01-27 03:36:01 +0000487 # Terminate workers which haven't already finished.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000488 if pool and hasattr(pool[0], 'terminate'):
489 debug('terminating workers')
490 for p in pool:
Jesse Noller1f0b6582010-01-27 03:36:01 +0000491 if p.exitcode is None:
492 p.terminate()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000493
494 debug('joining task handler')
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000495 task_handler.join()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000496
497 debug('joining result handler')
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000498 task_handler.join()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000499
500 if pool and hasattr(pool[0], 'terminate'):
501 debug('joining pool workers')
502 for p in pool:
Florent Xicluna998171f2010-03-08 13:32:17 +0000503 if p.is_alive():
Jesse Noller1f0b6582010-01-27 03:36:01 +0000504 # worker has not yet exited
Florent Xicluna998171f2010-03-08 13:32:17 +0000505 debug('cleaning up worker %d' % p.pid)
506 p.join()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000507
508#
509# Class whose instances are returned by `Pool.apply_async()`
510#
511
512class ApplyResult(object):
513
Ask Solem2afcbf22010-11-09 20:55:52 +0000514 def __init__(self, cache, callback, error_callback):
Benjamin Petersone711caf2008-06-11 16:44:04 +0000515 self._cond = threading.Condition(threading.Lock())
516 self._job = next(job_counter)
517 self._cache = cache
518 self._ready = False
519 self._callback = callback
Ask Solem2afcbf22010-11-09 20:55:52 +0000520 self._error_callback = error_callback
Benjamin Petersone711caf2008-06-11 16:44:04 +0000521 cache[self._job] = self
522
523 def ready(self):
524 return self._ready
525
526 def successful(self):
527 assert self._ready
528 return self._success
529
530 def wait(self, timeout=None):
531 self._cond.acquire()
532 try:
533 if not self._ready:
534 self._cond.wait(timeout)
535 finally:
536 self._cond.release()
537
538 def get(self, timeout=None):
539 self.wait(timeout)
540 if not self._ready:
541 raise TimeoutError
542 if self._success:
543 return self._value
544 else:
545 raise self._value
546
547 def _set(self, i, obj):
548 self._success, self._value = obj
549 if self._callback and self._success:
550 self._callback(self._value)
Ask Solem2afcbf22010-11-09 20:55:52 +0000551 if self._error_callback and not self._success:
552 self._error_callback(self._value)
Benjamin Petersone711caf2008-06-11 16:44:04 +0000553 self._cond.acquire()
554 try:
555 self._ready = True
556 self._cond.notify()
557 finally:
558 self._cond.release()
559 del self._cache[self._job]
560
561#
562# Class whose instances are returned by `Pool.map_async()`
563#
564
565class MapResult(ApplyResult):
566
Ask Solem2afcbf22010-11-09 20:55:52 +0000567 def __init__(self, cache, chunksize, length, callback, error_callback):
568 ApplyResult.__init__(self, cache, callback,
569 error_callback=error_callback)
Benjamin Petersone711caf2008-06-11 16:44:04 +0000570 self._success = True
571 self._value = [None] * length
572 self._chunksize = chunksize
573 if chunksize <= 0:
574 self._number_left = 0
575 self._ready = True
576 else:
577 self._number_left = length//chunksize + bool(length % chunksize)
578
579 def _set(self, i, success_result):
580 success, result = success_result
581 if success:
582 self._value[i*self._chunksize:(i+1)*self._chunksize] = result
583 self._number_left -= 1
584 if self._number_left == 0:
585 if self._callback:
586 self._callback(self._value)
587 del self._cache[self._job]
588 self._cond.acquire()
589 try:
590 self._ready = True
591 self._cond.notify()
592 finally:
593 self._cond.release()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000594 else:
595 self._success = False
596 self._value = result
Ask Solem2afcbf22010-11-09 20:55:52 +0000597 if self._error_callback:
598 self._error_callback(self._value)
Benjamin Petersone711caf2008-06-11 16:44:04 +0000599 del self._cache[self._job]
600 self._cond.acquire()
601 try:
602 self._ready = True
603 self._cond.notify()
604 finally:
605 self._cond.release()
606
607#
608# Class whose instances are returned by `Pool.imap()`
609#
610
611class IMapIterator(object):
612
613 def __init__(self, cache):
614 self._cond = threading.Condition(threading.Lock())
615 self._job = next(job_counter)
616 self._cache = cache
617 self._items = collections.deque()
618 self._index = 0
619 self._length = None
620 self._unsorted = {}
621 cache[self._job] = self
622
623 def __iter__(self):
624 return self
625
626 def next(self, timeout=None):
627 self._cond.acquire()
628 try:
629 try:
630 item = self._items.popleft()
631 except IndexError:
632 if self._index == self._length:
633 raise StopIteration
634 self._cond.wait(timeout)
635 try:
636 item = self._items.popleft()
637 except IndexError:
638 if self._index == self._length:
639 raise StopIteration
640 raise TimeoutError
641 finally:
642 self._cond.release()
643
644 success, value = item
645 if success:
646 return value
647 raise value
648
649 __next__ = next # XXX
650
651 def _set(self, i, obj):
652 self._cond.acquire()
653 try:
654 if self._index == i:
655 self._items.append(obj)
656 self._index += 1
657 while self._index in self._unsorted:
658 obj = self._unsorted.pop(self._index)
659 self._items.append(obj)
660 self._index += 1
661 self._cond.notify()
662 else:
663 self._unsorted[i] = obj
664
665 if self._index == self._length:
666 del self._cache[self._job]
667 finally:
668 self._cond.release()
669
670 def _set_length(self, length):
671 self._cond.acquire()
672 try:
673 self._length = length
674 if self._index == self._length:
675 self._cond.notify()
676 del self._cache[self._job]
677 finally:
678 self._cond.release()
679
680#
681# Class whose instances are returned by `Pool.imap_unordered()`
682#
683
684class IMapUnorderedIterator(IMapIterator):
685
686 def _set(self, i, obj):
687 self._cond.acquire()
688 try:
689 self._items.append(obj)
690 self._index += 1
691 self._cond.notify()
692 if self._index == self._length:
693 del self._cache[self._job]
694 finally:
695 self._cond.release()
696
697#
698#
699#
700
701class ThreadPool(Pool):
702
703 from .dummy import Process
704
705 def __init__(self, processes=None, initializer=None, initargs=()):
706 Pool.__init__(self, processes, initializer, initargs)
707
708 def _setup_queues(self):
709 self._inqueue = queue.Queue()
710 self._outqueue = queue.Queue()
711 self._quick_put = self._inqueue.put
712 self._quick_get = self._outqueue.get
713
714 @staticmethod
715 def _help_stuff_finish(inqueue, task_handler, size):
716 # put sentinels at head of inqueue to make workers finish
717 inqueue.not_empty.acquire()
718 try:
719 inqueue.queue.clear()
720 inqueue.queue.extend([None] * size)
Benjamin Peterson672b8032008-06-11 19:14:14 +0000721 inqueue.not_empty.notify_all()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000722 finally:
723 inqueue.not_empty.release()