greenpool.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import traceback
  2. import eventlet
  3. from eventlet import queue
  4. from eventlet.support import greenlets as greenlet
  5. import six
  6. __all__ = ['GreenPool', 'GreenPile']
  7. DEBUG = True
  8. class GreenPool(object):
  9. """The GreenPool class is a pool of green threads.
  10. """
  11. def __init__(self, size=1000):
  12. try:
  13. size = int(size)
  14. except ValueError as e:
  15. msg = 'GreenPool() expect size :: int, actual: {0} {1}'.format(type(size), str(e))
  16. raise TypeError(msg)
  17. if size < 0:
  18. msg = 'GreenPool() expect size >= 0, actual: {0}'.format(repr(size))
  19. raise ValueError(msg)
  20. self.size = size
  21. self.coroutines_running = set()
  22. self.sem = eventlet.Semaphore(size)
  23. self.no_coros_running = eventlet.Event()
  24. def resize(self, new_size):
  25. """ Change the max number of greenthreads doing work at any given time.
  26. If resize is called when there are more than *new_size* greenthreads
  27. already working on tasks, they will be allowed to complete but no new
  28. tasks will be allowed to get launched until enough greenthreads finish
  29. their tasks to drop the overall quantity below *new_size*. Until
  30. then, the return value of free() will be negative.
  31. """
  32. size_delta = new_size - self.size
  33. self.sem.counter += size_delta
  34. self.size = new_size
  35. def running(self):
  36. """ Returns the number of greenthreads that are currently executing
  37. functions in the GreenPool."""
  38. return len(self.coroutines_running)
  39. def free(self):
  40. """ Returns the number of greenthreads available for use.
  41. If zero or less, the next call to :meth:`spawn` or :meth:`spawn_n` will
  42. block the calling greenthread until a slot becomes available."""
  43. return self.sem.counter
  44. def spawn(self, function, *args, **kwargs):
  45. """Run the *function* with its arguments in its own green thread.
  46. Returns the :class:`GreenThread <eventlet.GreenThread>`
  47. object that is running the function, which can be used to retrieve the
  48. results.
  49. If the pool is currently at capacity, ``spawn`` will block until one of
  50. the running greenthreads completes its task and frees up a slot.
  51. This function is reentrant; *function* can call ``spawn`` on the same
  52. pool without risk of deadlocking the whole thing.
  53. """
  54. # if reentering an empty pool, don't try to wait on a coroutine freeing
  55. # itself -- instead, just execute in the current coroutine
  56. current = eventlet.getcurrent()
  57. if self.sem.locked() and current in self.coroutines_running:
  58. # a bit hacky to use the GT without switching to it
  59. gt = eventlet.greenthread.GreenThread(current)
  60. gt.main(function, args, kwargs)
  61. return gt
  62. else:
  63. self.sem.acquire()
  64. gt = eventlet.spawn(function, *args, **kwargs)
  65. if not self.coroutines_running:
  66. self.no_coros_running = eventlet.Event()
  67. self.coroutines_running.add(gt)
  68. gt.link(self._spawn_done)
  69. return gt
  70. def _spawn_n_impl(self, func, args, kwargs, coro):
  71. try:
  72. try:
  73. func(*args, **kwargs)
  74. except (KeyboardInterrupt, SystemExit, greenlet.GreenletExit):
  75. raise
  76. except:
  77. if DEBUG:
  78. traceback.print_exc()
  79. finally:
  80. if coro is None:
  81. return
  82. else:
  83. coro = eventlet.getcurrent()
  84. self._spawn_done(coro)
  85. def spawn_n(self, function, *args, **kwargs):
  86. """Create a greenthread to run the *function*, the same as
  87. :meth:`spawn`. The difference is that :meth:`spawn_n` returns
  88. None; the results of *function* are not retrievable.
  89. """
  90. # if reentering an empty pool, don't try to wait on a coroutine freeing
  91. # itself -- instead, just execute in the current coroutine
  92. current = eventlet.getcurrent()
  93. if self.sem.locked() and current in self.coroutines_running:
  94. self._spawn_n_impl(function, args, kwargs, None)
  95. else:
  96. self.sem.acquire()
  97. g = eventlet.spawn_n(
  98. self._spawn_n_impl,
  99. function, args, kwargs, True)
  100. if not self.coroutines_running:
  101. self.no_coros_running = eventlet.Event()
  102. self.coroutines_running.add(g)
  103. def waitall(self):
  104. """Waits until all greenthreads in the pool are finished working."""
  105. assert eventlet.getcurrent() not in self.coroutines_running, \
  106. "Calling waitall() from within one of the " \
  107. "GreenPool's greenthreads will never terminate."
  108. if self.running():
  109. self.no_coros_running.wait()
  110. def _spawn_done(self, coro):
  111. self.sem.release()
  112. if coro is not None:
  113. self.coroutines_running.remove(coro)
  114. # if done processing (no more work is waiting for processing),
  115. # we can finish off any waitall() calls that might be pending
  116. if self.sem.balance == self.size:
  117. self.no_coros_running.send(None)
  118. def waiting(self):
  119. """Return the number of greenthreads waiting to spawn.
  120. """
  121. if self.sem.balance < 0:
  122. return -self.sem.balance
  123. else:
  124. return 0
  125. def _do_map(self, func, it, gi):
  126. for args in it:
  127. gi.spawn(func, *args)
  128. gi.done_spawning()
  129. def starmap(self, function, iterable):
  130. """This is the same as :func:`itertools.starmap`, except that *func* is
  131. executed in a separate green thread for each item, with the concurrency
  132. limited by the pool's size. In operation, starmap consumes a constant
  133. amount of memory, proportional to the size of the pool, and is thus
  134. suited for iterating over extremely long input lists.
  135. """
  136. if function is None:
  137. function = lambda *a: a
  138. # We use a whole separate greenthread so its spawn() calls can block
  139. # without blocking OUR caller. On the other hand, we must assume that
  140. # our caller will immediately start trying to iterate over whatever we
  141. # return. If that were a GreenPile, our caller would always see an
  142. # empty sequence because the hub hasn't even entered _do_map() yet --
  143. # _do_map() hasn't had a chance to spawn a single greenthread on this
  144. # GreenPool! A GreenMap is safe to use with different producer and
  145. # consumer greenthreads, because it doesn't raise StopIteration until
  146. # the producer has explicitly called done_spawning().
  147. gi = GreenMap(self.size)
  148. eventlet.spawn_n(self._do_map, function, iterable, gi)
  149. return gi
  150. def imap(self, function, *iterables):
  151. """This is the same as :func:`itertools.imap`, and has the same
  152. concurrency and memory behavior as :meth:`starmap`.
  153. It's quite convenient for, e.g., farming out jobs from a file::
  154. def worker(line):
  155. return do_something(line)
  156. pool = GreenPool()
  157. for result in pool.imap(worker, open("filename", 'r')):
  158. print(result)
  159. """
  160. return self.starmap(function, six.moves.zip(*iterables))
  161. class GreenPile(object):
  162. """GreenPile is an abstraction representing a bunch of I/O-related tasks.
  163. Construct a GreenPile with an existing GreenPool object. The GreenPile will
  164. then use that pool's concurrency as it processes its jobs. There can be
  165. many GreenPiles associated with a single GreenPool.
  166. A GreenPile can also be constructed standalone, not associated with any
  167. GreenPool. To do this, construct it with an integer size parameter instead
  168. of a GreenPool.
  169. It is not advisable to iterate over a GreenPile in a different greenthread
  170. than the one which is calling spawn. The iterator will exit early in that
  171. situation.
  172. """
  173. def __init__(self, size_or_pool=1000):
  174. if isinstance(size_or_pool, GreenPool):
  175. self.pool = size_or_pool
  176. else:
  177. self.pool = GreenPool(size_or_pool)
  178. self.waiters = queue.LightQueue()
  179. self.counter = 0
  180. def spawn(self, func, *args, **kw):
  181. """Runs *func* in its own green thread, with the result available by
  182. iterating over the GreenPile object."""
  183. self.counter += 1
  184. try:
  185. gt = self.pool.spawn(func, *args, **kw)
  186. self.waiters.put(gt)
  187. except:
  188. self.counter -= 1
  189. raise
  190. def __iter__(self):
  191. return self
  192. def next(self):
  193. """Wait for the next result, suspending the current greenthread until it
  194. is available. Raises StopIteration when there are no more results."""
  195. if self.counter == 0:
  196. raise StopIteration()
  197. return self._next()
  198. __next__ = next
  199. def _next(self):
  200. try:
  201. return self.waiters.get().wait()
  202. finally:
  203. self.counter -= 1
  204. # this is identical to GreenPile but it blocks on spawn if the results
  205. # aren't consumed, and it doesn't generate its own StopIteration exception,
  206. # instead relying on the spawning process to send one in when it's done
  207. class GreenMap(GreenPile):
  208. def __init__(self, size_or_pool):
  209. super(GreenMap, self).__init__(size_or_pool)
  210. self.waiters = queue.LightQueue(maxsize=self.pool.size)
  211. def done_spawning(self):
  212. self.spawn(lambda: StopIteration())
  213. def next(self):
  214. val = self._next()
  215. if isinstance(val, StopIteration):
  216. raise val
  217. else:
  218. return val
  219. __next__ = next