celery.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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.close
  53. Cleans-up after application, like closing any pool connections.
  54. Only necessary for dynamically created apps for which you can
  55. use the with statement::
  56. with Celery(...) as app:
  57. with app.connection() as conn:
  58. pass
  59. .. method:: Celery.bugreport
  60. Returns a string with information useful for the Celery core
  61. developers when reporting a bug.
  62. .. method:: Celery.config_from_object(obj, silent=False)
  63. Reads configuration from object, where object is either
  64. an object or the name of a module to import.
  65. :keyword silent: If true then import errors will be ignored.
  66. .. code-block:: python
  67. >>> celery.config_from_object("myapp.celeryconfig")
  68. >>> from myapp import celeryconfig
  69. >>> celery.config_from_object(celeryconfig)
  70. .. method:: Celery.config_from_envvar(variable_name, silent=False)
  71. Read configuration from environment variable.
  72. The value of the environment variable must be the name
  73. of a module to import.
  74. .. code-block:: python
  75. >>> os.environ["CELERY_CONFIG_MODULE"] = "myapp.celeryconfig"
  76. >>> celery.config_from_envvar("CELERY_CONFIG_MODULE")
  77. .. method:: Celery.add_defaults(d)
  78. Add default configuration from dict ``d``.
  79. If the argument is a callable function then it will be regarded
  80. as a promise, and it won't be loaded until the configuration is
  81. actually needed.
  82. This method can be compared to::
  83. >>> celery.conf.update(d)
  84. with a difference that 1) no copy will be made and 2) the dict will
  85. not be transferred when the worker spawns child processes, so
  86. it's important that the same configuration happens at import time
  87. when pickle restores the object on the other side.
  88. .. method:: Celery.start(argv=None)
  89. Run :program:`celery` using `argv`.
  90. Uses :data:`sys.argv` if `argv` is not specified.
  91. .. method:: Celery.task(fun, ...)
  92. Decorator to create a task class out of any callable.
  93. Examples:
  94. .. code-block:: python
  95. @celery.task
  96. def refresh_feed(url):
  97. return ...
  98. with setting extra options:
  99. .. code-block:: python
  100. @celery.task(exchange="feeds")
  101. def refresh_feed(url):
  102. return ...
  103. .. admonition:: App Binding
  104. For custom apps the task decorator returns proxy
  105. objects, so that the act of creating the task is not performed
  106. until the task is used or the task registry is accessed.
  107. If you are depending on binding to be deferred, then you must
  108. not access any attributes on the returned object until the
  109. application is fully set up (finalized).
  110. .. method:: Celery.send_task(name[, args[, kwargs[, ...]]])
  111. Send task by name.
  112. :param name: Name of task to call (e.g. `"tasks.add"`).
  113. :keyword result_cls: Specify custom result class. Default is
  114. using :meth:`AsyncResult`.
  115. Otherwise supports the same arguments as :meth:`@-Task.apply_async`.
  116. .. attribute:: Celery.AsyncResult
  117. Create new result instance. See :class:`~celery.result.AsyncResult`.
  118. .. attribute:: Celery.GroupResult
  119. Create new taskset result instance.
  120. See :class:`~celery.result.GroupResult`.
  121. .. method:: Celery.worker_main(argv=None)
  122. Run :program:`celeryd` using `argv`.
  123. Uses :data:`sys.argv` if `argv` is not specified."""
  124. .. attribute:: Celery.Worker
  125. Worker application. See :class:`~@Worker`.
  126. .. attribute:: Celery.WorkController
  127. Embeddable worker. See :class:`~@WorkController`.
  128. .. attribute:: Celery.Beat
  129. Celerybeat scheduler application.
  130. See :class:`~@Beat`.
  131. .. method:: Celery.connection(url=default, [ssl, [transport_options={}]])
  132. Establish a connection to the message broker.
  133. :param url: Either the URL or the hostname of the broker to use.
  134. :keyword hostname: URL, Hostname/IP-address of the broker.
  135. If an URL is used, then the other argument below will
  136. be taken from the URL instead.
  137. :keyword userid: Username to authenticate as.
  138. :keyword password: Password to authenticate with
  139. :keyword virtual_host: Virtual host to use (domain).
  140. :keyword port: Port to connect to.
  141. :keyword ssl: Defaults to the :setting:`BROKER_USE_SSL` setting.
  142. :keyword transport: defaults to the :setting:`BROKER_TRANSPORT`
  143. setting.
  144. :returns :class:`kombu.connection.Connection`:
  145. .. method:: Celery.connection_or_acquire(connection=None)
  146. For use within a with-statement to get a connection from the pool
  147. if one is not already provided.
  148. :keyword connection: If not provided, then a connection will be
  149. acquired from the connection pool.
  150. .. method:: Celery.producer_or_acquire(producer=None)
  151. For use within a with-statement to get a producer from the pool
  152. if one is not already provided
  153. :keyword producer: If not provided, then a producer will be
  154. acquired from the producer pool.
  155. .. method:: Celery.mail_admins(subject, body, fail_silently=False)
  156. Sends an email to the admins in the :setting:`ADMINS` setting.
  157. .. method:: Celery.select_queues(queues=[])
  158. Select a subset of queues, where queues must be a list of queue
  159. names to keep.
  160. .. method:: Celery.now()
  161. Returns the current time and date as a :class:`~datetime.datetime`
  162. object.
  163. .. method:: Celery.set_current()
  164. Makes this the current app for this thread.
  165. .. method:: Celery.finalize()
  166. Finalizes the app by loading built-in tasks,
  167. and evaluating pending task decorators
  168. .. attribute:: Celery.Pickler
  169. Helper class used to pickle this application.
  170. Grouping Tasks
  171. --------------
  172. .. class:: group(task1[, task2[, task3[,... taskN]]])
  173. Creates a group of tasks to be executed in parallel.
  174. Example::
  175. >>> res = group([add.s(2, 2), add.s(4, 4)]).apply_async()
  176. >>> res.get()
  177. [4, 8]
  178. The ``apply_async`` method returns :class:`~@GroupResult`.
  179. .. class:: chain(task1[, task2[, task3[,... taskN]]])
  180. Chains tasks together, so that each tasks follows each other
  181. by being applied as a callback of the previous task.
  182. If called with only one argument, then that argument must
  183. be an iterable of tasks to chain.
  184. Example::
  185. >>> res = chain(add.s(2, 2), add.s(4)).apply_async()
  186. is effectively :math:`(2 + 2) + 4)`::
  187. >>> res.get()
  188. 8
  189. Calling a chain will return the result of the last task in the chain.
  190. You can get to the other tasks by following the ``result.parent``'s::
  191. >>> res.parent.get()
  192. 4
  193. .. class:: chord(header[, body])
  194. A chord consists of a header and a body.
  195. The header is a group of tasks that must complete before the callback is
  196. called. A chord is essentially a callback for a group of tasks.
  197. Example::
  198. >>> res = chord([add.s(2, 2), add.s(4, 4)])(sum_task.s())
  199. is effectively :math:`\Sigma ((2 + 2) + (4 + 4))`::
  200. >>> res.get()
  201. 12
  202. The body is applied with the return values of all the header
  203. tasks as a list.
  204. .. class:: subtask(task=None, args=(), kwargs={}, options={})
  205. Describes the arguments and execution options for a single task invocation.
  206. Used as the parts in a :class:`group` or to safely pass
  207. tasks around as callbacks.
  208. Subtasks can also be created from tasks::
  209. >>> add.subtask(args=(), kwargs={}, options={})
  210. or the ``.s()`` shortcut::
  211. >>> add.s(*args, **kwargs)
  212. :param task: Either a task class/instance, or the name of a task.
  213. :keyword args: Positional arguments to apply.
  214. :keyword kwargs: Keyword arguments to apply.
  215. :keyword options: Additional options to :meth:`Task.apply_async`.
  216. Note that if the first argument is a :class:`dict`, the other
  217. arguments will be ignored and the values in the dict will be used
  218. instead.
  219. >>> s = subtask("tasks.add", args=(2, 2))
  220. >>> subtask(s)
  221. {"task": "tasks.add", args=(2, 2), kwargs={}, options={}}
  222. .. method:: subtask.delay(*args, \*\*kwargs)
  223. Shortcut to :meth:`apply_async`.
  224. .. method:: subtask.apply_async(args=(), kwargs={}, ...)
  225. Apply this task asynchronously.
  226. :keyword args: Partial args to be prepended to the existing args.
  227. :keyword kwargs: Partial kwargs to be merged with the existing kwargs.
  228. :keyword options: Partial options to be merged with the existing
  229. options.
  230. See :meth:`~@Task.apply_async`.
  231. .. method:: subtask.apply(args=(), kwargs={}, ...)
  232. Same as :meth:`apply_async` but executed the task inline instead
  233. of sending a task message.
  234. .. method:: subtask.clone(args=(), kwargs={}, ...)
  235. Returns a copy of this subtask.
  236. :keyword args: Partial args to be prepended to the existing args.
  237. :keyword kwargs: Partial kwargs to be merged with the existing kwargs.
  238. :keyword options: Partial options to be merged with the existing
  239. options.
  240. .. method:: subtask.replace(args=None, kwargs=None, options=None)
  241. Replace the args, kwargs or options set for this subtask.
  242. These are only replaced if the selected is not :const:`None`.
  243. .. method:: subtask.link(other_subtask)
  244. Add a callback task to be applied if this task
  245. executes successfully.
  246. :returns: ``other_subtask`` (to work with :func:`~functools.reduce`).
  247. .. method:: subtask.link_error(other_subtask)
  248. Add a callback task to be applied if an error occurs
  249. while executing this task.
  250. :returns: ``other_subtask`` (to work with :func:`~functools.reduce`)
  251. .. method:: subtask.set(...)
  252. Set arbitrary options (same as ``.options.update(...)``).
  253. This is a chaining method call (i.e. it returns itself).
  254. .. method:: subtask.flatten_links()
  255. Gives a recursive list of dependencies (unchain if you will,
  256. but with links intact).
  257. Proxies
  258. -------
  259. .. data:: current_app
  260. The currently set app for this thread.
  261. .. data:: current_task
  262. The task currently being executed
  263. (only set in the worker, or when eager/apply is used).