greenthread.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. from collections import deque
  2. import sys
  3. from eventlet import event
  4. from eventlet import hubs
  5. from eventlet import support
  6. from eventlet import timeout
  7. from eventlet.hubs import timer
  8. from eventlet.support import greenlets as greenlet, six
  9. import warnings
  10. __all__ = ['getcurrent', 'sleep', 'spawn', 'spawn_n',
  11. 'kill',
  12. 'spawn_after', 'spawn_after_local', 'GreenThread']
  13. getcurrent = greenlet.getcurrent
  14. def sleep(seconds=0):
  15. """Yield control to another eligible coroutine until at least *seconds* have
  16. elapsed.
  17. *seconds* may be specified as an integer, or a float if fractional seconds
  18. are desired. Calling :func:`~greenthread.sleep` with *seconds* of 0 is the
  19. canonical way of expressing a cooperative yield. For example, if one is
  20. looping over a large list performing an expensive calculation without
  21. calling any socket methods, it's a good idea to call ``sleep(0)``
  22. occasionally; otherwise nothing else will run.
  23. """
  24. hub = hubs.get_hub()
  25. current = getcurrent()
  26. assert hub.greenlet is not current, 'do not call blocking functions from the mainloop'
  27. timer = hub.schedule_call_global(seconds, current.switch)
  28. try:
  29. hub.switch()
  30. finally:
  31. timer.cancel()
  32. def spawn(func, *args, **kwargs):
  33. """Create a greenthread to run ``func(*args, **kwargs)``. Returns a
  34. :class:`GreenThread` object which you can use to get the results of the
  35. call.
  36. Execution control returns immediately to the caller; the created greenthread
  37. is merely scheduled to be run at the next available opportunity.
  38. Use :func:`spawn_after` to arrange for greenthreads to be spawned
  39. after a finite delay.
  40. """
  41. hub = hubs.get_hub()
  42. g = GreenThread(hub.greenlet)
  43. hub.schedule_call_global(0, g.switch, func, args, kwargs)
  44. return g
  45. def spawn_n(func, *args, **kwargs):
  46. """Same as :func:`spawn`, but returns a ``greenlet`` object from
  47. which it is not possible to retrieve either a return value or
  48. whether it raised any exceptions. This is faster than
  49. :func:`spawn`; it is fastest if there are no keyword arguments.
  50. If an exception is raised in the function, spawn_n prints a stack
  51. trace; the print can be disabled by calling
  52. :func:`eventlet.debug.hub_exceptions` with False.
  53. """
  54. return _spawn_n(0, func, args, kwargs)[1]
  55. def spawn_after(seconds, func, *args, **kwargs):
  56. """Spawns *func* after *seconds* have elapsed. It runs as scheduled even if
  57. the current greenthread has completed.
  58. *seconds* may be specified as an integer, or a float if fractional seconds
  59. are desired. The *func* will be called with the given *args* and
  60. keyword arguments *kwargs*, and will be executed within its own greenthread.
  61. The return value of :func:`spawn_after` is a :class:`GreenThread` object,
  62. which can be used to retrieve the results of the call.
  63. To cancel the spawn and prevent *func* from being called,
  64. call :meth:`GreenThread.cancel` on the return value of :func:`spawn_after`.
  65. This will not abort the function if it's already started running, which is
  66. generally the desired behavior. If terminating *func* regardless of whether
  67. it's started or not is the desired behavior, call :meth:`GreenThread.kill`.
  68. """
  69. hub = hubs.get_hub()
  70. g = GreenThread(hub.greenlet)
  71. hub.schedule_call_global(seconds, g.switch, func, args, kwargs)
  72. return g
  73. def spawn_after_local(seconds, func, *args, **kwargs):
  74. """Spawns *func* after *seconds* have elapsed. The function will NOT be
  75. called if the current greenthread has exited.
  76. *seconds* may be specified as an integer, or a float if fractional seconds
  77. are desired. The *func* will be called with the given *args* and
  78. keyword arguments *kwargs*, and will be executed within its own greenthread.
  79. The return value of :func:`spawn_after` is a :class:`GreenThread` object,
  80. which can be used to retrieve the results of the call.
  81. To cancel the spawn and prevent *func* from being called,
  82. call :meth:`GreenThread.cancel` on the return value. This will not abort the
  83. function if it's already started running. If terminating *func* regardless
  84. of whether it's started or not is the desired behavior, call
  85. :meth:`GreenThread.kill`.
  86. """
  87. hub = hubs.get_hub()
  88. g = GreenThread(hub.greenlet)
  89. hub.schedule_call_local(seconds, g.switch, func, args, kwargs)
  90. return g
  91. def call_after_global(seconds, func, *args, **kwargs):
  92. warnings.warn(
  93. "call_after_global is renamed to spawn_after, which"
  94. "has the same signature and semantics (plus a bit extra). Please do a"
  95. " quick search-and-replace on your codebase, thanks!",
  96. DeprecationWarning, stacklevel=2)
  97. return _spawn_n(seconds, func, args, kwargs)[0]
  98. def call_after_local(seconds, function, *args, **kwargs):
  99. warnings.warn(
  100. "call_after_local is renamed to spawn_after_local, which"
  101. "has the same signature and semantics (plus a bit extra).",
  102. DeprecationWarning, stacklevel=2)
  103. hub = hubs.get_hub()
  104. g = greenlet.greenlet(function, parent=hub.greenlet)
  105. t = hub.schedule_call_local(seconds, g.switch, *args, **kwargs)
  106. return t
  107. call_after = call_after_local
  108. def exc_after(seconds, *throw_args):
  109. warnings.warn("Instead of exc_after, which is deprecated, use "
  110. "Timeout(seconds, exception)",
  111. DeprecationWarning, stacklevel=2)
  112. if seconds is None: # dummy argument, do nothing
  113. return timer.Timer(seconds, lambda: None)
  114. hub = hubs.get_hub()
  115. return hub.schedule_call_local(seconds, getcurrent().throw, *throw_args)
  116. # deprecate, remove
  117. TimeoutError, with_timeout = (
  118. support.wrap_deprecated(old, new)(fun) for old, new, fun in (
  119. ('greenthread.TimeoutError', 'Timeout', timeout.Timeout),
  120. ('greenthread.with_timeout', 'with_timeout', timeout.with_timeout),
  121. ))
  122. def _spawn_n(seconds, func, args, kwargs):
  123. hub = hubs.get_hub()
  124. g = greenlet.greenlet(func, parent=hub.greenlet)
  125. t = hub.schedule_call_global(seconds, g.switch, *args, **kwargs)
  126. return t, g
  127. class GreenThread(greenlet.greenlet):
  128. """The GreenThread class is a type of Greenlet which has the additional
  129. property of being able to retrieve the return value of the main function.
  130. Do not construct GreenThread objects directly; call :func:`spawn` to get one.
  131. """
  132. def __init__(self, parent):
  133. greenlet.greenlet.__init__(self, self.main, parent)
  134. self._exit_event = event.Event()
  135. self._resolving_links = False
  136. def wait(self):
  137. """ Returns the result of the main function of this GreenThread. If the
  138. result is a normal return value, :meth:`wait` returns it. If it raised
  139. an exception, :meth:`wait` will raise the same exception (though the
  140. stack trace will unavoidably contain some frames from within the
  141. greenthread module)."""
  142. return self._exit_event.wait()
  143. def link(self, func, *curried_args, **curried_kwargs):
  144. """ Set up a function to be called with the results of the GreenThread.
  145. The function must have the following signature::
  146. def func(gt, [curried args/kwargs]):
  147. When the GreenThread finishes its run, it calls *func* with itself
  148. and with the `curried arguments <http://en.wikipedia.org/wiki/Currying>`_ supplied
  149. at link-time. If the function wants to retrieve the result of the GreenThread,
  150. it should call wait() on its first argument.
  151. Note that *func* is called within execution context of
  152. the GreenThread, so it is possible to interfere with other linked
  153. functions by doing things like switching explicitly to another
  154. greenthread.
  155. """
  156. self._exit_funcs = getattr(self, '_exit_funcs', deque())
  157. self._exit_funcs.append((func, curried_args, curried_kwargs))
  158. if self._exit_event.ready():
  159. self._resolve_links()
  160. def unlink(self, func, *curried_args, **curried_kwargs):
  161. """ remove linked function set by :meth:`link`
  162. Remove successfully return True, otherwise False
  163. """
  164. if not getattr(self, '_exit_funcs', None):
  165. return False
  166. try:
  167. self._exit_funcs.remove((func, curried_args, curried_kwargs))
  168. return True
  169. except ValueError:
  170. return False
  171. def main(self, function, args, kwargs):
  172. try:
  173. result = function(*args, **kwargs)
  174. except:
  175. self._exit_event.send_exception(*sys.exc_info())
  176. self._resolve_links()
  177. raise
  178. else:
  179. self._exit_event.send(result)
  180. self._resolve_links()
  181. def _resolve_links(self):
  182. # ca and ckw are the curried function arguments
  183. if self._resolving_links:
  184. return
  185. self._resolving_links = True
  186. try:
  187. exit_funcs = getattr(self, '_exit_funcs', deque())
  188. while exit_funcs:
  189. f, ca, ckw = exit_funcs.popleft()
  190. f(self, *ca, **ckw)
  191. finally:
  192. self._resolving_links = False
  193. def kill(self, *throw_args):
  194. """Kills the greenthread using :func:`kill`. After being killed
  195. all calls to :meth:`wait` will raise *throw_args* (which default
  196. to :class:`greenlet.GreenletExit`)."""
  197. return kill(self, *throw_args)
  198. def cancel(self, *throw_args):
  199. """Kills the greenthread using :func:`kill`, but only if it hasn't
  200. already started running. After being canceled,
  201. all calls to :meth:`wait` will raise *throw_args* (which default
  202. to :class:`greenlet.GreenletExit`)."""
  203. return cancel(self, *throw_args)
  204. def cancel(g, *throw_args):
  205. """Like :func:`kill`, but only terminates the greenthread if it hasn't
  206. already started execution. If the grenthread has already started
  207. execution, :func:`cancel` has no effect."""
  208. if not g:
  209. kill(g, *throw_args)
  210. def kill(g, *throw_args):
  211. """Terminates the target greenthread by raising an exception into it.
  212. Whatever that greenthread might be doing; be it waiting for I/O or another
  213. primitive, it sees an exception right away.
  214. By default, this exception is GreenletExit, but a specific exception
  215. may be specified. *throw_args* should be the same as the arguments to
  216. raise; either an exception instance or an exc_info tuple.
  217. Calling :func:`kill` causes the calling greenthread to cooperatively yield.
  218. """
  219. if g.dead:
  220. return
  221. hub = hubs.get_hub()
  222. if not g:
  223. # greenlet hasn't started yet and therefore throw won't work
  224. # on its own; semantically we want it to be as though the main
  225. # method never got called
  226. def just_raise(*a, **kw):
  227. if throw_args:
  228. six.reraise(throw_args[0], throw_args[1], throw_args[2])
  229. else:
  230. raise greenlet.GreenletExit()
  231. g.run = just_raise
  232. if isinstance(g, GreenThread):
  233. # it's a GreenThread object, so we want to call its main
  234. # method to take advantage of the notification
  235. try:
  236. g.main(just_raise, (), {})
  237. except:
  238. pass
  239. current = getcurrent()
  240. if current is not hub.greenlet:
  241. # arrange to wake the caller back up immediately
  242. hub.ensure_greenlet()
  243. hub.schedule_call_global(0, current.switch)
  244. g.throw(*throw_args)