celery.rst 8.4 KB

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