celery.rst 8.4 KB

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