calling.html 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Calling Tasks &mdash; Celery 2.6.0rc4 documentation</title>
  7. <link rel="stylesheet" href="../_static/celery.css" type="text/css" />
  8. <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  9. <link rel="stylesheet" href="../_static/issuetracker.css" type="text/css" />
  10. <script type="text/javascript">
  11. var DOCUMENTATION_OPTIONS = {
  12. URL_ROOT: '../',
  13. VERSION: '2.6.0rc4',
  14. COLLAPSE_INDEX: false,
  15. FILE_SUFFIX: '.html',
  16. HAS_SOURCE: true
  17. };
  18. </script>
  19. <script type="text/javascript" src="../_static/jquery.js"></script>
  20. <script type="text/javascript" src="../_static/underscore.js"></script>
  21. <script type="text/javascript" src="../_static/doctools.js"></script>
  22. <link rel="top" title="Celery 2.6.0rc4 documentation" href="../index.html" />
  23. <link rel="up" title="User Guide" href="index.html" />
  24. <link rel="next" title="Workers Guide" href="workers.html" />
  25. <link rel="prev" title="Tasks" href="tasks.html" />
  26. </head>
  27. <body>
  28. <div class="related">
  29. <h3>Navigation</h3>
  30. <ul>
  31. <li class="right" style="margin-right: 10px">
  32. <a href="../genindex.html" title="General Index"
  33. accesskey="I">index</a></li>
  34. <li class="right" >
  35. <a href="../py-modindex.html" title="Python Module Index"
  36. >modules</a> |</li>
  37. <li class="right" >
  38. <a href="workers.html" title="Workers Guide"
  39. accesskey="N">next</a> |</li>
  40. <li class="right" >
  41. <a href="tasks.html" title="Tasks"
  42. accesskey="P">previous</a> |</li>
  43. <li><a href="../index.html">Celery 2.6.0rc4 documentation</a> &raquo;</li>
  44. <li><a href="index.html" accesskey="U">User Guide</a> &raquo;</li>
  45. </ul>
  46. </div>
  47. <div class="document">
  48. <div class="documentwrapper">
  49. <div class="bodywrapper">
  50. <div class="body">
  51. <div class="deck">
  52. <p class="developmentversion">
  53. This document is for Celery's development version, which can be
  54. significantly different from previous releases. Get old docs here:
  55. <a href="http://docs.celeryproject.org/en/latest/userguide/calling.html">2.5</a>.
  56. </p>
  57. </div>
  58. <div class="section" id="calling-tasks">
  59. <span id="guide-calling"></span><h1>Calling Tasks<a class="headerlink" href="#calling-tasks" title="Permalink to this headline">¶</a></h1>
  60. <div class="contents local topic" id="contents">
  61. <ul class="simple">
  62. <li><a class="reference internal" href="#basics" id="id1">Basics</a></li>
  63. <li><a class="reference internal" href="#linking-callbacks-errbacks" id="id2">Linking (callbacks/errbacks)</a></li>
  64. <li><a class="reference internal" href="#eta-and-countdown" id="id3">ETA and countdown</a></li>
  65. <li><a class="reference internal" href="#expiration" id="id4">Expiration</a></li>
  66. <li><a class="reference internal" href="#message-sending-retry" id="id5">Message Sending Retry</a></li>
  67. <li><a class="reference internal" href="#serializers" id="id6">Serializers</a></li>
  68. <li><a class="reference internal" href="#compression" id="id7">Compression</a></li>
  69. <li><a class="reference internal" href="#connections" id="id8">Connections</a></li>
  70. <li><a class="reference internal" href="#routing-options" id="id9">Routing options</a></li>
  71. </ul>
  72. </div>
  73. <div class="section" id="basics">
  74. <span id="calling-basics"></span><h2><a class="toc-backref" href="#id1">Basics</a><a class="headerlink" href="#basics" title="Permalink to this headline">¶</a></h2>
  75. <p>This document describes Celery&#8217;s uniform &#8220;Calling API&#8221;
  76. used by task instances and the <a class="reference internal" href="canvas.html#guide-canvas"><em>canvas</em></a>.</p>
  77. <p>The API defines a standard set of execution options, as well as three methods:</p>
  78. <blockquote>
  79. <div><ul>
  80. <li><p class="first"><tt class="docutils literal"><span class="pre">apply_async(args[,</span> <span class="pre">kwargs[,</span> <span class="pre">...]])</span></tt></p>
  81. <blockquote>
  82. <div><p>Sends a task message.</p>
  83. </div></blockquote>
  84. </li>
  85. <li><p class="first"><tt class="docutils literal"><span class="pre">delay(*args,</span> <span class="pre">**kwargs)</span></tt></p>
  86. <blockquote>
  87. <div><p>Shortcut to send a task message, but does not support execution
  88. options.</p>
  89. </div></blockquote>
  90. </li>
  91. <li><p class="first"><tt class="docutils literal"><span class="pre">apply()</span></tt></p>
  92. <blockquote>
  93. <div><p>Does not send a message but executes the task inline instead.</p>
  94. </div></blockquote>
  95. </li>
  96. </ul>
  97. </div></blockquote>
  98. <div class="topic" id="calling-cheat">
  99. <p class="topic-title first">Quick Cheat Sheet</p>
  100. <ul>
  101. <li><dl class="first docutils">
  102. <dt><tt class="docutils literal"><span class="pre">T.delay(arg,</span> <span class="pre">kwarg=value)</span></tt></dt>
  103. <dd><p class="first last">always a shortcut to <tt class="docutils literal"><span class="pre">.apply_async</span></tt>.</p>
  104. </dd>
  105. </dl>
  106. </li>
  107. <li><p class="first"><tt class="docutils literal"><span class="pre">T.apply_async((arg,</span> <span class="pre">),</span> <span class="pre">{&quot;kwarg&quot;:</span> <span class="pre">value})</span></tt></p>
  108. </li>
  109. <li><dl class="first docutils">
  110. <dt><tt class="docutils literal"><span class="pre">T.apply_async(countdown=10)</span></tt></dt>
  111. <dd><p class="first last">executes 10 seconds from now.</p>
  112. </dd>
  113. </dl>
  114. </li>
  115. <li><dl class="first docutils">
  116. <dt><tt class="docutils literal"><span class="pre">T.apply_async(eta=now</span> <span class="pre">+</span> <span class="pre">timedelta(seconds=10))</span></tt></dt>
  117. <dd><p class="first last">executes 10 seconds from now, specifed using <tt class="docutils literal"><span class="pre">eta</span></tt></p>
  118. </dd>
  119. </dl>
  120. </li>
  121. <li><dl class="first docutils">
  122. <dt><tt class="docutils literal"><span class="pre">T.apply_async(countdown=60,</span> <span class="pre">expires=120)</span></tt></dt>
  123. <dd><p class="first last">executes in one minute from now, but expires after 2 minutes.</p>
  124. </dd>
  125. </dl>
  126. </li>
  127. <li><dl class="first docutils">
  128. <dt><tt class="docutils literal"><span class="pre">T.apply_async(expires=now</span> <span class="pre">+</span> <span class="pre">timedelta(days=2))</span></tt></dt>
  129. <dd><p class="first last">expires in 2 days, set using <a class="reference external" href="http://docs.python.org/dev/library/datetime.html#datetime.datetime" title="(in Python v3.3)"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a>.</p>
  130. </dd>
  131. </dl>
  132. </li>
  133. </ul>
  134. </div>
  135. <div class="section" id="example">
  136. <h3>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h3>
  137. <p>The <a class="reference internal" href="../reference/celery.app.task.html#celery.app.task.Task.delay" title="celery.app.task.Task.delay"><tt class="xref py py-meth docutils literal"><span class="pre">delay()</span></tt></a> method is convenient as it looks like calling a regular
  138. function:</p>
  139. <div class="highlight-python"><div class="highlight"><pre><span class="n">task</span><span class="o">.</span><span class="n">delay</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="n">kwarg1</span><span class="o">=</span><span class="s">&quot;x&quot;</span><span class="p">,</span> <span class="n">kwarg2</span><span class="o">=</span><span class="s">&quot;y&quot;</span><span class="p">)</span>
  140. </pre></div>
  141. </div>
  142. <p>Using <a class="reference internal" href="../reference/celery.app.task.html#celery.app.task.Task.apply_async" title="celery.app.task.Task.apply_async"><tt class="xref py py-meth docutils literal"><span class="pre">apply_async()</span></tt></a> instead we have to write:</p>
  143. <div class="highlight-python"><pre>.. code-block:: python</pre>
  144. </div>
  145. <blockquote>
  146. <div>task.apply_async(args=[arg1, arg2], kwargs={&#8220;kwarg1&#8221;: &#8220;x&#8221;, &#8220;kwarg2&#8221;: &#8220;y&#8221;})</div></blockquote>
  147. <div class="sidebar">
  148. <p class="first sidebar-title">Tip</p>
  149. <p class="last">If the task is not registered in the current process
  150. you can use <a class="reference internal" href="../reference/celery.html#celery.Celery.send_task" title="celery.Celery.send_task"><tt class="xref py py-meth docutils literal"><span class="pre">send_task()</span></tt></a> to call the task by name instead.</p>
  151. </div>
  152. <p>So <cite>delay</cite> is clearly convenient, but it doesn&#8217;t give you as much control as using
  153. <cite>apply_async</cite>. With <cite>apply_async</cite> you can override the execution options
  154. available as attributes on the <a class="reference internal" href="../reference/celery.app.task.html#celery.app.task.Task" title="celery.app.task.Task"><tt class="xref py py-class docutils literal"><span class="pre">Task</span></tt></a> class (see <a class="reference internal" href="tasks.html#task-options"><em>Task options</em></a>).
  155. In addition you can set countdown/eta, task expiry, provide a custom broker
  156. connection and more.</p>
  157. <p>The rest of this document will go into the task execution
  158. options in detail. All examples use a task
  159. called <cite>add</cite>, returning the sum of two arguments:</p>
  160. <div class="highlight-python"><div class="highlight"><pre><span class="nd">@celery.task</span><span class="p">()</span>
  161. <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
  162. <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span>
  163. </pre></div>
  164. </div>
  165. <div class="topic">
  166. <p class="topic-title first">There&#8217;s another way...</p>
  167. <p>You will learn more about this later while reading about the <a class="reference internal" href="canvas.html#guide-canvas"><em>Canvas</em></a>, but <a class="reference internal" href="../reference/celery.html#celery.subtask" title="celery.subtask"><tt class="xref py py-class docutils literal"><span class="pre">subtask</span></tt></a>&#8216;s are objects used to pass around
  168. the signature of a task invocation, (for example to send it over the
  169. network), and they also support the Calling API:</p>
  170. <div class="highlight-python"><div class="highlight"><pre><span class="n">task</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="n">kwarg1</span><span class="o">=</span><span class="s">&quot;x&quot;</span><span class="p">,</span> <span class="n">kwargs2</span><span class="o">=</span><span class="s">&quot;y&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">apply_async</span><span class="p">()</span>
  171. </pre></div>
  172. </div>
  173. </div>
  174. </div>
  175. </div>
  176. <div class="section" id="linking-callbacks-errbacks">
  177. <span id="calling-links"></span><h2><a class="toc-backref" href="#id2">Linking (callbacks/errbacks)</a><a class="headerlink" href="#linking-callbacks-errbacks" title="Permalink to this headline">¶</a></h2>
  178. <p>Celery supports linking tasks together so that one task follows another.
  179. The callback task will be applied with the result of the parent task
  180. as a partial argument:</p>
  181. <div class="highlight-python"><div class="highlight"><pre><span class="n">add</span><span class="o">.</span><span class="n">apply_async</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">link</span><span class="o">=</span><span class="n">add</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="mi">16</span><span class="p">))</span>
  182. </pre></div>
  183. </div>
  184. <div class="sidebar">
  185. <p class="first sidebar-title">What is <tt class="docutils literal"><span class="pre">s</span></tt>?</p>
  186. <p>The <tt class="docutils literal"><span class="pre">add.s</span></tt> call used here is called a subtask, we talk
  187. more about subtasks in the <a class="reference internal" href="canvas.html#guide-canvas"><em>canvas guide</em></a>,
  188. where you can also learn about <a class="reference internal" href="../reference/celery.html#celery.chain" title="celery.chain"><tt class="xref py py-class docutils literal"><span class="pre">chain</span></tt></a>, which
  189. is a simpler way to chain tasks together.</p>
  190. <p class="last">In practice the <tt class="docutils literal"><span class="pre">link</span></tt> execution option is considered an internal
  191. primitive, and you will probably not use it directly, but
  192. rather use chains instead.</p>
  193. </div>
  194. <p>Here the result of the first task (4) will be sent to a new
  195. task that adds 16 to the previous result, forming the expression
  196. <img class="math" src="../_images/math/5bec3f9bd85f65932e771e2f003f923f49ed1a94.png" alt="(2 + 2) + 16 = 20"/></p>
  197. <p>You can also cause a callback to be applied if task raises an exception
  198. (<em>errback</em>), but this behaves differently from a regular callback
  199. in that it will be passed the id of the parent task, not the result.
  200. This is because it may not always be possible to serialize
  201. the exception raised, and so this way the error callback requires
  202. a result backend to be enabled, and the task must retrieve the result
  203. of the task instead.</p>
  204. <p>This is an example error callback:</p>
  205. <div class="highlight-python"><div class="highlight"><pre><span class="nd">@celery.task</span><span class="p">()</span>
  206. <span class="k">def</span> <span class="nf">error_handler</span><span class="p">(</span><span class="n">uuid</span><span class="p">):</span>
  207. <span class="n">result</span> <span class="o">=</span> <span class="n">AsyncResult</span><span class="p">(</span><span class="n">uuid</span><span class="p">)</span>
  208. <span class="n">exc</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">propagate</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
  209. <span class="k">print</span><span class="p">(</span><span class="s">&quot;Task </span><span class="si">%r</span><span class="s"> raised exception: </span><span class="si">%r</span><span class="se">\n</span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span>
  210. <span class="n">exc</span><span class="p">,</span> <span class="n">result</span><span class="o">.</span><span class="n">traceback</span><span class="p">))</span>
  211. </pre></div>
  212. </div>
  213. <p>it can be added to the task using the <tt class="docutils literal"><span class="pre">link_error</span></tt> execution
  214. option:</p>
  215. <div class="highlight-python"><div class="highlight"><pre><span class="n">add</span><span class="o">.</span><span class="n">apply_async</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">link_error</span><span class="o">=</span><span class="n">error_handler</span><span class="o">.</span><span class="n">s</span><span class="p">())</span>
  216. </pre></div>
  217. </div>
  218. <p>In addition, both the <tt class="docutils literal"><span class="pre">link</span></tt> and <tt class="docutils literal"><span class="pre">link_error</span></tt> options can be expressed
  219. as a list:</p>
  220. <div class="highlight-python"><div class="highlight"><pre><span class="n">add</span><span class="o">.</span><span class="n">apply_async</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">link</span><span class="o">=</span><span class="p">[</span><span class="n">add</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="mi">16</span><span class="p">),</span> <span class="n">other_task</span><span class="o">.</span><span class="n">s</span><span class="p">()])</span>
  221. </pre></div>
  222. </div>
  223. <p>The callbacks/errbacks will then be called in order, and all
  224. callbacks will be called with the return value of the parent task
  225. as a partial argument.</p>
  226. </div>
  227. <div class="section" id="eta-and-countdown">
  228. <span id="calling-eta"></span><h2><a class="toc-backref" href="#id3">ETA and countdown</a><a class="headerlink" href="#eta-and-countdown" title="Permalink to this headline">¶</a></h2>
  229. <p>The ETA (estimated time of arrival) lets you set a specific date and time that
  230. is the earliest time at which your task will be executed. <cite>countdown</cite> is
  231. a shortcut to set eta by seconds into the future.</p>
  232. <div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">result</span> <span class="o">=</span> <span class="n">add</span><span class="o">.</span><span class="n">apply_async</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">countdown</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
  233. <span class="gp">&gt;&gt;&gt; </span><span class="n">result</span><span class="o">.</span><span class="n">get</span><span class="p">()</span> <span class="c"># this takes at least 3 seconds to return</span>
  234. <span class="go">20</span>
  235. </pre></div>
  236. </div>
  237. <p>The task is guaranteed to be executed at some time <em>after</em> the
  238. specified date and time, but not necessarily at that exact time.
  239. Possible reasons for broken deadlines may include many items waiting
  240. in the queue, or heavy network latency. To make sure your tasks
  241. are executed in a timely manner you should monitor the queue for congestion. Use
  242. Munin, or similar tools, to receive alerts, so appropriate action can be
  243. taken to ease the workload. See <a class="reference internal" href="monitoring.html#monitoring-munin"><em>Munin</em></a>.</p>
  244. <p>While <cite>countdown</cite> is an integer, <cite>eta</cite> must be a <a class="reference external" href="http://docs.python.org/dev/library/datetime.html#datetime.datetime" title="(in Python v3.3)"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a>
  245. object, specifying an exact date and time (including millisecond precision,
  246. and timezone information):</p>
  247. <div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">timedelta</span>
  248. <span class="gp">&gt;&gt;&gt; </span><span class="n">tomorrow</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">utcnow</span><span class="p">()</span> <span class="o">+</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
  249. <span class="gp">&gt;&gt;&gt; </span><span class="n">add</span><span class="o">.</span><span class="n">apply_async</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">eta</span><span class="o">=</span><span class="n">tomorrow</span><span class="p">)</span>
  250. </pre></div>
  251. </div>
  252. </div>
  253. <div class="section" id="expiration">
  254. <span id="calling-expiration"></span><h2><a class="toc-backref" href="#id4">Expiration</a><a class="headerlink" href="#expiration" title="Permalink to this headline">¶</a></h2>
  255. <p>The <cite>expires</cite> argument defines an optional expiry time,
  256. either as seconds after task publish, or a specific date and time using
  257. <a class="reference external" href="http://docs.python.org/dev/library/datetime.html#datetime.datetime" title="(in Python v3.3)"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a>:</p>
  258. <div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># Task expires after one minute from now.</span>
  259. <span class="gp">&gt;&gt;&gt; </span><span class="n">add</span><span class="o">.</span><span class="n">apply_async</span><span class="p">((</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span> <span class="n">expires</span><span class="o">=</span><span class="mi">60</span><span class="p">)</span>
  260. <span class="gp">&gt;&gt;&gt; </span><span class="c"># Also supports datetime</span>
  261. <span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">timedelta</span>
  262. <span class="gp">&gt;&gt;&gt; </span><span class="n">add</span><span class="o">.</span><span class="n">apply_async</span><span class="p">((</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span> <span class="n">kwargs</span><span class="p">,</span>
  263. <span class="gp">... </span> <span class="n">expires</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span> <span class="o">+</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
  264. </pre></div>
  265. </div>
  266. <p>When a worker receives an expired task it will mark
  267. the task as <a class="reference internal" href="tasks.html#std:state-REVOKED"><tt class="xref std std-state docutils literal"><span class="pre">REVOKED</span></tt></a> (<a class="reference internal" href="../reference/celery.exceptions.html#celery.exceptions.TaskRevokedError" title="celery.exceptions.TaskRevokedError"><tt class="xref py py-exc docutils literal"><span class="pre">TaskRevokedError</span></tt></a>).</p>
  268. </div>
  269. <div class="section" id="message-sending-retry">
  270. <span id="calling-retry"></span><h2><a class="toc-backref" href="#id5">Message Sending Retry</a><a class="headerlink" href="#message-sending-retry" title="Permalink to this headline">¶</a></h2>
  271. <p>Celery will automatically retry sending messages in the event of connection
  272. failure, and retry behavior can be configured &#8211; like how often to retry, or a maximum
  273. number of retries &#8211; or disabled all together.</p>
  274. <p>To disable retry you can set the <tt class="docutils literal"><span class="pre">retry</span></tt> execution option to <tt class="xref py py-const docutils literal"><span class="pre">False</span></tt>:</p>
  275. <div class="highlight-python"><div class="highlight"><pre><span class="n">add</span><span class="o">.</span><span class="n">apply_async</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">retry</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
  276. </pre></div>
  277. </div>
  278. <div class="topic">
  279. <p class="topic-title first">Related Settings</p>
  280. <table class="hlist"><tr><td><ul class="simple">
  281. <li><a class="reference internal" href="../configuration.html#std:setting-CELERY_TASK_PUBLISH_RETRY"><tt class="xref std std-setting docutils literal"><span class="pre">CELERY_TASK_PUBLISH_RETRY</span></tt></a></li>
  282. </ul>
  283. </td><td><ul class="simple">
  284. <li><a class="reference internal" href="../configuration.html#std:setting-CELERY_TASK_PUBLISH_RETRY_POLICY"><tt class="xref std std-setting docutils literal"><span class="pre">CELERY_TASK_PUBLISH_RETRY_POLICY</span></tt></a></li>
  285. </ul>
  286. </td></tr></table>
  287. </div>
  288. <div class="section" id="retry-policy">
  289. <h3>Retry Policy<a class="headerlink" href="#retry-policy" title="Permalink to this headline">¶</a></h3>
  290. <p>A retry policy is a mapping that controls how retries behave,
  291. and can contain the following keys:</p>
  292. <ul>
  293. <li><p class="first"><cite>max_retries</cite></p>
  294. <blockquote>
  295. <div><p>Maximum number of retries before giving up, in this case the
  296. exception that caused the retry to fail will be raised.</p>
  297. <p>A value of 0 or <tt class="xref py py-const docutils literal"><span class="pre">None</span></tt> means it will retry forever.</p>
  298. <p>The default is to retry 3 times.</p>
  299. </div></blockquote>
  300. </li>
  301. <li><p class="first"><cite>interval_start</cite></p>
  302. <blockquote>
  303. <div><p>Defines the number of seconds (float or integer) to wait between
  304. retries. Default is 0, which means the first retry will be
  305. instantaneous.</p>
  306. </div></blockquote>
  307. </li>
  308. <li><p class="first"><cite>interval_step</cite></p>
  309. <blockquote>
  310. <div><p>On each consecutive retry this number will be added to the retry
  311. delay (float or integer). Default is 0.2.</p>
  312. </div></blockquote>
  313. </li>
  314. <li><p class="first"><cite>interval_max</cite></p>
  315. <blockquote>
  316. <div><p>Maximum number of seconds (float or integer) to wait between
  317. retries. Default is 0.2.</p>
  318. </div></blockquote>
  319. </li>
  320. </ul>
  321. <p>For example, the default policy correlates to:</p>
  322. <div class="highlight-python"><div class="highlight"><pre><span class="n">add</span><span class="o">.</span><span class="n">apply_async</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">retry</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">retry_policy</span><span class="o">=</span><span class="p">{</span>
  323. <span class="s">&quot;max_retries&quot;</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
  324. <span class="s">&quot;interval_start&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
  325. <span class="s">&quot;interval_step&quot;</span><span class="p">:</span> <span class="mf">0.2</span><span class="p">,</span>
  326. <span class="s">&quot;interval_max&quot;</span><span class="p">:</span> <span class="mf">0.2</span><span class="p">,</span>
  327. <span class="p">})</span>
  328. </pre></div>
  329. </div>
  330. <p>the maximum time spent retrying will be 0.4 seconds. It is set relatively
  331. short by default because a connection failure could lead to a retry pile effect
  332. if the broker connection is down: e.g. many web server processes waiting
  333. to retry blocking other incoming requests.</p>
  334. </div>
  335. </div>
  336. <div class="section" id="serializers">
  337. <span id="calling-serializers"></span><h2><a class="toc-backref" href="#id6">Serializers</a><a class="headerlink" href="#serializers" title="Permalink to this headline">¶</a></h2>
  338. <div class="sidebar">
  339. <p class="first sidebar-title">Security</p>
  340. <p>The pickle module allows for execution of arbitrary functions,
  341. please see the <a class="reference internal" href="security.html#guide-security"><em>security guide</em></a>.</p>
  342. <p class="last">Celery also comes with a special serializer that uses
  343. cryptography to sign your messages.</p>
  344. </div>
  345. <p>Data transferred between clients and workers needs to be serialized,
  346. so every message in Celery has a <tt class="docutils literal"><span class="pre">content_type</span></tt> header that
  347. describes the serialization method used to encode it.</p>
  348. <p>The default serializer is <a class="reference external" href="http://docs.python.org/dev/library/pickle.html#pickle" title="(in Python v3.3)"><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a>, but you can
  349. change this using the <a class="reference internal" href="../configuration.html#std:setting-CELERY_TASK_SERIALIZER"><tt class="xref std std-setting docutils literal"><span class="pre">CELERY_TASK_SERIALIZER</span></tt></a> setting,
  350. or for each individual task, or even per message.</p>
  351. <p>There&#8217;s built-in support for <a class="reference external" href="http://docs.python.org/dev/library/pickle.html#pickle" title="(in Python v3.3)"><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a>, <cite>JSON</cite>, <cite>YAML</cite>
  352. and <cite>msgpack</cite>, and you can also add your own custom serializers by registering
  353. them into the Kombu serializer registry (see <a class="reference external" href="http://packages.python.org/kombu/introduction.html#serialization-of-data">Kombu: Serialization of Data</a>).</p>
  354. <p>Each option has its advantages and disadvantages.</p>
  355. <dl class="docutils">
  356. <dt>json &#8211; JSON is supported in many programming languages, is now</dt>
  357. <dd><p class="first">a standard part of Python (since 2.6), and is fairly fast to decode
  358. using the modern Python libraries such as <tt class="xref py py-mod docutils literal"><span class="pre">cjson</span></tt> or <tt class="xref py py-mod docutils literal"><span class="pre">simplejson</span></tt>.</p>
  359. <p>The primary disadvantage to JSON is that it limits you to the following
  360. data types: strings, Unicode, floats, boolean, dictionaries, and lists.
  361. Decimals and dates are notably missing.</p>
  362. <p>Also, binary data will be transferred using Base64 encoding, which will
  363. cause the transferred data to be around 34% larger than an encoding which
  364. supports native binary types.</p>
  365. <p>However, if your data fits inside the above constraints and you need
  366. cross-language support, the default setting of JSON is probably your
  367. best choice.</p>
  368. <p class="last">See <a class="reference external" href="http://json.org">http://json.org</a> for more information.</p>
  369. </dd>
  370. <dt>pickle &#8211; If you have no desire to support any language other than</dt>
  371. <dd><p class="first">Python, then using the pickle encoding will gain you the support of
  372. all built-in Python data types (except class instances), smaller
  373. messages when sending binary files, and a slight speedup over JSON
  374. processing.</p>
  375. <p class="last">See <a class="reference external" href="http://docs.python.org/library/pickle.html">http://docs.python.org/library/pickle.html</a> for more information.</p>
  376. </dd>
  377. <dt>yaml &#8211; YAML has many of the same characteristics as json,</dt>
  378. <dd><p class="first">except that it natively supports more data types (including dates,
  379. recursive references, etc.)</p>
  380. <p>However, the Python libraries for YAML are a good bit slower than the
  381. libraries for JSON.</p>
  382. <p>If you need a more expressive set of data types and need to maintain
  383. cross-language compatibility, then YAML may be a better fit than the above.</p>
  384. <p class="last">See <a class="reference external" href="http://yaml.org/">http://yaml.org/</a> for more information.</p>
  385. </dd>
  386. <dt>msgpack &#8211; msgpack is a binary serialization format that is closer to JSON</dt>
  387. <dd><p class="first">in features. It is very young however, and support should be considered
  388. experimental at this point.</p>
  389. <p class="last">See <a class="reference external" href="http://msgpack.org/">http://msgpack.org/</a> for more information.</p>
  390. </dd>
  391. </dl>
  392. <p>The encoding used is available as a message header, so the worker knows how to
  393. deserialize any task. If you use a custom serializer, this serializer must
  394. be available for the worker.</p>
  395. <p>The following order is used to decide which serializer
  396. to use when sending a task:</p>
  397. <blockquote>
  398. <div><ol class="arabic simple">
  399. <li>The <cite>serializer</cite> execution option.</li>
  400. <li>The <a class="reference internal" href="../reference/celery.app.task.html#celery.app.task.Task.serializer" title="celery.app.task.Task.serializer"><tt class="xref py py-attr docutils literal"><span class="pre">Task.serializer</span></tt></a> attribute</li>
  401. <li>The <a class="reference internal" href="../configuration.html#std:setting-CELERY_TASK_SERIALIZER"><tt class="xref std std-setting docutils literal"><span class="pre">CELERY_TASK_SERIALIZER</span></tt></a> setting.</li>
  402. </ol>
  403. </div></blockquote>
  404. <p>Example setting a custom serializer for a single task invocation:</p>
  405. <div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">add</span><span class="o">.</span><span class="n">apply_async</span><span class="p">((</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span> <span class="n">serializer</span><span class="o">=</span><span class="s">&quot;json&quot;</span><span class="p">)</span>
  406. </pre></div>
  407. </div>
  408. </div>
  409. <div class="section" id="compression">
  410. <span id="calling-compression"></span><h2><a class="toc-backref" href="#id7">Compression</a><a class="headerlink" href="#compression" title="Permalink to this headline">¶</a></h2>
  411. <p>Celery can compress the messages using either <em>gzip</em>, or <em>bzip2</em>.
  412. You can also create your own compression schemes and register
  413. them in the <a class="reference external" href="http://kombu.readthedocs.org/en/latest/reference/kombu.compression.html#kombu.compression.register" title="(in Kombu v2.2)"><tt class="xref py py-func docutils literal"><span class="pre">kombu</span> <span class="pre">compression</span> <span class="pre">registry</span></tt></a>.</p>
  414. <p>The following order is used to decide which compression scheme
  415. to use when sending a task:</p>
  416. <blockquote>
  417. <div><ol class="arabic simple">
  418. <li>The <cite>compression</cite> execution option.</li>
  419. <li>The <tt class="xref py py-attr docutils literal"><span class="pre">Task.compression</span></tt> attribute.</li>
  420. <li>The <a class="reference internal" href="../configuration.html#std:setting-CELERY_MESSAGE_COMPRESSION"><tt class="xref std std-setting docutils literal"><span class="pre">CELERY_MESSAGE_COMPRESSION</span></tt></a> attribute.</li>
  421. </ol>
  422. </div></blockquote>
  423. <p>Example specifying the compression used when calling a task:</p>
  424. <div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">add</span><span class="o">.</span><span class="n">apply_async</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">compression</span><span class="o">=</span><span class="s">&quot;zlib&quot;</span><span class="p">)</span>
  425. </pre></div>
  426. </div>
  427. </div>
  428. <div class="section" id="connections">
  429. <span id="calling-connections"></span><h2><a class="toc-backref" href="#id8">Connections</a><a class="headerlink" href="#connections" title="Permalink to this headline">¶</a></h2>
  430. <div class="sidebar">
  431. <p class="first sidebar-title">Automatic Pool Support</p>
  432. <p>Since version 2.3 there is support for automatic connection pools,
  433. so you don&#8217;t have to manually handle connections and publishers
  434. to reuse connections.</p>
  435. <p>The connection pool is enabled by default since version 2.5.</p>
  436. <p class="last">See the <a class="reference internal" href="../configuration.html#std:setting-BROKER_POOL_LIMIT"><tt class="xref std std-setting docutils literal"><span class="pre">BROKER_POOL_LIMIT</span></tt></a> setting for more information.</p>
  437. </div>
  438. <p>You can handle the connection manually by creating a
  439. publisher:</p>
  440. <div class="highlight-python"><div class="highlight"><pre><span class="n">results</span> <span class="o">=</span> <span class="p">[]</span>
  441. <span class="k">with</span> <span class="n">add</span><span class="o">.</span><span class="n">app</span><span class="o">.</span><span class="n">pool</span><span class="o">.</span><span class="n">acquire</span><span class="p">(</span><span class="n">block</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> <span class="k">as</span> <span class="n">connection</span><span class="p">:</span>
  442. <span class="k">with</span> <span class="n">add</span><span class="o">.</span><span class="n">get_publisher</span><span class="p">(</span><span class="n">connection</span><span class="p">)</span> <span class="k">as</span> <span class="n">publisher</span><span class="p">:</span>
  443. <span class="k">try</span><span class="p">:</span>
  444. <span class="k">for</span> <span class="n">args</span> <span class="ow">in</span> <span class="n">numbers</span><span class="p">:</span>
  445. <span class="n">res</span> <span class="o">=</span> <span class="n">add</span><span class="o">.</span><span class="n">apply_async</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">publisher</span><span class="o">=</span><span class="n">publisher</span><span class="p">)</span>
  446. <span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
  447. <span class="k">print</span><span class="p">([</span><span class="n">res</span><span class="o">.</span><span class="n">get</span><span class="p">()</span> <span class="k">for</span> <span class="n">res</span> <span class="ow">in</span> <span class="n">results</span><span class="p">])</span>
  448. </pre></div>
  449. </div>
  450. <p>Though this particular example is much better expressed as a group:</p>
  451. <div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">celery</span> <span class="kn">import</span> <span class="n">group</span>
  452. <span class="gp">&gt;&gt;&gt; </span><span class="n">numbers</span> <span class="o">=</span> <span class="p">[(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="p">(</span><span class="mi">8</span><span class="p">,</span> <span class="mi">8</span><span class="p">),</span> <span class="p">(</span><span class="mi">16</span><span class="p">,</span> <span class="mi">16</span><span class="p">)]</span>
  453. <span class="gp">&gt;&gt;&gt; </span><span class="n">res</span> <span class="o">=</span> <span class="n">group</span><span class="p">(</span><span class="n">add</span><span class="o">.</span><span class="n">subtask</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">numbers</span><span class="p">)</span><span class="o">.</span><span class="n">apply_async</span><span class="p">()</span>
  454. <span class="gp">&gt;&gt;&gt; </span><span class="n">res</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
  455. <span class="go">[4, 8, 16, 32]</span>
  456. </pre></div>
  457. </div>
  458. </div>
  459. <div class="section" id="routing-options">
  460. <span id="calling-routing"></span><h2><a class="toc-backref" href="#id9">Routing options</a><a class="headerlink" href="#routing-options" title="Permalink to this headline">¶</a></h2>
  461. <p>Celery can route tasks to different queues.</p>
  462. <p>Simple routing (name &lt;-&gt; name) is accomplished using the <tt class="docutils literal"><span class="pre">queue</span></tt> option:</p>
  463. <div class="highlight-python"><div class="highlight"><pre><span class="n">add</span><span class="o">.</span><span class="n">apply_async</span><span class="p">(</span><span class="n">queue</span><span class="o">=</span><span class="s">&quot;priority.high&quot;</span><span class="p">)</span>
  464. </pre></div>
  465. </div>
  466. <p>You can then assign workers to the <tt class="docutils literal"><span class="pre">priority.high</span></tt> queue by using
  467. the workers <em class="xref std std-option">-Q</em> argument:</p>
  468. <div class="highlight-python"><pre>$ celery worker -l info -Q celery,priority.high</pre>
  469. </div>
  470. <div class="admonition-see-also admonition seealso">
  471. <p class="first admonition-title">See also</p>
  472. <p>Hard-coding queue names in code is not recommended, the best practice
  473. is to use configuration routers (<a class="reference internal" href="../configuration.html#std:setting-CELERY_ROUTES"><tt class="xref std std-setting docutils literal"><span class="pre">CELERY_ROUTES</span></tt></a>).</p>
  474. <p class="last">To find out more about routing, please see <a class="reference internal" href="routing.html#guide-routing"><em>Routing Tasks</em></a>.</p>
  475. </div>
  476. <div class="section" id="advanced-options">
  477. <h3>Advanced Options<a class="headerlink" href="#advanced-options" title="Permalink to this headline">¶</a></h3>
  478. <p>These options are for advanced users who want to take use of
  479. AMQP&#8217;s full routing capabilities. Interested parties may read the
  480. <a class="reference internal" href="routing.html#guide-routing"><em>routing guide</em></a>.</p>
  481. <ul>
  482. <li><p class="first">exchange</p>
  483. <blockquote>
  484. <div><p>Name of exchange (or a <a class="reference external" href="http://kombu.readthedocs.org/en/latest/reference/kombu.entity.html#kombu.entity.Exchange" title="(in Kombu v2.2)"><tt class="xref py py-class docutils literal"><span class="pre">kombu.entity.Exchange</span></tt></a>) to
  485. send the message to.</p>
  486. </div></blockquote>
  487. </li>
  488. <li><p class="first">routing_key</p>
  489. <blockquote>
  490. <div><p>Routing key used to determine.</p>
  491. </div></blockquote>
  492. </li>
  493. <li><p class="first">mandatory</p>
  494. <blockquote>
  495. <div><p>This sets the delivery to be mandatory. An exception will be raised
  496. if there are no running workers able to take on the task.</p>
  497. <p>Not supported by <tt class="xref py py-mod docutils literal"><span class="pre">amqplib</span></tt>.</p>
  498. </div></blockquote>
  499. </li>
  500. <li><p class="first">immediate</p>
  501. <blockquote>
  502. <div><p>Request immediate delivery. Will raise an exception
  503. if the task cannot be routed to a worker immediately.</p>
  504. <p>Not supported by <tt class="xref py py-mod docutils literal"><span class="pre">amqplib</span></tt>.</p>
  505. </div></blockquote>
  506. </li>
  507. <li><p class="first">priority</p>
  508. <blockquote>
  509. <div><p>A number between <cite>0</cite> and <cite>9</cite>, where <cite>0</cite> is the highest priority.</p>
  510. <p>Supported by: redis, beanstalk</p>
  511. </div></blockquote>
  512. </li>
  513. </ul>
  514. </div>
  515. </div>
  516. </div>
  517. </div>
  518. </div>
  519. </div>
  520. <div class="sphinxsidebar">
  521. <div class="sphinxsidebarwrapper"><p class="logo"><a href="../index.html">
  522. <img class="logo" src="http://cloud.github.com/downloads/celery/celery/celery_128.png" alt="Logo"/>
  523. </a></p>
  524. <h4>Previous topic</h4>
  525. <p class="topless"><a href="tasks.html"
  526. title="previous chapter">Tasks</a></p>
  527. <h4>Next topic</h4>
  528. <p class="topless"><a href="workers.html"
  529. title="next chapter">Workers Guide</a></p>
  530. <h3>This Page</h3>
  531. <ul class="this-page-menu">
  532. <li><a href="../_sources/userguide/calling.txt"
  533. rel="nofollow">Show Source</a></li>
  534. </ul>
  535. <div id="searchbox" style="display: none">
  536. <h3>Quick search</h3>
  537. <form class="search" action="../search.html" method="get">
  538. <input type="text" name="q" />
  539. <input type="submit" value="Go" />
  540. <input type="hidden" name="check_keywords" value="yes" />
  541. <input type="hidden" name="area" value="default" />
  542. </form>
  543. <p class="searchtip" style="font-size: 90%">
  544. Enter search terms or a module, class or function name.
  545. </p>
  546. </div>
  547. <script type="text/javascript">$('#searchbox').show(0);</script>
  548. </div>
  549. </div>
  550. <div class="clearer"></div>
  551. </div>
  552. <div class="related">
  553. <h3>Navigation</h3>
  554. <ul>
  555. <li class="right" style="margin-right: 10px">
  556. <a href="../genindex.html" title="General Index"
  557. >index</a></li>
  558. <li class="right" >
  559. <a href="../py-modindex.html" title="Python Module Index"
  560. >modules</a> |</li>
  561. <li class="right" >
  562. <a href="workers.html" title="Workers Guide"
  563. >next</a> |</li>
  564. <li class="right" >
  565. <a href="tasks.html" title="Tasks"
  566. >previous</a> |</li>
  567. <li><a href="../index.html">Celery 2.6.0rc4 documentation</a> &raquo;</li>
  568. <li><a href="index.html" >User Guide</a> &raquo;</li>
  569. </ul>
  570. </div>
  571. <div class="footer">
  572. &copy; Copyright 2009-2012, Ask Solem &amp; Contributors.
  573. </div>
  574. </body>
  575. </html>