celery.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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: URL, Hostname/IP-address of the broker.
  121. If an URL is used, then the other argument below will
  122. be taken from the URL instead.
  123. :keyword userid: Username to authenticate as.
  124. :keyword password: Password to authenticate with
  125. :keyword virtual_host: Virtual host to use (domain).
  126. :keyword port: Port to connect to.
  127. :keyword ssl: Defaults to the :setting:`BROKER_USE_SSL` setting.
  128. :keyword transport: defaults to the :setting:`BROKER_TRANSPORT`
  129. setting.
  130. :returns :class:`kombu.connection.BrokerConnection`:
  131. .. method:: default_connection(connection=None)
  132. For use within a with-statement to get a connection from the pool
  133. if one is not already provided.
  134. :keyword connection: If not provided, then a connection will be
  135. acquired from the connection pool.
  136. .. method:: mail_admins(subject, body, fail_silently=False)
  137. Sends an email to the admins in the :setting:`ADMINS` setting.
  138. .. method:: select_queues(queues=[])
  139. Select a subset of queues, where queues must be a list of queue
  140. names to keep.
  141. .. method:: now()
  142. Returns the current time and date as a :class:`~datetime.datetime`
  143. object.
  144. .. method:: set_current()
  145. Makes this the current app for this thread.
  146. .. method:: finalize()
  147. Finalizes the app by loading built-in tasks,
  148. and evaluating pending task decorators
  149. .. attribute:: Pickler
  150. Helper class used to pickle this application.
  151. Grouping Tasks
  152. --------------
  153. .. class:: group(tasks=[])
  154. Creates a group of tasks to be executed in parallel.
  155. Example::
  156. >>> res = group([add.s(2, 2), add.s(4, 4)]).apply_async()
  157. >>> res.get()
  158. [4, 8]
  159. The ``apply_async`` method returns :class:`~@TaskSetResult`.
  160. .. class:: chain(*tasks)
  161. Chains tasks together, so that each tasks follows each other
  162. by being applied as a callback of the previous task.
  163. Example::
  164. >>> res = chain(add.s(2, 2), add.s(4)).apply_async()
  165. is effectively :math:`(2 + 2) + 4)`::
  166. >>> res.get()
  167. 8
  168. Applying a chain will return the result of the last task in the chain.
  169. You can get to the other tasks by following the ``result.parent``'s::
  170. >>> res.parent.get()
  171. 4
  172. .. class:: chord(header)(body)
  173. A chord consists of a header and a body.
  174. The header is a group of tasks that must complete before the callback is
  175. called. A chord is essentially a callback for a group of tasks.
  176. Example::
  177. >>> res = chord([add.s(2, 2), add.s(4, 4)])(sum_task.s())
  178. is effectively :math:`\Sigma ((2 + 2) + (4 + 4))`::
  179. >>> res.get()
  180. 12
  181. The body is applied with the return values of all the header
  182. tasks as a list.
  183. .. class:: subtask(task=None, args=(), kwargs={}, options={})
  184. Describes the arguments and execution options for a single task invocation.
  185. Used as the parts in a :class:`group` or to safely pass
  186. tasks around as callbacks.
  187. Subtasks can also be created from tasks::
  188. >>> add.subtask(args=(), kwargs={}, options={})
  189. or the ``.s()`` shortcut::
  190. >>> add.s(*args, **kwargs)
  191. :param task: Either a task class/instance, or the name of a task.
  192. :keyword args: Positional arguments to apply.
  193. :keyword kwargs: Keyword arguments to apply.
  194. :keyword options: Additional options to :meth:`Task.apply_async`.
  195. Note that if the first argument is a :class:`dict`, the other
  196. arguments will be ignored and the values in the dict will be used
  197. instead.
  198. >>> s = subtask("tasks.add", args=(2, 2))
  199. >>> subtask(s)
  200. {"task": "tasks.add", args=(2, 2), kwargs={}, options={}}
  201. .. method:: delay(*args, **kwargs)
  202. Shortcut to :meth:`apply_async`.
  203. .. method:: apply_async(args=(), kwargs={}, **options)
  204. Apply this task asynchronously.
  205. :keyword args: Partial args to be prepended to the existing args.
  206. :keyword kwargs: Partial kwargs to be merged with the existing kwargs.
  207. :keyword options: Partial options to be merged with the existing
  208. options.
  209. See :meth:`~@Task.apply_async`.
  210. .. method:: apply(args=(), kwargs={}, **options)
  211. Same as :meth:`apply_async` but executes inline instead
  212. of sending a task message.
  213. .. method:: clone(args=(), kwargs={}, **options)
  214. Returns a copy of this subtask.
  215. :keyword args: Partial args to be prepended to the existing args.
  216. :keyword kwargs: Partial kwargs to be merged with the existing kwargs.
  217. :keyword options: Partial options to be merged with the existing
  218. options.
  219. .. method:: replace(args=None, kwargs=None, options=None)
  220. Replace the args, kwargs or options set for this subtask.
  221. These are only replaced if the selected is not :const:`None`.
  222. .. method:: link(other_subtask)
  223. Add a callback task to be applied if this task
  224. executes successfully.
  225. .. method:: link_error(other_subtask)
  226. Add a callback task to be applied if an error occurs
  227. while executing this task.
  228. .. method:: set(**options)
  229. Set arbitrary options (same as ``.options.update(...)``).
  230. This is a chaining method call (i.e. it returns itself).
  231. .. method:: 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).