forked from matthewrobertbell/python-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custompool.py
executable file
·28 lines (25 loc) · 935 Bytes
/
custompool.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
from gevent import pool
from gevent.queue import Queue
import gevent
class LimitedIMapUnordered(pool.IMapUnordered):
def __init__(self, func, iterable, max_queue, spawn=None):
pool.IMapUnordered.__init__(self, func, iterable, spawn)
self.queue = Queue(max_queue)
self.max_queue = max_queue
def _run(self):
try:
func = self.func
for item in self.iterable:
while self.queue.qsize() + self.count + 2 == self.max_queue:
gevent.sleep(0.1)
self.count += 1
self.spawn(func, item).rawlink(self._on_result)
finally:
self.__dict__.pop('spawn', None)
self.__dict__.pop('func', None)
self.__dict__.pop('iterable', None)
class Pool(pool.Pool):
def imap_unordered(self, func, iterable):
"""The same as imap() except that the ordering of the results from the
returned iterator should be considered in arbitrary order."""
return LimitedIMapUnordered.spawn(func, iterable, self.size, self.spawn)