greenthread.py 10 KB

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