result.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """
  2. Asynchronous result types.
  3. """
  4. from celery.backends import default_backend
  5. class BaseAsyncResult(object):
  6. """Base class for pending result, supports custom
  7. task meta :attr:`backend`
  8. :param task_id: see :attr:`task_id`.
  9. :param backend: see :attr:`backend`.
  10. .. attribute:: task_id
  11. The unique identifier for this task.
  12. .. attribute:: backend
  13. The task result backend used.
  14. """
  15. def __init__(self, task_id, backend):
  16. self.task_id = task_id
  17. self.backend = backend
  18. def is_done(self):
  19. """Returns ``True`` if the task executed successfully.
  20. :rtype: bool
  21. """
  22. return self.backend.is_done(self.task_id)
  23. def get(self):
  24. """Alias to :meth:`wait`."""
  25. return self.wait()
  26. def wait(self, timeout=None):
  27. """Wait for task, and return the result when it arrives.
  28. :keyword timeout: How long to wait in seconds, before the
  29. operation times out.
  30. :raises celery.timer.TimeoutError: if ``timeout`` is not ``None`` and
  31. the result does not arrive within ``timeout`` seconds.
  32. If the remote call raised an exception then that
  33. exception will be re-raised.
  34. """
  35. return self.backend.wait_for(self.task_id, timeout=timeout)
  36. def ready(self):
  37. """Returns ``True`` if the task executed successfully, or raised
  38. an exception. If the task is still pending, or is waiting for retry
  39. then ``False`` is returned.
  40. :rtype: bool
  41. """
  42. status = self.backend.get_status(self.task_id)
  43. return status != "PENDING" or status != "RETRY"
  44. def successful(self):
  45. """Alias to :meth:`is_done`."""
  46. return self.is_done()
  47. def __str__(self):
  48. """``str(self)`` -> ``self.task_id``"""
  49. return self.task_id
  50. def __repr__(self):
  51. return "<AsyncResult: %s>" % self.task_id
  52. @property
  53. def result(self):
  54. """When the task has been executed, this contains the return value.
  55. If the task raised an exception, this will be the exception instance.
  56. """
  57. if self.status == "DONE" or self.status == "FAILURE":
  58. return self.backend.get_result(self.task_id)
  59. return None
  60. @property
  61. def status(self):
  62. """The current status of the task.
  63. Can be one of the following:
  64. *PENDING*
  65. The task is waiting for execution.
  66. *RETRY*
  67. The task is to be retried, possibly because of failure.
  68. *FAILURE*
  69. The task raised an exception, or has been retried more times
  70. than its limit. The :attr:`result` attribute contains the
  71. exception raised.
  72. *DONE*
  73. The task executed successfully. The :attr:`result` attribute
  74. contains the resulting value.
  75. """
  76. return self.backend.get_status(self.task_id)
  77. class AsyncResult(BaseAsyncResult):
  78. """Pending task result using the default backend.
  79. :param task_id: see :attr:`task_id`.
  80. .. attribute:: task_id
  81. The unique identifier for this task.
  82. .. attribute:: backend
  83. Instance of :class:`celery.backends.DefaultBackend`.
  84. """
  85. def __init__(self, task_id):
  86. super(AsyncResult, self).__init__(task_id, backend=default_backend)