PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/docs/reference/celery.rst

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