result.py 20 KB

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