result.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.result
  4. ~~~~~~~~~~~~~
  5. Task results/state and groups of results.
  6. """
  7. from __future__ import absolute_import
  8. import time
  9. from collections import deque
  10. from copy import copy
  11. from kombu.utils import cached_property
  12. from kombu.utils.compat import OrderedDict
  13. from . import current_app
  14. from . import states
  15. from ._state import task_join_will_block
  16. from .app import app_or_default
  17. from .datastructures import DependencyGraph, GraphFormatter
  18. from .exceptions import IncompleteStream, TimeoutError
  19. from .five import items, range, string_t, monotonic
  20. __all__ = ['ResultBase', 'AsyncResult', 'ResultSet', 'GroupResult',
  21. 'EagerResult', 'result_from_tuple']
  22. E_WOULDBLOCK = """\
  23. Never call result.get() within a task!
  24. See http://docs.celeryq.org/en/latest/userguide/tasks.html\
  25. #task-synchronous-subtasks
  26. """
  27. def assert_will_not_block():
  28. if task_join_will_block():
  29. raise Exception(E_WOULDBLOCK)
  30. class ResultBase(object):
  31. """Base class for all results"""
  32. #: Parent result (if part of a chain)
  33. parent = None
  34. class AsyncResult(ResultBase):
  35. """Query task state.
  36. :param id: see :attr:`id`.
  37. :keyword backend: see :attr:`backend`.
  38. """
  39. app = None
  40. #: Error raised for timeouts.
  41. TimeoutError = TimeoutError
  42. #: The task's UUID.
  43. id = None
  44. #: The task result backend to use.
  45. backend = None
  46. def __init__(self, id, backend=None, task_name=None,
  47. app=None, parent=None):
  48. self.app = app_or_default(app or self.app)
  49. self.id = id
  50. self.backend = backend or self.app.backend
  51. self.task_name = task_name
  52. self.parent = parent
  53. def as_tuple(self):
  54. parent = self.parent
  55. return (self.id, parent and parent.as_tuple()), None
  56. serializable = as_tuple # XXX compat
  57. def forget(self):
  58. """Forget about (and possibly remove the result of) this task."""
  59. self.backend.forget(self.id)
  60. def revoke(self, connection=None, terminate=False, signal=None,
  61. wait=False, timeout=None):
  62. """Send revoke signal to all workers.
  63. Any worker receiving the task, or having reserved the
  64. task, *must* ignore it.
  65. :keyword terminate: Also terminate the process currently working
  66. on the task (if any).
  67. :keyword signal: Name of signal to send to process if terminate.
  68. Default is TERM.
  69. :keyword wait: Wait for replies from workers. Will wait for 1 second
  70. by default or you can specify a custom ``timeout``.
  71. :keyword timeout: Time in seconds to wait for replies if ``wait``
  72. enabled.
  73. """
  74. self.app.control.revoke(self.id, connection=connection,
  75. terminate=terminate, signal=signal,
  76. reply=wait, timeout=timeout)
  77. def get(self, timeout=None, propagate=True, interval=0.5):
  78. """Wait until task is ready, and return its result.
  79. .. warning::
  80. Waiting for tasks within a task may lead to deadlocks.
  81. Please read :ref:`task-synchronous-subtasks`.
  82. :keyword timeout: How long to wait, in seconds, before the
  83. operation times out.
  84. :keyword propagate: Re-raise exception if the task failed.
  85. :keyword interval: Time to wait (in seconds) before retrying to
  86. retrieve the result. Note that this does not have any effect
  87. when using the amqp result store backend, as it does not
  88. use polling.
  89. :raises celery.exceptions.TimeoutError: if `timeout` is not
  90. :const:`None` and the result does not arrive within `timeout`
  91. seconds.
  92. If the remote call raised an exception then that exception will
  93. be re-raised.
  94. """
  95. assert_will_not_block()
  96. if propagate and self.parent:
  97. for node in reversed(list(self._parents())):
  98. node.get(propagate=True, timeout=timeout, interval=interval)
  99. return self.backend.wait_for(self.id, timeout=timeout,
  100. propagate=propagate,
  101. interval=interval)
  102. wait = get # deprecated alias to :meth:`get`.
  103. def _parents(self):
  104. node = self.parent
  105. while node:
  106. yield node
  107. node = node.parent
  108. def collect(self, intermediate=False, **kwargs):
  109. """Iterator, like :meth:`get` will wait for the task to complete,
  110. but will also follow :class:`AsyncResult` and :class:`ResultSet`
  111. returned by the task, yielding for each result in the tree.
  112. An example would be having the following tasks:
  113. .. code-block:: python
  114. @task()
  115. def A(how_many):
  116. return group(B.s(i) for i in range(how_many))
  117. @task()
  118. def B(i):
  119. return pow2.delay(i)
  120. @task()
  121. def pow2(i):
  122. return i ** 2
  123. Calling :meth:`collect` would return:
  124. .. code-block:: python
  125. >>> from proj.tasks import A
  126. >>> result = A.delay(10)
  127. >>> list(result.collect())
  128. [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
  129. """
  130. for _, R in self.iterdeps(intermediate=intermediate):
  131. yield R, R.get(**kwargs)
  132. def get_leaf(self):
  133. value = None
  134. for _, R in self.iterdeps():
  135. value = R.get()
  136. return value
  137. def iterdeps(self, intermediate=False):
  138. stack = deque([(None, self)])
  139. while stack:
  140. parent, node = stack.popleft()
  141. yield parent, node
  142. if node.ready():
  143. stack.extend((node, child) for child in node.children or [])
  144. else:
  145. if not intermediate:
  146. raise IncompleteStream()
  147. def ready(self):
  148. """Returns :const:`True` if the task has been executed.
  149. If the task is still running, pending, or is waiting
  150. for retry then :const:`False` is returned.
  151. """
  152. return self.state in self.backend.READY_STATES
  153. def successful(self):
  154. """Returns :const:`True` if the task executed successfully."""
  155. return self.state == states.SUCCESS
  156. def failed(self):
  157. """Returns :const:`True` if the task failed."""
  158. return self.state == states.FAILURE
  159. def build_graph(self, intermediate=False, formatter=None):
  160. graph = DependencyGraph(
  161. formatter=formatter or GraphFormatter(root=self.id, shape='oval'),
  162. )
  163. for parent, node in self.iterdeps(intermediate=intermediate):
  164. graph.add_arc(node)
  165. if parent:
  166. graph.add_edge(parent, node)
  167. return graph
  168. def __str__(self):
  169. """`str(self) -> self.id`"""
  170. return str(self.id)
  171. def __hash__(self):
  172. """`hash(self) -> hash(self.id)`"""
  173. return hash(self.id)
  174. def __repr__(self):
  175. return '<{0}: {1}>'.format(type(self).__name__, self.id)
  176. def __eq__(self, other):
  177. if isinstance(other, AsyncResult):
  178. return other.id == self.id
  179. elif isinstance(other, string_t):
  180. return other == self.id
  181. return NotImplemented
  182. def __ne__(self, other):
  183. return not self.__eq__(other)
  184. def __copy__(self):
  185. return self.__class__(
  186. self.id, self.backend, self.task_name, self.app, self.parent,
  187. )
  188. def __reduce__(self):
  189. return self.__class__, self.__reduce_args__()
  190. def __reduce_args__(self):
  191. return self.id, self.backend, self.task_name, None, self.parent
  192. @cached_property
  193. def graph(self):
  194. return self.build_graph()
  195. @property
  196. def supports_native_join(self):
  197. return self.backend.supports_native_join
  198. @property
  199. def children(self):
  200. children = self.backend.get_children(self.id)
  201. if children:
  202. return [result_from_tuple(child, self.app) for child in children]
  203. @property
  204. def result(self):
  205. """When the task has been executed, this contains the return value.
  206. If the task raised an exception, this will be the exception
  207. instance."""
  208. return self.backend.get_result(self.id)
  209. info = result
  210. @property
  211. def traceback(self):
  212. """Get the traceback of a failed task."""
  213. return self.backend.get_traceback(self.id)
  214. @property
  215. def state(self):
  216. """The tasks current state.
  217. Possible values includes:
  218. *PENDING*
  219. The task is waiting for execution.
  220. *STARTED*
  221. The task has been started.
  222. *RETRY*
  223. The task is to be retried, possibly because of failure.
  224. *FAILURE*
  225. The task raised an exception, or has exceeded the retry limit.
  226. The :attr:`result` attribute then contains the
  227. exception raised by the task.
  228. *SUCCESS*
  229. The task executed successfully. The :attr:`result` attribute
  230. then contains the tasks return value.
  231. """
  232. return self.backend.get_status(self.id)
  233. status = state
  234. @property
  235. def task_id(self):
  236. """compat alias to :attr:`id`"""
  237. return self.id
  238. @task_id.setter # noqa
  239. def task_id(self, id):
  240. self.id = id
  241. BaseAsyncResult = AsyncResult # for backwards compatibility.
  242. class ResultSet(ResultBase):
  243. """Working with more than one result.
  244. :param results: List of result instances.
  245. """
  246. app = None
  247. #: List of results in in the set.
  248. results = None
  249. def __init__(self, results, app=None, **kwargs):
  250. self.app = app_or_default(app or self.app)
  251. self.results = results
  252. def add(self, result):
  253. """Add :class:`AsyncResult` as a new member of the set.
  254. Does nothing if the result is already a member.
  255. """
  256. if result not in self.results:
  257. self.results.append(result)
  258. def remove(self, result):
  259. """Remove result from the set; it must be a member.
  260. :raises KeyError: if the result is not a member.
  261. """
  262. if isinstance(result, string_t):
  263. result = self.app.AsyncResult(result)
  264. try:
  265. self.results.remove(result)
  266. except ValueError:
  267. raise KeyError(result)
  268. def discard(self, result):
  269. """Remove result from the set if it is a member.
  270. If it is not a member, do nothing.
  271. """
  272. try:
  273. self.remove(result)
  274. except KeyError:
  275. pass
  276. def update(self, results):
  277. """Update set with the union of itself and an iterable with
  278. results."""
  279. self.results.extend(r for r in results if r not in self.results)
  280. def clear(self):
  281. """Remove all results from this set."""
  282. self.results[:] = [] # don't create new list.
  283. def successful(self):
  284. """Was all of the tasks successful?
  285. :returns: :const:`True` if all of the tasks finished
  286. successfully (i.e. did not raise an exception).
  287. """
  288. return all(result.successful() for result in self.results)
  289. def failed(self):
  290. """Did any of the tasks fail?
  291. :returns: :const:`True` if one of the tasks failed.
  292. (i.e., raised an exception)
  293. """
  294. return any(result.failed() for result in self.results)
  295. def waiting(self):
  296. """Are any of the tasks incomplete?
  297. :returns: :const:`True` if one of the tasks are still
  298. waiting for execution.
  299. """
  300. return any(not result.ready() for result in self.results)
  301. def ready(self):
  302. """Did all of the tasks complete? (either by success of failure).
  303. :returns: :const:`True` if all of the tasks has been
  304. executed.
  305. """
  306. return all(result.ready() for result in self.results)
  307. def completed_count(self):
  308. """Task completion count.
  309. :returns: the number of tasks completed.
  310. """
  311. return sum(int(result.successful()) for result in self.results)
  312. def forget(self):
  313. """Forget about (and possible remove the result of) all the tasks."""
  314. for result in self.results:
  315. result.forget()
  316. def revoke(self, connection=None, terminate=False, signal=None,
  317. wait=False, timeout=None):
  318. """Send revoke signal to all workers for all tasks in the set.
  319. :keyword terminate: Also terminate the process currently working
  320. on the task (if any).
  321. :keyword signal: Name of signal to send to process if terminate.
  322. Default is TERM.
  323. :keyword wait: Wait for replies from worker. Will wait for 1 second
  324. by default or you can specify a custom ``timeout``.
  325. :keyword timeout: Time in seconds to wait for replies if ``wait``
  326. enabled.
  327. """
  328. self.app.control.revoke([r.id for r in self.results],
  329. connection=connection, timeout=timeout,
  330. terminate=terminate, signal=signal, reply=wait)
  331. def __iter__(self):
  332. return iter(self.results)
  333. def __getitem__(self, index):
  334. """`res[i] -> res.results[i]`"""
  335. return self.results[index]
  336. def iterate(self, timeout=None, propagate=True, interval=0.5):
  337. """Iterate over the return values of the tasks as they finish
  338. one by one.
  339. :raises: The exception if any of the tasks raised an exception.
  340. """
  341. elapsed = 0.0
  342. results = OrderedDict((result.id, copy(result))
  343. for result in self.results)
  344. while results:
  345. removed = set()
  346. for task_id, result in items(results):
  347. if result.ready():
  348. yield result.get(timeout=timeout and timeout - elapsed,
  349. propagate=propagate)
  350. removed.add(task_id)
  351. else:
  352. if result.backend.subpolling_interval:
  353. time.sleep(result.backend.subpolling_interval)
  354. for task_id in removed:
  355. results.pop(task_id, None)
  356. time.sleep(interval)
  357. elapsed += interval
  358. if timeout and elapsed >= timeout:
  359. raise TimeoutError('The operation timed out')
  360. def get(self, timeout=None, propagate=True, interval=0.5, callback=None):
  361. """See :meth:`join`
  362. This is here for API compatibility with :class:`AsyncResult`,
  363. in addition it uses :meth:`join_native` if available for the
  364. current result backend.
  365. """
  366. return (self.join_native if self.supports_native_join else self.join)(
  367. timeout=timeout, propagate=propagate,
  368. interval=interval, callback=callback)
  369. def join(self, timeout=None, propagate=True, interval=0.5, callback=None):
  370. """Gathers the results of all tasks as a list in order.
  371. .. note::
  372. This can be an expensive operation for result store
  373. backends that must resort to polling (e.g. database).
  374. You should consider using :meth:`join_native` if your backend
  375. supports it.
  376. .. warning::
  377. Waiting for tasks within a task may lead to deadlocks.
  378. Please see :ref:`task-synchronous-subtasks`.
  379. :keyword timeout: The number of seconds to wait for results before
  380. the operation times out.
  381. :keyword propagate: If any of the tasks raises an exception, the
  382. exception will be re-raised.
  383. :keyword interval: Time to wait (in seconds) before retrying to
  384. retrieve a result from the set. Note that this
  385. does not have any effect when using the amqp
  386. result store backend, as it does not use polling.
  387. :keyword callback: Optional callback to be called for every result
  388. received. Must have signature ``(task_id, value)``
  389. No results will be returned by this function if
  390. a callback is specified. The order of results
  391. is also arbitrary when a callback is used.
  392. :raises celery.exceptions.TimeoutError: if `timeout` is not
  393. :const:`None` and the operation takes longer than `timeout`
  394. seconds.
  395. """
  396. assert_will_not_block()
  397. time_start = monotonic()
  398. remaining = None
  399. results = []
  400. for result in self.results:
  401. remaining = None
  402. if timeout:
  403. remaining = timeout - (monotonic() - time_start)
  404. if remaining <= 0.0:
  405. raise TimeoutError('join operation timed out')
  406. value = result.get(timeout=remaining,
  407. propagate=propagate,
  408. interval=interval)
  409. if callback:
  410. callback(result.id, value)
  411. else:
  412. results.append(value)
  413. return results
  414. def iter_native(self, timeout=None, interval=0.5):
  415. """Backend optimized version of :meth:`iterate`.
  416. .. versionadded:: 2.2
  417. Note that this does not support collecting the results
  418. for different task types using different backends.
  419. This is currently only supported by the amqp, Redis and cache
  420. result backends.
  421. """
  422. results = self.results
  423. if not results:
  424. return iter([])
  425. return results[0].backend.get_many(
  426. set(r.id for r in results), timeout=timeout, interval=interval,
  427. )
  428. def join_native(self, timeout=None, propagate=True,
  429. interval=0.5, callback=None):
  430. """Backend optimized version of :meth:`join`.
  431. .. versionadded:: 2.2
  432. Note that this does not support collecting the results
  433. for different task types using different backends.
  434. This is currently only supported by the amqp, Redis and cache
  435. result backends.
  436. """
  437. assert_will_not_block()
  438. order_index = None if callback else dict(
  439. (result.id, i) for i, result in enumerate(self.results)
  440. )
  441. acc = None if callback else [None for _ in range(len(self))]
  442. for task_id, meta in self.iter_native(timeout, interval):
  443. value = meta['result']
  444. if propagate and meta['status'] in states.PROPAGATE_STATES:
  445. raise value
  446. if callback:
  447. callback(task_id, value)
  448. else:
  449. acc[order_index[task_id]] = value
  450. return acc
  451. def _failed_join_report(self):
  452. return (res for res in self.results
  453. if res.backend.is_cached(res.id) and
  454. res.state in states.PROPAGATE_STATES)
  455. def __len__(self):
  456. return len(self.results)
  457. def __eq__(self, other):
  458. if isinstance(other, ResultSet):
  459. return other.results == self.results
  460. return NotImplemented
  461. def __ne__(self, other):
  462. return not self.__eq__(other)
  463. def __repr__(self):
  464. return '<{0}: [{1}]>'.format(type(self).__name__,
  465. ', '.join(r.id for r in self.results))
  466. @property
  467. def subtasks(self):
  468. """Deprecated alias to :attr:`results`."""
  469. return self.results
  470. @property
  471. def supports_native_join(self):
  472. return self.results[0].supports_native_join
  473. class GroupResult(ResultSet):
  474. """Like :class:`ResultSet`, but with an associated id.
  475. This type is returned by :class:`~celery.group`, and the
  476. deprecated TaskSet, meth:`~celery.task.TaskSet.apply_async` method.
  477. It enables inspection of the tasks state and return values as
  478. a single entity.
  479. :param id: The id of the group.
  480. :param results: List of result instances.
  481. """
  482. #: The UUID of the group.
  483. id = None
  484. #: List/iterator of results in the group
  485. results = None
  486. def __init__(self, id=None, results=None, **kwargs):
  487. self.id = id
  488. ResultSet.__init__(self, results, **kwargs)
  489. def save(self, backend=None):
  490. """Save group-result for later retrieval using :meth:`restore`.
  491. Example::
  492. >>> def save_and_restore(result):
  493. ... result.save()
  494. ... result = GroupResult.restore(result.id)
  495. """
  496. return (backend or self.app.backend).save_group(self.id, self)
  497. def delete(self, backend=None):
  498. """Remove this result if it was previously saved."""
  499. (backend or self.app.backend).delete_group(self.id)
  500. def __reduce__(self):
  501. return self.__class__, self.__reduce_args__()
  502. def __reduce_args__(self):
  503. return self.id, self.results
  504. def __eq__(self, other):
  505. if isinstance(other, GroupResult):
  506. return other.id == self.id and other.results == self.results
  507. return NotImplemented
  508. def __ne__(self, other):
  509. return not self.__eq__(other)
  510. def __repr__(self):
  511. return '<{0}: {1} [{2}]>'.format(type(self).__name__, self.id,
  512. ', '.join(r.id for r in self.results))
  513. def as_tuple(self):
  514. return self.id, [r.as_tuple() for r in self.results]
  515. serializable = as_tuple # XXX compat
  516. @property
  517. def children(self):
  518. return self.results
  519. @classmethod
  520. def restore(self, id, backend=None):
  521. """Restore previously saved group result."""
  522. return (
  523. backend or (self.app.backend if self.app else current_app.backend)
  524. ).restore_group(id)
  525. class TaskSetResult(GroupResult):
  526. """Deprecated version of :class:`GroupResult`"""
  527. def __init__(self, taskset_id, results=None, **kwargs):
  528. # XXX supports the taskset_id kwarg.
  529. # XXX previously the "results" arg was named "subtasks".
  530. if 'subtasks' in kwargs:
  531. results = kwargs['subtasks']
  532. GroupResult.__init__(self, taskset_id, results, **kwargs)
  533. def itersubtasks(self):
  534. """Deprecated. Use ``iter(self.results)`` instead."""
  535. return iter(self.results)
  536. @property
  537. def total(self):
  538. """Deprecated: Use ``len(r)``."""
  539. return len(self)
  540. @property
  541. def taskset_id(self):
  542. """compat alias to :attr:`self.id`"""
  543. return self.id
  544. @taskset_id.setter # noqa
  545. def taskset_id(self, id):
  546. self.id = id
  547. class EagerResult(AsyncResult):
  548. """Result that we know has already been executed."""
  549. task_name = None
  550. def __init__(self, id, ret_value, state, traceback=None):
  551. self.id = id
  552. self._result = ret_value
  553. self._state = state
  554. self._traceback = traceback
  555. def __reduce__(self):
  556. return self.__class__, self.__reduce_args__()
  557. def __reduce_args__(self):
  558. return (self.id, self._result, self._state, self._traceback)
  559. def __copy__(self):
  560. cls, args = self.__reduce__()
  561. return cls(*args)
  562. def ready(self):
  563. return True
  564. def get(self, timeout=None, propagate=True, **kwargs):
  565. if self.successful():
  566. return self.result
  567. elif self.state in states.PROPAGATE_STATES:
  568. if propagate:
  569. raise self.result
  570. return self.result
  571. wait = get
  572. def forget(self):
  573. pass
  574. def revoke(self, *args, **kwargs):
  575. self._state = states.REVOKED
  576. def __repr__(self):
  577. return '<EagerResult: {0.id}>'.format(self)
  578. @property
  579. def result(self):
  580. """The tasks return value"""
  581. return self._result
  582. @property
  583. def state(self):
  584. """The tasks state."""
  585. return self._state
  586. status = state
  587. @property
  588. def traceback(self):
  589. """The traceback if the task failed."""
  590. return self._traceback
  591. @property
  592. def supports_native_join(self):
  593. return False
  594. def result_from_tuple(r, app=None):
  595. # earlier backends may just pickle, so check if
  596. # result is already prepared.
  597. app = app_or_default(app)
  598. Result = app.AsyncResult
  599. if not isinstance(r, ResultBase):
  600. res, nodes = r
  601. if nodes:
  602. return app.GroupResult(
  603. res, [result_from_tuple(child, app) for child in nodes],
  604. )
  605. # previously did not include parent
  606. id, parent = res if isinstance(res, (list, tuple)) else (res, None)
  607. if parent:
  608. parent = result_from_tuple(parent, app)
  609. return Result(id, parent=parent)
  610. return r
  611. from_serializable = result_from_tuple # XXX compat