/usr/share/doc/python-tornado-doc/html/concurrent.html is in python-tornado-doc 4.4.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | <!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>tornado.concurrent — Work with threads and futures — Tornado 4.4.3 documentation</title>
<link rel="shortcut icon" href="_static/favicon.ico"/>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="index" title="Index"
href="genindex.html"/>
<link rel="search" title="Search" href="search.html"/>
<link rel="top" title="Tornado 4.4.3 documentation" href="index.html"/>
<link rel="up" title="Coroutines and concurrency" href="coroutine.html"/>
<link rel="next" title="tornado.locks – Synchronization primitives" href="locks.html"/>
<link rel="prev" title="tornado.gen — Simplify asynchronous code" href="gen.html"/>
<script src="_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="index.html" class="icon icon-home"> Tornado
</a>
<div class="version">
4.4.3
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="guide.html">User’s guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="webframework.html">Web framework</a></li>
<li class="toctree-l1"><a class="reference internal" href="http.html">HTTP servers and clients</a></li>
<li class="toctree-l1"><a class="reference internal" href="networking.html">Asynchronous networking</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="coroutine.html">Coroutines and concurrency</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="gen.html"><code class="docutils literal"><span class="pre">tornado.gen</span></code> — Simplify asynchronous code</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#"><code class="docutils literal"><span class="pre">tornado.concurrent</span></code> — Work with threads and futures</a></li>
<li class="toctree-l2"><a class="reference internal" href="locks.html"><code class="docutils literal"><span class="pre">tornado.locks</span></code> – Synchronization primitives</a></li>
<li class="toctree-l2"><a class="reference internal" href="queues.html"><code class="docutils literal"><span class="pre">tornado.queues</span></code> – Queues for coroutines</a></li>
<li class="toctree-l2"><a class="reference internal" href="process.html"><code class="docutils literal"><span class="pre">tornado.process</span></code> — Utilities for multiple processes</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="integration.html">Integration with other services</a></li>
<li class="toctree-l1"><a class="reference internal" href="utilities.html">Utilities</a></li>
<li class="toctree-l1"><a class="reference internal" href="faq.html">Frequently Asked Questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="releases.html">Release notes</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">Tornado</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> »</li>
<li><a href="coroutine.html">Coroutines and concurrency</a> »</li>
<li><code class="docutils literal"><span class="pre">tornado.concurrent</span></code> — Work with threads and futures</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/concurrent.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="tornado-concurrent-work-with-threads-and-futures">
<h1><code class="docutils literal"><span class="pre">tornado.concurrent</span></code> — Work with threads and futures<a class="headerlink" href="#tornado-concurrent-work-with-threads-and-futures" title="Permalink to this headline">¶</a></h1>
<span class="target" id="module-tornado.concurrent"></span><p>Utilities for working with threads and <code class="docutils literal"><span class="pre">Futures</span></code>.</p>
<p><code class="docutils literal"><span class="pre">Futures</span></code> are a pattern for concurrent programming introduced in
Python 3.2 in the <a class="reference external" href="/usr/share/doc/python3-doc/html/library/concurrent.futures.html#module-concurrent.futures" title="(in Python v3.5)"><code class="xref py py-obj docutils literal"><span class="pre">concurrent.futures</span></code></a> package. This package defines
a mostly-compatible <a class="reference internal" href="#tornado.concurrent.Future" title="tornado.concurrent.Future"><code class="xref py py-obj docutils literal"><span class="pre">Future</span></code></a> class designed for use from coroutines,
as well as some utility functions for interacting with the
<a class="reference external" href="/usr/share/doc/python3-doc/html/library/concurrent.futures.html#module-concurrent.futures" title="(in Python v3.5)"><code class="xref py py-obj docutils literal"><span class="pre">concurrent.futures</span></code></a> package.</p>
<dl class="class">
<dt id="tornado.concurrent.Future">
<em class="property">class </em><code class="descclassname">tornado.concurrent.</code><code class="descname">Future</code><a class="reference internal" href="_modules/tornado/concurrent.html#Future"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.concurrent.Future" title="Permalink to this definition">¶</a></dt>
<dd><p>Placeholder for an asynchronous result.</p>
<p>A <code class="docutils literal"><span class="pre">Future</span></code> encapsulates the result of an asynchronous
operation. In synchronous applications <code class="docutils literal"><span class="pre">Futures</span></code> are used
to wait for the result from a thread or process pool; in
Tornado they are normally used with <a class="reference internal" href="ioloop.html#tornado.ioloop.IOLoop.add_future" title="tornado.ioloop.IOLoop.add_future"><code class="xref py py-obj docutils literal"><span class="pre">IOLoop.add_future</span></code></a> or by
yielding them in a <a class="reference internal" href="gen.html#tornado.gen.coroutine" title="tornado.gen.coroutine"><code class="xref py py-obj docutils literal"><span class="pre">gen.coroutine</span></code></a>.</p>
<p><a class="reference internal" href="#tornado.concurrent.Future" title="tornado.concurrent.Future"><code class="xref py py-obj docutils literal"><span class="pre">tornado.concurrent.Future</span></code></a> is similar to
<a class="reference external" href="/usr/share/doc/python3-doc/html/library/concurrent.futures.html#concurrent.futures.Future" title="(in Python v3.5)"><code class="xref py py-obj docutils literal"><span class="pre">concurrent.futures.Future</span></code></a>, but not thread-safe (and therefore
faster for use with single-threaded event loops).</p>
<p>In addition to <code class="docutils literal"><span class="pre">exception</span></code> and <code class="docutils literal"><span class="pre">set_exception</span></code>, methods <code class="docutils literal"><span class="pre">exc_info</span></code>
and <code class="docutils literal"><span class="pre">set_exc_info</span></code> are supported to capture tracebacks in Python 2.
The traceback is automatically available in Python 3, but in the
Python 2 futures backport this information is discarded.
This functionality was previously available in a separate class
<code class="docutils literal"><span class="pre">TracebackFuture</span></code>, which is now a deprecated alias for this class.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 4.0: </span><a class="reference internal" href="#tornado.concurrent.Future" title="tornado.concurrent.Future"><code class="xref py py-obj docutils literal"><span class="pre">tornado.concurrent.Future</span></code></a> is always a thread-unsafe <code class="docutils literal"><span class="pre">Future</span></code>
with support for the <code class="docutils literal"><span class="pre">exc_info</span></code> methods. Previously it would
be an alias for the thread-safe <a class="reference external" href="/usr/share/doc/python3-doc/html/library/concurrent.futures.html#concurrent.futures.Future" title="(in Python v3.5)"><code class="xref py py-obj docutils literal"><span class="pre">concurrent.futures.Future</span></code></a>
if that package was available and fall back to the thread-unsafe
implementation if it was not.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 4.1: </span>If a <a class="reference internal" href="#tornado.concurrent.Future" title="tornado.concurrent.Future"><code class="xref py py-obj docutils literal"><span class="pre">Future</span></code></a> contains an error but that error is never observed
(by calling <code class="docutils literal"><span class="pre">result()</span></code>, <code class="docutils literal"><span class="pre">exception()</span></code>, or <code class="docutils literal"><span class="pre">exc_info()</span></code>),
a stack trace will be logged when the <a class="reference internal" href="#tornado.concurrent.Future" title="tornado.concurrent.Future"><code class="xref py py-obj docutils literal"><span class="pre">Future</span></code></a> is garbage collected.
This normally indicates an error in the application, but in cases
where it results in undesired logging it may be necessary to
suppress the logging by ensuring that the exception is observed:
<code class="docutils literal"><span class="pre">f.add_done_callback(lambda</span> <span class="pre">f:</span> <span class="pre">f.exception())</span></code>.</p>
</div>
</dd></dl>
<div class="section" id="consumer-methods">
<h2>Consumer methods<a class="headerlink" href="#consumer-methods" title="Permalink to this headline">¶</a></h2>
<dl class="method">
<dt id="tornado.concurrent.Future.result">
<code class="descclassname">Future.</code><code class="descname">result</code><span class="sig-paren">(</span><em>timeout=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/concurrent.html#Future.result"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.concurrent.Future.result" title="Permalink to this definition">¶</a></dt>
<dd><p>If the operation succeeded, return its result. If it failed,
re-raise its exception.</p>
<p>This method takes a <code class="docutils literal"><span class="pre">timeout</span></code> argument for compatibility with
<a class="reference external" href="/usr/share/doc/python3-doc/html/library/concurrent.futures.html#concurrent.futures.Future" title="(in Python v3.5)"><code class="xref py py-obj docutils literal"><span class="pre">concurrent.futures.Future</span></code></a> but it is an error to call it
before the <a class="reference internal" href="#tornado.concurrent.Future" title="tornado.concurrent.Future"><code class="xref py py-obj docutils literal"><span class="pre">Future</span></code></a> is done, so the <code class="docutils literal"><span class="pre">timeout</span></code> is never used.</p>
</dd></dl>
<dl class="method">
<dt id="tornado.concurrent.Future.exception">
<code class="descclassname">Future.</code><code class="descname">exception</code><span class="sig-paren">(</span><em>timeout=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/concurrent.html#Future.exception"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.concurrent.Future.exception" title="Permalink to this definition">¶</a></dt>
<dd><p>If the operation raised an exception, return the <a class="reference external" href="/usr/share/doc/python3-doc/html/library/exceptions.html#Exception" title="(in Python v3.5)"><code class="xref py py-obj docutils literal"><span class="pre">Exception</span></code></a>
object. Otherwise returns None.</p>
<p>This method takes a <code class="docutils literal"><span class="pre">timeout</span></code> argument for compatibility with
<a class="reference external" href="/usr/share/doc/python3-doc/html/library/concurrent.futures.html#concurrent.futures.Future" title="(in Python v3.5)"><code class="xref py py-obj docutils literal"><span class="pre">concurrent.futures.Future</span></code></a> but it is an error to call it
before the <a class="reference internal" href="#tornado.concurrent.Future" title="tornado.concurrent.Future"><code class="xref py py-obj docutils literal"><span class="pre">Future</span></code></a> is done, so the <code class="docutils literal"><span class="pre">timeout</span></code> is never used.</p>
</dd></dl>
<dl class="method">
<dt id="tornado.concurrent.Future.exc_info">
<code class="descclassname">Future.</code><code class="descname">exc_info</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/concurrent.html#Future.exc_info"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.concurrent.Future.exc_info" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a tuple in the same format as <a class="reference external" href="/usr/share/doc/python3-doc/html/library/sys.html#sys.exc_info" title="(in Python v3.5)"><code class="xref py py-obj docutils literal"><span class="pre">sys.exc_info</span></code></a> or None.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 4.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="tornado.concurrent.Future.add_done_callback">
<code class="descclassname">Future.</code><code class="descname">add_done_callback</code><span class="sig-paren">(</span><em>fn</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/concurrent.html#Future.add_done_callback"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.concurrent.Future.add_done_callback" title="Permalink to this definition">¶</a></dt>
<dd><p>Attaches the given callback to the <a class="reference internal" href="#tornado.concurrent.Future" title="tornado.concurrent.Future"><code class="xref py py-obj docutils literal"><span class="pre">Future</span></code></a>.</p>
<p>It will be invoked with the <a class="reference internal" href="#tornado.concurrent.Future" title="tornado.concurrent.Future"><code class="xref py py-obj docutils literal"><span class="pre">Future</span></code></a> as its argument when the Future
has finished running and its result is available. In Tornado
consider using <a class="reference internal" href="ioloop.html#tornado.ioloop.IOLoop.add_future" title="tornado.ioloop.IOLoop.add_future"><code class="xref py py-obj docutils literal"><span class="pre">IOLoop.add_future</span></code></a> instead of calling
<a class="reference internal" href="#tornado.concurrent.Future.add_done_callback" title="tornado.concurrent.Future.add_done_callback"><code class="xref py py-obj docutils literal"><span class="pre">add_done_callback</span></code></a> directly.</p>
</dd></dl>
<dl class="method">
<dt id="tornado.concurrent.Future.done">
<code class="descclassname">Future.</code><code class="descname">done</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/concurrent.html#Future.done"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.concurrent.Future.done" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if the future has finished running.</p>
</dd></dl>
<dl class="method">
<dt id="tornado.concurrent.Future.running">
<code class="descclassname">Future.</code><code class="descname">running</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/concurrent.html#Future.running"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.concurrent.Future.running" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if this operation is currently running.</p>
</dd></dl>
<dl class="method">
<dt id="tornado.concurrent.Future.cancel">
<code class="descclassname">Future.</code><code class="descname">cancel</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/concurrent.html#Future.cancel"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.concurrent.Future.cancel" title="Permalink to this definition">¶</a></dt>
<dd><p>Cancel the operation, if possible.</p>
<p>Tornado <code class="docutils literal"><span class="pre">Futures</span></code> do not support cancellation, so this method always
returns False.</p>
</dd></dl>
<dl class="method">
<dt id="tornado.concurrent.Future.cancelled">
<code class="descclassname">Future.</code><code class="descname">cancelled</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/concurrent.html#Future.cancelled"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.concurrent.Future.cancelled" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if the operation has been cancelled.</p>
<p>Tornado <code class="docutils literal"><span class="pre">Futures</span></code> do not support cancellation, so this method
always returns False.</p>
</dd></dl>
</div>
<div class="section" id="producer-methods">
<h2>Producer methods<a class="headerlink" href="#producer-methods" title="Permalink to this headline">¶</a></h2>
<dl class="method">
<dt id="tornado.concurrent.Future.set_result">
<code class="descclassname">Future.</code><code class="descname">set_result</code><span class="sig-paren">(</span><em>result</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/concurrent.html#Future.set_result"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.concurrent.Future.set_result" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets the result of a <code class="docutils literal"><span class="pre">Future</span></code>.</p>
<p>It is undefined to call any of the <code class="docutils literal"><span class="pre">set</span></code> methods more than once
on the same object.</p>
</dd></dl>
<dl class="method">
<dt id="tornado.concurrent.Future.set_exception">
<code class="descclassname">Future.</code><code class="descname">set_exception</code><span class="sig-paren">(</span><em>exception</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/concurrent.html#Future.set_exception"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.concurrent.Future.set_exception" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets the exception of a <code class="docutils literal"><span class="pre">Future.</span></code></p>
</dd></dl>
<dl class="method">
<dt id="tornado.concurrent.Future.set_exc_info">
<code class="descclassname">Future.</code><code class="descname">set_exc_info</code><span class="sig-paren">(</span><em>exc_info</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/concurrent.html#Future.set_exc_info"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.concurrent.Future.set_exc_info" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets the exception information of a <code class="docutils literal"><span class="pre">Future.</span></code></p>
<p>Preserves tracebacks on Python 2.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 4.0.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="tornado.concurrent.run_on_executor">
<code class="descclassname">tornado.concurrent.</code><code class="descname">run_on_executor</code><span class="sig-paren">(</span><em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/concurrent.html#run_on_executor"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.concurrent.run_on_executor" title="Permalink to this definition">¶</a></dt>
<dd><p>Decorator to run a synchronous method asynchronously on an executor.</p>
<p>The decorated method may be called with a <code class="docutils literal"><span class="pre">callback</span></code> keyword
argument and returns a future.</p>
<p>The <a class="reference internal" href="ioloop.html#tornado.ioloop.IOLoop" title="tornado.ioloop.IOLoop"><code class="xref py py-obj docutils literal"><span class="pre">IOLoop</span></code></a> and executor to be used are determined by the <code class="docutils literal"><span class="pre">io_loop</span></code>
and <code class="docutils literal"><span class="pre">executor</span></code> attributes of <code class="docutils literal"><span class="pre">self</span></code>. To use different attributes,
pass keyword arguments to the decorator:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nd">@run_on_executor</span><span class="p">(</span><span class="n">executor</span><span class="o">=</span><span class="s1">'_thread_pool'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
</pre></div>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 4.2: </span>Added keyword arguments to use alternative attributes.</p>
</div>
</dd></dl>
<dl class="function">
<dt id="tornado.concurrent.return_future">
<code class="descclassname">tornado.concurrent.</code><code class="descname">return_future</code><span class="sig-paren">(</span><em>f</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/concurrent.html#return_future"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.concurrent.return_future" title="Permalink to this definition">¶</a></dt>
<dd><p>Decorator to make a function that returns via callback return a
<a class="reference internal" href="#tornado.concurrent.Future" title="tornado.concurrent.Future"><code class="xref py py-obj docutils literal"><span class="pre">Future</span></code></a>.</p>
<p>The wrapped function should take a <code class="docutils literal"><span class="pre">callback</span></code> keyword argument
and invoke it with one argument when it has finished. To signal failure,
the function can simply raise an exception (which will be
captured by the <a class="reference internal" href="stack_context.html#tornado.stack_context.StackContext" title="tornado.stack_context.StackContext"><code class="xref py py-obj docutils literal"><span class="pre">StackContext</span></code></a> and passed along to the <code class="docutils literal"><span class="pre">Future</span></code>).</p>
<p>From the caller’s perspective, the callback argument is optional.
If one is given, it will be invoked when the function is complete
with <a class="reference internal" href="#tornado.concurrent.Future.result" title="tornado.concurrent.Future.result"><code class="xref py py-obj docutils literal"><span class="pre">Future.result()</span></code></a> as an argument. If the function fails, the
callback will not be run and an exception will be raised into the
surrounding <a class="reference internal" href="stack_context.html#tornado.stack_context.StackContext" title="tornado.stack_context.StackContext"><code class="xref py py-obj docutils literal"><span class="pre">StackContext</span></code></a>.</p>
<p>If no callback is given, the caller should use the <code class="docutils literal"><span class="pre">Future</span></code> to
wait for the function to complete (perhaps by yielding it in a
<a class="reference internal" href="gen.html#tornado.gen.engine" title="tornado.gen.engine"><code class="xref py py-obj docutils literal"><span class="pre">gen.engine</span></code></a> function, or passing it to <a class="reference internal" href="ioloop.html#tornado.ioloop.IOLoop.add_future" title="tornado.ioloop.IOLoop.add_future"><code class="xref py py-obj docutils literal"><span class="pre">IOLoop.add_future</span></code></a>).</p>
<p>Usage:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nd">@return_future</span>
<span class="k">def</span> <span class="nf">future_func</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">callback</span><span class="p">):</span>
<span class="c1"># Do stuff (possibly asynchronous)</span>
<span class="n">callback</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
<span class="nd">@gen</span><span class="o">.</span><span class="n">engine</span>
<span class="k">def</span> <span class="nf">caller</span><span class="p">(</span><span class="n">callback</span><span class="p">):</span>
<span class="k">yield</span> <span class="n">future_func</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">callback</span><span class="p">()</span>
</pre></div>
</div>
<p>Note that <code class="docutils literal"><span class="pre">@return_future</span></code> and <code class="docutils literal"><span class="pre">@gen.engine</span></code> can be applied to the
same function, provided <code class="docutils literal"><span class="pre">@return_future</span></code> appears first. However,
consider using <code class="docutils literal"><span class="pre">@gen.coroutine</span></code> instead of this combination.</p>
</dd></dl>
<dl class="function">
<dt id="tornado.concurrent.chain_future">
<code class="descclassname">tornado.concurrent.</code><code class="descname">chain_future</code><span class="sig-paren">(</span><em>a</em>, <em>b</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/concurrent.html#chain_future"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.concurrent.chain_future" title="Permalink to this definition">¶</a></dt>
<dd><p>Chain two futures together so that when one completes, so does the other.</p>
<p>The result (success or failure) of <code class="docutils literal"><span class="pre">a</span></code> will be copied to <code class="docutils literal"><span class="pre">b</span></code>, unless
<code class="docutils literal"><span class="pre">b</span></code> has already been completed or cancelled by the time <code class="docutils literal"><span class="pre">a</span></code> finishes.</p>
</dd></dl>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="locks.html" class="btn btn-neutral float-right" title="tornado.locks – Synchronization primitives" accesskey="n">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="gen.html" class="btn btn-neutral" title="tornado.gen — Simplify asynchronous code" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2009-2017, The Tornado Authors.
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'./',
VERSION:'4.4.3',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>
|