calling.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. .. _guide-calling:
  2. ===============
  3. Calling Tasks
  4. ===============
  5. .. contents::
  6. :local:
  7. :depth: 1
  8. .. _calling-basics:
  9. Basics
  10. ======
  11. This document describes Celery's uniform "Calling API"
  12. used by task instances and the :ref:`canvas <guide-canvas>`.
  13. The API defines a standard set of execution options, as well as three methods:
  14. - ``apply_async(args[, kwargs[, ...]])``
  15. Sends a task message.
  16. - ``delay(*args, **kwargs)``
  17. Shortcut to send a task message, but does not support execution
  18. options.
  19. - ``apply()``
  20. Does not send a message but executes the task inline instead.
  21. .. _calling-cheat:
  22. .. topic:: Quick Cheat Sheet
  23. - ``T.delay(arg, kwarg=value)``
  24. always a shortcut to ``.apply_async``.
  25. - ``T.apply_async((arg, ), {"kwarg": value})``
  26. - ``T.apply_async(countdown=10)``
  27. executes 10 seconds from now.
  28. - ``T.apply_async(eta=now + timedelta(seconds=10))``
  29. executes 10 seconds from now, specifed using ``eta``
  30. - ``T.apply_async(countdown=60, expires=120)``
  31. executes in one minute from now, but expires after 2 minutes.
  32. - ``T.apply_async(expires=now + timedelta(days=2))``
  33. expires in 2 days, set using :class:`~datetime.datetime`.
  34. Example
  35. -------
  36. The :meth:`~@Task.delay` method is convenient as it looks like calling a regular
  37. function:
  38. .. code-block:: python
  39. task.delay(arg1, arg2, kwarg1="x", kwarg2="y")
  40. Using :meth:`~@Task.apply_async` instead we have to write::
  41. .. code-block:: python
  42. task.apply_async(args=[arg1, arg2], kwargs={"kwarg1": "x", "kwarg2": "y"})
  43. .. sidebar:: Tip
  44. If the task is not registered in the current process
  45. you can use :meth:`~@send_task` to call the task by name instead.
  46. So `delay` is clearly convenient, but it doesn't give you as much control as using
  47. `apply_async`. With `apply_async` you can override the execution options
  48. available as attributes on the :class:`~@Task` class (see :ref:`task-options`).
  49. In addition you can set countdown/eta, task expiry, provide a custom broker
  50. connection and more.
  51. The rest of this document will go into the task execution
  52. options in detail. All examples use a task
  53. called `add`, returning the sum of two arguments:
  54. .. code-block:: python
  55. @celery.task()
  56. def add(x, y):
  57. return x + y
  58. .. topic:: There's another way...
  59. You will learn more about this later while reading about the :ref:`Canvas
  60. <guide-canvas>`, but :class:`~celery.subtask`'s are objects used to pass around
  61. the signature of a task invocation, (for example to send it over the
  62. network), and they also support the Calling API:
  63. .. code-block:: python
  64. task.s(arg1, arg2, kwarg1="x", kwargs2="y").apply_async()
  65. .. _calling-links:
  66. Linking (callbacks/errbacks)
  67. ============================
  68. Celery supports linking tasks together so that one task follows another.
  69. The callback task will be applied with the result of the parent task
  70. as a partial argument:
  71. .. code-block:: python
  72. add.apply_async((2, 2), link=add.s(16))
  73. .. sidebar:: What is ``s``?
  74. The ``add.s`` call used here is called a subtask, we talk
  75. more about subtasks in the :ref:`canvas guide <guide-canvas>`,
  76. where you can also learn about :class:`~celery.chain`, which
  77. is a simpler way to chain tasks together.
  78. In practice the ``link`` execution option is considered an internal
  79. primitive, and you will probably not use it directly, but
  80. rather use chains instead.
  81. Here the result of the first task (4) will be sent to a new
  82. task that adds 16 to the previous result, forming the expression
  83. :math:`(2 + 2) + 16 = 20`
  84. You can also cause a callback to be applied if task raises an exception
  85. (*errback*), but this behaves differently from a regular callback
  86. in that it will be passed the id of the parent task, not the result.
  87. This is because it may not always be possible to serialize
  88. the exception raised, and so this way the error callback requires
  89. a result backend to be enabled, and the task must retrieve the result
  90. of the task instead.
  91. This is an example error callback:
  92. .. code-block:: python
  93. @celery.task()
  94. def error_handler(uuid):
  95. result = AsyncResult(uuid)
  96. exc = result.get(propagate=False)
  97. print("Task %r raised exception: %r\n%r" % (
  98. exc, result.traceback))
  99. it can be added to the task using the ``link_error`` execution
  100. option:
  101. .. code-block:: python
  102. add.apply_async((2, 2), link_error=error_handler.s())
  103. In addition, both the ``link`` and ``link_error`` options can be expressed
  104. as a list::
  105. add.apply_async((2, 2), link=[add.s(16), other_task.s()])
  106. The callbacks/errbacks will then be called in order, and all
  107. callbacks will be called with the return value of the parent task
  108. as a partial argument.
  109. .. _calling-eta:
  110. ETA and countdown
  111. =================
  112. The ETA (estimated time of arrival) lets you set a specific date and time that
  113. is the earliest time at which your task will be executed. `countdown` is
  114. a shortcut to set eta by seconds into the future.
  115. .. code-block:: python
  116. >>> result = add.apply_async((2, 2), countdown=3)
  117. >>> result.get() # this takes at least 3 seconds to return
  118. 20
  119. The task is guaranteed to be executed at some time *after* the
  120. specified date and time, but not necessarily at that exact time.
  121. Possible reasons for broken deadlines may include many items waiting
  122. in the queue, or heavy network latency. To make sure your tasks
  123. are executed in a timely manner you should monitor the queue for congestion. Use
  124. Munin, or similar tools, to receive alerts, so appropriate action can be
  125. taken to ease the workload. See :ref:`monitoring-munin`.
  126. While `countdown` is an integer, `eta` must be a :class:`~datetime.datetime`
  127. object, specifying an exact date and time (including millisecond precision,
  128. and timezone information):
  129. .. code-block:: python
  130. >>> from datetime import datetime, timedelta
  131. >>> tomorrow = datetime.utcnow() + timedelta(days=1)
  132. >>> add.apply_async((2, 2), eta=tomorrow)
  133. .. _calling-expiration:
  134. Expiration
  135. ==========
  136. The `expires` argument defines an optional expiry time,
  137. either as seconds after task publish, or a specific date and time using
  138. :class:`~datetime.datetime`:
  139. .. code-block:: python
  140. >>> # Task expires after one minute from now.
  141. >>> add.apply_async((10, 10), expires=60)
  142. >>> # Also supports datetime
  143. >>> from datetime import datetime, timedelta
  144. >>> add.apply_async((10, 10), kwargs,
  145. ... expires=datetime.now() + timedelta(days=1)
  146. When a worker receives an expired task it will mark
  147. the task as :state:`REVOKED` (:exc:`~@TaskRevokedError`).
  148. .. _calling-retry:
  149. Message Sending Retry
  150. =====================
  151. Celery will automatically retry sending messages in the event of connection
  152. failure, and retry behavior can be configured -- like how often to retry, or a maximum
  153. number of retries -- or disabled all together.
  154. To disable retry you can set the ``retry`` execution option to :const:`False`:
  155. .. code-block:: python
  156. add.apply_async((2, 2), retry=False)
  157. .. topic:: Related Settings
  158. .. hlist::
  159. :columns: 2
  160. - :setting:`CELERY_TASK_PUBLISH_RETRY`
  161. - :setting:`CELERY_TASK_PUBLISH_RETRY_POLICY`
  162. Retry Policy
  163. ------------
  164. A retry policy is a mapping that controls how retries behave,
  165. and can contain the following keys:
  166. - `max_retries`
  167. Maximum number of retries before giving up, in this case the
  168. exception that caused the retry to fail will be raised.
  169. A value of 0 or :const:`None` means it will retry forever.
  170. The default is to retry 3 times.
  171. - `interval_start`
  172. Defines the number of seconds (float or integer) to wait between
  173. retries. Default is 0, which means the first retry will be
  174. instantaneous.
  175. - `interval_step`
  176. On each consecutive retry this number will be added to the retry
  177. delay (float or integer). Default is 0.2.
  178. - `interval_max`
  179. Maximum number of seconds (float or integer) to wait between
  180. retries. Default is 0.2.
  181. For example, the default policy correlates to:
  182. .. code-block:: python
  183. add.apply_async((2, 2), retry=True, retry_policy={
  184. "max_retries": 3,
  185. "interval_start": 0,
  186. "interval_step": 0.2,
  187. "interval_max": 0.2,
  188. })
  189. the maximum time spent retrying will be 0.4 seconds. It is set relatively
  190. short by default because a connection failure could lead to a retry pile effect
  191. if the broker connection is down: e.g. many web server processes waiting
  192. to retry blocking other incoming requests.
  193. .. _calling-serializers:
  194. Serializers
  195. ===========
  196. .. sidebar:: Security
  197. The pickle module allows for execution of arbitrary functions,
  198. please see the :ref:`security guide <guide-security>`.
  199. Celery also comes with a special serializer that uses
  200. cryptography to sign your messages.
  201. Data transferred between clients and workers needs to be serialized,
  202. so every message in Celery has a ``content_type`` header that
  203. describes the serialization method used to encode it.
  204. The default serializer is :mod:`pickle`, but you can
  205. change this using the :setting:`CELERY_TASK_SERIALIZER` setting,
  206. or for each individual task, or even per message.
  207. There's built-in support for :mod:`pickle`, `JSON`, `YAML`
  208. and `msgpack`, and you can also add your own custom serializers by registering
  209. them into the Kombu serializer registry (see `Kombu: Serialization of Data`_).
  210. .. _`Kombu: Serialization of Data`:
  211. http://packages.python.org/kombu/introduction.html#serialization-of-data
  212. Each option has its advantages and disadvantages.
  213. json -- JSON is supported in many programming languages, is now
  214. a standard part of Python (since 2.6), and is fairly fast to decode
  215. using the modern Python libraries such as :mod:`cjson` or :mod:`simplejson`.
  216. The primary disadvantage to JSON is that it limits you to the following
  217. data types: strings, Unicode, floats, boolean, dictionaries, and lists.
  218. Decimals and dates are notably missing.
  219. Also, binary data will be transferred using Base64 encoding, which will
  220. cause the transferred data to be around 34% larger than an encoding which
  221. supports native binary types.
  222. However, if your data fits inside the above constraints and you need
  223. cross-language support, the default setting of JSON is probably your
  224. best choice.
  225. See http://json.org for more information.
  226. pickle -- If you have no desire to support any language other than
  227. Python, then using the pickle encoding will gain you the support of
  228. all built-in Python data types (except class instances), smaller
  229. messages when sending binary files, and a slight speedup over JSON
  230. processing.
  231. See http://docs.python.org/library/pickle.html for more information.
  232. yaml -- YAML has many of the same characteristics as json,
  233. except that it natively supports more data types (including dates,
  234. recursive references, etc.)
  235. However, the Python libraries for YAML are a good bit slower than the
  236. libraries for JSON.
  237. If you need a more expressive set of data types and need to maintain
  238. cross-language compatibility, then YAML may be a better fit than the above.
  239. See http://yaml.org/ for more information.
  240. msgpack -- msgpack is a binary serialization format that is closer to JSON
  241. in features. It is very young however, and support should be considered
  242. experimental at this point.
  243. See http://msgpack.org/ for more information.
  244. The encoding used is available as a message header, so the worker knows how to
  245. deserialize any task. If you use a custom serializer, this serializer must
  246. be available for the worker.
  247. The following order is used to decide which serializer
  248. to use when sending a task:
  249. 1. The `serializer` execution option.
  250. 2. The :attr:`@-Task.serializer` attribute
  251. 3. The :setting:`CELERY_TASK_SERIALIZER` setting.
  252. Example setting a custom serializer for a single task invocation:
  253. .. code-block:: python
  254. >>> add.apply_async((10, 10), serializer="json")
  255. .. _calling-compression:
  256. Compression
  257. ===========
  258. Celery can compress the messages using either *gzip*, or *bzip2*.
  259. You can also create your own compression schemes and register
  260. them in the :func:`kombu compression registry <kombu.compression.register>`.
  261. The following order is used to decide which compression scheme
  262. to use when sending a task:
  263. 1. The `compression` execution option.
  264. 2. The :attr:`@-Task.compression` attribute.
  265. 3. The :setting:`CELERY_MESSAGE_COMPRESSION` attribute.
  266. Example specifying the compression used when calling a task::
  267. >>> add.apply_async((2, 2), compression="zlib")
  268. .. _calling-connections:
  269. Connections
  270. ===========
  271. .. sidebar:: Automatic Pool Support
  272. Since version 2.3 there is support for automatic connection pools,
  273. so you don't have to manually handle connections and publishers
  274. to reuse connections.
  275. The connection pool is enabled by default since version 2.5.
  276. See the :setting:`BROKER_POOL_LIMIT` setting for more information.
  277. You can handle the connection manually by creating a
  278. publisher:
  279. .. code-block:: python
  280. results = []
  281. with add.app.pool.acquire(block=True) as connection:
  282. with add.get_publisher(connection) as publisher:
  283. try:
  284. for args in numbers:
  285. res = add.apply_async((2, 2), publisher=publisher)
  286. results.append(res)
  287. print([res.get() for res in results])
  288. Though this particular example is much better expressed as a group:
  289. .. code-block:: python
  290. >>> from celery import group
  291. >>> numbers = [(2, 2), (4, 4), (8, 8), (16, 16)]
  292. >>> res = group(add.subtask(n) for i in numbers).apply_async()
  293. >>> res.get()
  294. [4, 8, 16, 32]
  295. .. _calling-routing:
  296. Routing options
  297. ===============
  298. Celery can route tasks to different queues.
  299. Simple routing (name <-> name) is accomplished using the ``queue`` option::
  300. add.apply_async(queue="priority.high")
  301. You can then assign workers to the ``priority.high`` queue by using
  302. the workers :option:`-Q` argument::
  303. $ celery worker -l info -Q celery,priority.high
  304. .. seealso::
  305. Hard-coding queue names in code is not recommended, the best practice
  306. is to use configuration routers (:setting:`CELERY_ROUTES`).
  307. To find out more about routing, please see :ref:`guide-routing`.
  308. Advanced Options
  309. ----------------
  310. These options are for advanced users who want to take use of
  311. AMQP's full routing capabilities. Interested parties may read the
  312. :ref:`routing guide <guide-routing>`.
  313. - exchange
  314. Name of exchange (or a :class:`kombu.entity.Exchange`) to
  315. send the message to.
  316. - routing_key
  317. Routing key used to determine.
  318. - mandatory
  319. This sets the delivery to be mandatory. An exception will be raised
  320. if there are no running workers able to take on the task.
  321. Not supported by :mod:`amqplib`.
  322. - immediate
  323. Request immediate delivery. Will raise an exception
  324. if the task cannot be routed to a worker immediately.
  325. Not supported by :mod:`amqplib`.
  326. - priority
  327. A number between `0` and `9`, where `0` is the highest priority.
  328. Supported by: redis, beanstalk