celery.rst 7.8 KB

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