celery.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. ===========================================
  2. :mod:`celery` --- Distributed processing
  3. ===========================================
  4. .. currentmodule:: celery
  5. .. module:: celery
  6. :synopsis: Distributed processing
  7. .. moduleauthor:: Ask Solem <ask@celeryproject.org>
  8. .. sectionauthor:: Ask Solem <ask@celeryproject.org>
  9. --------------
  10. This module is the main entry-point for the Celery API.
  11. It includes commonly needed things for calling tasks,
  12. and creating Celery applications.
  13. ===================== ===================================================
  14. :class:`Celery` celery application instance
  15. :class:`group` group tasks together
  16. :class:`chain` chain tasks together
  17. :class:`chord` chords enable callbacks for groups
  18. :class:`signature` object describing a task invocation
  19. :data:`current_app` proxy to the current application instance
  20. :data:`current_task` proxy to the currently executing task
  21. ===================== ===================================================
  22. :class:`Celery` application objects
  23. -----------------------------------
  24. .. versionadded:: 2.5
  25. .. autoclass:: Celery
  26. .. autoattribute:: user_options
  27. .. autoattribute:: steps
  28. .. autoattribute:: current_task
  29. .. autoattribute:: amqp
  30. .. autoattribute:: backend
  31. .. autoattribute:: loader
  32. .. autoattribute:: control
  33. .. autoattribute:: events
  34. .. autoattribute:: log
  35. .. autoattribute:: tasks
  36. .. autoattribute:: pool
  37. .. autoattribute:: producer_pool
  38. .. autoattribute:: Task
  39. .. autoattribute:: timezone
  40. .. automethod:: close
  41. .. automethod:: signature
  42. .. automethod:: bugreport
  43. .. automethod:: config_from_object
  44. .. automethod:: config_from_envvar
  45. .. automethod:: autodiscover_tasks
  46. .. automethod:: add_defaults
  47. .. automethod:: setup_security
  48. .. automethod:: start
  49. .. automethod:: task
  50. .. automethod:: send_task
  51. .. autoattribute:: AsyncResult
  52. .. autoattribute:: GroupResult
  53. .. automethod:: worker_main
  54. .. autoattribute:: Worker
  55. .. autoattribute:: WorkController
  56. .. autoattribute:: Beat
  57. .. automethod:: connection
  58. .. automethod:: connection_or_acquire
  59. .. automethod:: producer_or_acquire
  60. .. automethod:: mail_admins
  61. .. automethod:: select_queues
  62. .. automethod:: now
  63. .. automethod:: set_current
  64. .. automethod:: finalize
  65. .. data:: on_configure
  66. Signal sent when app is loading configuration.
  67. .. data:: on_after_configure
  68. Signal sent after app has prepared the configuration.
  69. .. data:: on_after_finalize
  70. Signal sent after app has been finalized.
  71. Canvas primitives
  72. -----------------
  73. See :ref:`guide-canvas` for more about creating task workflows.
  74. .. class:: group(task1[, task2[, task3[,… taskN]]])
  75. Creates a group of tasks to be executed in parallel.
  76. Example::
  77. >>> res = group([add.s(2, 2), add.s(4, 4)])()
  78. >>> res.get()
  79. [4, 8]
  80. A group is lazy so you must call it to take action and evaluate
  81. the group.
  82. Will return a `group` task that when called will then call all of the
  83. tasks in the group (and return a :class:`GroupResult` instance
  84. that can be used to inspect the state of the group).
  85. .. class:: chain(task1[, task2[, task3[,… taskN]]])
  86. Chains tasks together, so that each tasks follows each other
  87. by being applied as a callback of the previous task.
  88. If called with only one argument, then that argument must
  89. be an iterable of tasks to chain.
  90. Example::
  91. >>> res = chain(add.s(2, 2), add.s(4))()
  92. is effectively :math:`(2 + 2) + 4)`::
  93. >>> res.get()
  94. 8
  95. Calling a chain will return the result of the last task in the chain.
  96. You can get to the other tasks by following the ``result.parent``'s::
  97. >>> res.parent.get()
  98. 4
  99. .. class:: chord(header[, body])
  100. A chord consists of a header and a body.
  101. The header is a group of tasks that must complete before the callback is
  102. called. A chord is essentially a callback for a group of tasks.
  103. Example::
  104. >>> res = chord([add.s(2, 2), add.s(4, 4)])(sum_task.s())
  105. is effectively :math:`\Sigma ((2 + 2) + (4 + 4))`::
  106. >>> res.get()
  107. 12
  108. The body is applied with the return values of all the header
  109. tasks as a list.
  110. .. class:: signature(task=None, args=(), kwargs={}, options={})
  111. Describes the arguments and execution options for a single task invocation.
  112. Used as the parts in a :class:`group` or to safely pass
  113. tasks around as callbacks.
  114. Signatures can also be created from tasks::
  115. >>> add.signature(args=(), kwargs={}, options={})
  116. or the ``.s()`` shortcut::
  117. >>> add.s(*args, **kwargs)
  118. :param task: Either a task class/instance, or the name of a task.
  119. :keyword args: Positional arguments to apply.
  120. :keyword kwargs: Keyword arguments to apply.
  121. :keyword options: Additional options to :meth:`Task.apply_async`.
  122. Note that if the first argument is a :class:`dict`, the other
  123. arguments will be ignored and the values in the dict will be used
  124. instead.
  125. >>> s = signature('tasks.add', args=(2, 2))
  126. >>> signature(s)
  127. {'task': 'tasks.add', args=(2, 2), kwargs={}, options={}}
  128. .. method:: signature.__call__(*args \*\*kwargs)
  129. Call the task directly (in the current process).
  130. .. method:: signature.delay(*args, \*\*kwargs)
  131. Shortcut to :meth:`apply_async`.
  132. .. method:: signature.apply_async(args=(), kwargs={}, …)
  133. Apply this task asynchronously.
  134. :keyword args: Partial args to be prepended to the existing args.
  135. :keyword kwargs: Partial kwargs to be merged with the existing kwargs.
  136. :keyword options: Partial options to be merged with the existing
  137. options.
  138. See :meth:`~@Task.apply_async`.
  139. .. method:: signature.apply(args=(), kwargs={}, …)
  140. Same as :meth:`apply_async` but executed the task inline instead
  141. of sending a task message.
  142. .. method:: signature.freeze(_id=None)
  143. Finalize the signature by adding a concrete task id.
  144. The task will not be called and you should not call the signature
  145. twice after freezing it as that will result in two task messages
  146. using the same task id.
  147. :returns: :class:`@AsyncResult` instance.
  148. .. method:: signature.clone(args=(), kwargs={}, …)
  149. Return a copy of this signature.
  150. :keyword args: Partial args to be prepended to the existing args.
  151. :keyword kwargs: Partial kwargs to be merged with the existing kwargs.
  152. :keyword options: Partial options to be merged with the existing
  153. options.
  154. .. method:: signature.replace(args=None, kwargs=None, options=None)
  155. Replace the args, kwargs or options set for this signature.
  156. These are only replaced if the selected is not :const:`None`.
  157. .. method:: signature.link(other_signature)
  158. Add a callback task to be applied if this task
  159. executes successfully.
  160. :returns: ``other_signature`` (to work with :func:`~functools.reduce`).
  161. .. method:: signature.link_error(other_signature)
  162. Add a callback task to be applied if an error occurs
  163. while executing this task.
  164. :returns: ``other_signature`` (to work with :func:`~functools.reduce`)
  165. .. method:: signature.set(…)
  166. Set arbitrary options (same as ``.options.update(…)``).
  167. This is a chaining method call (i.e. it will return ``self``).
  168. .. method:: signature.flatten_links()
  169. Gives a recursive list of dependencies (unchain if you will,
  170. but with links intact).
  171. Proxies
  172. -------
  173. .. data:: current_app
  174. The currently set app for this thread.
  175. .. data:: current_task
  176. The task currently being executed
  177. (only set in the worker, or when eager/apply is used).