celery.rst 11 KB

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