celery.rst 11 KB

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