celery.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. ========
  2. celery
  3. ========
  4. .. currentmodule:: celery
  5. .. module:: celery
  6. .. contents::
  7. :local:
  8. Application
  9. -----------
  10. .. class:: Celery(main='__main__', broker='amqp://localhost//', ...)
  11. :param main: Name of the main module if running as `__main__`.
  12. :keyword broker: URL of the default broker used.
  13. :keyword loader: The loader class, or the name of the loader class to use.
  14. Default is :class:`celery.loaders.app.AppLoader`.
  15. :keyword backend: The result store backend class, or the name of the
  16. backend class to use. Default is the value of the
  17. :setting:`CELERY_RESULT_BACKEND` setting.
  18. :keyword amqp: AMQP object or class name.
  19. :keyword events: Events object or class name.
  20. :keyword log: Log object or class name.
  21. :keyword control: Control object or class name.
  22. :keyword set_as_current: Make this the global current app.
  23. :keyword tasks: A task registry or the name of a registry class.
  24. .. attribute:: Celery.main
  25. Name of the `__main__` module. Required for standalone scripts.
  26. If set this will be used instead of `__main__` when automatically
  27. generating task names.
  28. .. attribute:: Celery.conf
  29. Current configuration.
  30. .. attribute:: Celery.current_task
  31. The instance of the task that is being executed, or :const:`None`.
  32. .. attribute:: Celery.amqp
  33. AMQP related functionality: :class:`~@amqp`.
  34. .. attribute:: Celery.backend
  35. Current backend instance.
  36. .. attribute:: Celery.loader
  37. Current loader instance.
  38. .. attribute:: Celery.control
  39. Remote control: :class:`~@control`.
  40. .. attribute:: Celery.events
  41. Consuming and sending events: :class:`~@events`.
  42. .. attribute:: Celery.log
  43. Logging: :class:`~@log`.
  44. .. attribute:: Celery.tasks
  45. Task registry.
  46. Accessing this attribute will also finalize the app.
  47. .. attribute:: Celery.pool
  48. Broker connection pool: :class:`~@pool`.
  49. This attribute is not related to the workers concurrency pool.
  50. .. attribute:: Celery.Task
  51. Base task class for this app.
  52. .. method:: Celery.bugreport
  53. Returns a string with information useful for the Celery core
  54. developers when reporting a bug.
  55. .. method:: Celery.config_from_object(obj, silent=False)
  56. Reads configuration from object, where object is either
  57. an object or the name of a module to import.
  58. :keyword silent: If true then import errors will be ignored.
  59. .. code-block:: python
  60. >>> celery.config_from_object("myapp.celeryconfig")
  61. >>> from myapp import celeryconfig
  62. >>> celery.config_from_object(celeryconfig)
  63. .. method:: Celery.config_from_envvar(variable_name, silent=False)
  64. Read configuration from environment variable.
  65. The value of the environment variable must be the name
  66. of a module to import.
  67. .. code-block:: python
  68. >>> os.environ["CELERY_CONFIG_MODULE"] = "myapp.celeryconfig"
  69. >>> celery.config_from_envvar("CELERY_CONFIG_MODULE")
  70. .. method:: Celery.start(argv=None)
  71. Run :program:`celery` using `argv`.
  72. Uses :data:`sys.argv` if `argv` is not specified.
  73. .. method:: Celery.task(fun, ...)
  74. Decorator to create a task class out of any callable.
  75. Examples:
  76. .. code-block:: python
  77. @celery.task()
  78. def refresh_feed(url):
  79. return ...
  80. with setting extra options:
  81. .. code-block:: python
  82. @celery.task(exchange="feeds")
  83. def refresh_feed(url):
  84. return ...
  85. .. admonition:: App Binding
  86. For custom apps the task decorator returns proxy
  87. objects, so that the act of creating the task is not performed
  88. until the task is used or the task registry is accessed.
  89. If you are depending on binding to be deferred, then you must
  90. not access any attributes on the returned object until the
  91. application is fully set up (finalized).
  92. .. method:: Celery.send_task(name[, args[, kwargs[, ...]]])
  93. Send task by name.
  94. :param name: Name of task to call (e.g. `"tasks.add"`).
  95. :keyword result_cls: Specify custom result class. Default is
  96. using :meth:`AsyncResult`.
  97. Otherwise supports the same arguments as :meth:`@-Task.apply_async`.
  98. .. attribute:: Celery.AsyncResult
  99. Create new result instance. See :class:`~celery.result.AsyncResult`.
  100. .. attribute:: Celery.GroupResult
  101. Create new taskset result instance.
  102. See :class:`~celery.result.GroupResult`.
  103. .. method:: Celery.worker_main(argv=None)
  104. Run :program:`celeryd` using `argv`.
  105. Uses :data:`sys.argv` if `argv` is not specified."""
  106. .. attribute:: Celery.Worker
  107. Worker application. See :class:`~@Worker`.
  108. .. attribute:: Celery.WorkController
  109. Embeddable worker. See :class:`~@WorkController`.
  110. .. attribute:: Celery.Beat
  111. Celerybeat scheduler application.
  112. See :class:`~@Beat`.
  113. .. method:: Celery.connection(url=default, [ssl, [transport_options={}]])
  114. Establish a connection to the message broker.
  115. :param url: Either the URL or the hostname of the broker to use.
  116. :keyword hostname: URL, Hostname/IP-address of the broker.
  117. If an URL is used, then the other argument below will
  118. be taken from the URL instead.
  119. :keyword userid: Username to authenticate as.
  120. :keyword password: Password to authenticate with
  121. :keyword virtual_host: Virtual host to use (domain).
  122. :keyword port: Port to connect to.
  123. :keyword ssl: Defaults to the :setting:`BROKER_USE_SSL` setting.
  124. :keyword transport: defaults to the :setting:`BROKER_TRANSPORT`
  125. setting.
  126. :returns :class:`kombu.connection.Connection`:
  127. .. method:: Celery.default_connection(connection=None)
  128. For use within a with-statement to get a connection from the pool
  129. if one is not already provided.
  130. :keyword connection: If not provided, then a connection will be
  131. acquired from the connection pool.
  132. .. method:: Celery.mail_admins(subject, body, fail_silently=False)
  133. Sends an email to the admins in the :setting:`ADMINS` setting.
  134. .. method:: Celery.select_queues(queues=[])
  135. Select a subset of queues, where queues must be a list of queue
  136. names to keep.
  137. .. method:: Celery.now()
  138. Returns the current time and date as a :class:`~datetime.datetime`
  139. object.
  140. .. method:: Celery.set_current()
  141. Makes this the current app for this thread.
  142. .. method:: Celery.finalize()
  143. Finalizes the app by loading built-in tasks,
  144. and evaluating pending task decorators
  145. .. attribute:: Celery.Pickler
  146. Helper class used to pickle this application.
  147. Grouping Tasks
  148. --------------
  149. .. class:: group(task1[, task2[, task3[,... taskN]]])
  150. Creates a group of tasks to be executed in parallel.
  151. Example::
  152. >>> res = group([add.s(2, 2), add.s(4, 4)]).apply_async()
  153. >>> res.get()
  154. [4, 8]
  155. The ``apply_async`` method returns :class:`~@GroupResult`.
  156. .. class:: chain(task1[, task2[, task3[,... taskN]]])
  157. Chains tasks together, so that each tasks follows each other
  158. by being applied as a callback of the previous task.
  159. If called with only one argument, then that argument must
  160. be an iterable of tasks to chain.
  161. Example::
  162. >>> res = chain(add.s(2, 2), add.s(4)).apply_async()
  163. is effectively :math:`(2 + 2) + 4)`::
  164. >>> res.get()
  165. 8
  166. Calling a chain will return the result of the last task in the chain.
  167. You can get to the other tasks by following the ``result.parent``'s::
  168. >>> res.parent.get()
  169. 4
  170. .. class:: chord(header[, body])
  171. A chord consists of a header and a body.
  172. The header is a group of tasks that must complete before the callback is
  173. called. A chord is essentially a callback for a group of tasks.
  174. Example::
  175. >>> res = chord([add.s(2, 2), add.s(4, 4)])(sum_task.s())
  176. is effectively :math:`\Sigma ((2 + 2) + (4 + 4))`::
  177. >>> res.get()
  178. 12
  179. The body is applied with the return values of all the header
  180. tasks as a list.
  181. .. class:: subtask(task=None, args=(), kwargs={}, options={})
  182. Describes the arguments and execution options for a single task invocation.
  183. Used as the parts in a :class:`group` or to safely pass
  184. tasks around as callbacks.
  185. Subtasks can also be created from tasks::
  186. >>> add.subtask(args=(), kwargs={}, options={})
  187. or the ``.s()`` shortcut::
  188. >>> add.s(*args, **kwargs)
  189. :param task: Either a task class/instance, or the name of a task.
  190. :keyword args: Positional arguments to apply.
  191. :keyword kwargs: Keyword arguments to apply.
  192. :keyword options: Additional options to :meth:`Task.apply_async`.
  193. Note that if the first argument is a :class:`dict`, the other
  194. arguments will be ignored and the values in the dict will be used
  195. instead.
  196. >>> s = subtask("tasks.add", args=(2, 2))
  197. >>> subtask(s)
  198. {"task": "tasks.add", args=(2, 2), kwargs={}, options={}}
  199. .. method:: subtask.delay(*args, \*\*kwargs)
  200. Shortcut to :meth:`apply_async`.
  201. .. method:: subtask.apply_async(args=(), kwargs={}, ...)
  202. Apply this task asynchronously.
  203. :keyword args: Partial args to be prepended to the existing args.
  204. :keyword kwargs: Partial kwargs to be merged with the existing kwargs.
  205. :keyword options: Partial options to be merged with the existing
  206. options.
  207. See :meth:`~@Task.apply_async`.
  208. .. method:: subtask.apply(args=(), kwargs={}, ...)
  209. Same as :meth:`apply_async` but executed the task inline instead
  210. of sending a task message.
  211. .. method:: subtask.clone(args=(), kwargs={}, ...)
  212. Returns a copy of this subtask.
  213. :keyword args: Partial args to be prepended to the existing args.
  214. :keyword kwargs: Partial kwargs to be merged with the existing kwargs.
  215. :keyword options: Partial options to be merged with the existing
  216. options.
  217. .. method:: subtask.replace(args=None, kwargs=None, options=None)
  218. Replace the args, kwargs or options set for this subtask.
  219. These are only replaced if the selected is not :const:`None`.
  220. .. method:: subtask.link(other_subtask)
  221. Add a callback task to be applied if this task
  222. executes successfully.
  223. :returns: ``other_subtask`` (to work with :func:`~functools.reduce`).
  224. .. method:: subtask.link_error(other_subtask)
  225. Add a callback task to be applied if an error occurs
  226. while executing this task.
  227. :returns: ``other_subtask`` (to work with :func:`~functools.reduce`)
  228. .. method:: subtask.set(...)
  229. Set arbitrary options (same as ``.options.update(...)``).
  230. This is a chaining method call (i.e. it returns itself).
  231. .. method:: subtask.flatten_links()
  232. Gives a recursive list of dependencies (unchain if you will,
  233. but with links intact).
  234. Proxies
  235. -------
  236. .. data:: current_app
  237. The currently set app for this thread.
  238. .. data:: current_task
  239. The task currently being executed
  240. (only set in the worker, or when eager/apply is used).