gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

600 行
41KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="Futures" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/asyncio-future.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/asyncio/futures.py, Lib/asyncio/base_futures.py Future objects are used to bridge low-level callback-based code with high-level async/await code. Future Functions: Future Object: T..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/asyncio/futures.py, Lib/asyncio/base_futures.py Future objects are used to bridge low-level callback-based code with high-level async/await code. Future Functions: Future Object: T..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>Futures &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="Transports and Protocols" href="asyncio-protocol.html" />
  33. <link rel="prev" title="Event Loop" href="asyncio-eventloop.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/asyncio-future.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">Futures</a><ul>
  86. <li><a class="reference internal" href="#future-functions">Future Functions</a></li>
  87. <li><a class="reference internal" href="#future-object">Future Object</a></li>
  88. </ul>
  89. </li>
  90. </ul>
  91. </div>
  92. <div>
  93. <h4>Previous topic</h4>
  94. <p class="topless"><a href="asyncio-eventloop.html"
  95. title="previous chapter">Event Loop</a></p>
  96. </div>
  97. <div>
  98. <h4>Next topic</h4>
  99. <p class="topless"><a href="asyncio-protocol.html"
  100. title="next chapter">Transports and Protocols</a></p>
  101. </div>
  102. <div role="note" aria-label="source link">
  103. <h3>This Page</h3>
  104. <ul class="this-page-menu">
  105. <li><a href="../bugs.html">Report a Bug</a></li>
  106. <li>
  107. <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio-future.rst"
  108. rel="nofollow">Show Source
  109. </a>
  110. </li>
  111. </ul>
  112. </div>
  113. </nav>
  114. </div>
  115. </div>
  116. <div class="related" role="navigation" aria-label="related navigation">
  117. <h3>Navigation</h3>
  118. <ul>
  119. <li class="right" style="margin-right: 10px">
  120. <a href="../genindex.html" title="General Index"
  121. accesskey="I">index</a></li>
  122. <li class="right" >
  123. <a href="../py-modindex.html" title="Python Module Index"
  124. >modules</a> |</li>
  125. <li class="right" >
  126. <a href="asyncio-protocol.html" title="Transports and Protocols"
  127. accesskey="N">next</a> |</li>
  128. <li class="right" >
  129. <a href="asyncio-eventloop.html" title="Event Loop"
  130. accesskey="P">previous</a> |</li>
  131. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  132. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  133. <li class="switchers">
  134. <div class="language_switcher_placeholder"></div>
  135. <div class="version_switcher_placeholder"></div>
  136. </li>
  137. <li>
  138. </li>
  139. <li id="cpython-language-and-version">
  140. <a href="../index.html">3.12.3 Documentation</a> &#187;
  141. </li>
  142. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  143. <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
  144. <li class="nav-item nav-item-3"><a href="asyncio.html" accesskey="U"><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code> — Asynchronous I/O</a> &#187;</li>
  145. <li class="nav-item nav-item-this"><a href="">Futures</a></li>
  146. <li class="right">
  147. <div class="inline-search" role="search">
  148. <form class="inline-search" action="../search.html" method="get">
  149. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  150. <input type="submit" value="Go" />
  151. </form>
  152. </div>
  153. |
  154. </li>
  155. <li class="right">
  156. <label class="theme-selector-label">
  157. Theme
  158. <select class="theme-selector" oninput="activateTheme(this.value)">
  159. <option value="auto" selected>Auto</option>
  160. <option value="light">Light</option>
  161. <option value="dark">Dark</option>
  162. </select>
  163. </label> |</li>
  164. </ul>
  165. </div>
  166. <div class="document">
  167. <div class="documentwrapper">
  168. <div class="bodywrapper">
  169. <div class="body" role="main">
  170. <section id="futures">
  171. <span id="asyncio-futures"></span><h1>Futures<a class="headerlink" href="#futures" title="Link to this heading">¶</a></h1>
  172. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/asyncio/futures.py">Lib/asyncio/futures.py</a>,
  173. <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/asyncio/base_futures.py">Lib/asyncio/base_futures.py</a></p>
  174. <hr class="docutils" />
  175. <p><em>Future</em> objects are used to bridge <strong>low-level callback-based code</strong>
  176. with high-level async/await code.</p>
  177. <section id="future-functions">
  178. <h2>Future Functions<a class="headerlink" href="#future-functions" title="Link to this heading">¶</a></h2>
  179. <dl class="py function">
  180. <dt class="sig sig-object py" id="asyncio.isfuture">
  181. <span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">isfuture</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.isfuture" title="Link to this definition">¶</a></dt>
  182. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if <em>obj</em> is either of:</p>
  183. <ul class="simple">
  184. <li><p>an instance of <a class="reference internal" href="#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Future</span></code></a>,</p></li>
  185. <li><p>an instance of <a class="reference internal" href="asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Task</span></code></a>,</p></li>
  186. <li><p>a Future-like object with a <code class="docutils literal notranslate"><span class="pre">_asyncio_future_blocking</span></code>
  187. attribute.</p></li>
  188. </ul>
  189. <div class="versionadded">
  190. <p><span class="versionmodified added">New in version 3.5.</span></p>
  191. </div>
  192. </dd></dl>
  193. <dl class="py function">
  194. <dt class="sig sig-object py" id="asyncio.ensure_future">
  195. <span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">ensure_future</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">loop</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.ensure_future" title="Link to this definition">¶</a></dt>
  196. <dd><p>Return:</p>
  197. <ul class="simple">
  198. <li><p><em>obj</em> argument as is, if <em>obj</em> is a <a class="reference internal" href="#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a>,
  199. a <a class="reference internal" href="asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal notranslate"><span class="pre">Task</span></code></a>, or a Future-like object (<a class="reference internal" href="#asyncio.isfuture" title="asyncio.isfuture"><code class="xref py py-func docutils literal notranslate"><span class="pre">isfuture()</span></code></a>
  200. is used for the test.)</p></li>
  201. <li><p>a <a class="reference internal" href="asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal notranslate"><span class="pre">Task</span></code></a> object wrapping <em>obj</em>, if <em>obj</em> is a
  202. coroutine (<a class="reference internal" href="asyncio-task.html#asyncio.iscoroutine" title="asyncio.iscoroutine"><code class="xref py py-func docutils literal notranslate"><span class="pre">iscoroutine()</span></code></a> is used for the test);
  203. in this case the coroutine will be scheduled by
  204. <code class="docutils literal notranslate"><span class="pre">ensure_future()</span></code>.</p></li>
  205. <li><p>a <a class="reference internal" href="asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal notranslate"><span class="pre">Task</span></code></a> object that would await on <em>obj</em>, if <em>obj</em> is an
  206. awaitable (<a class="reference internal" href="inspect.html#inspect.isawaitable" title="inspect.isawaitable"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.isawaitable()</span></code></a> is used for the test.)</p></li>
  207. </ul>
  208. <p>If <em>obj</em> is neither of the above a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised.</p>
  209. <div class="admonition important">
  210. <p class="admonition-title">Important</p>
  211. <p>See also the <a class="reference internal" href="asyncio-task.html#asyncio.create_task" title="asyncio.create_task"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_task()</span></code></a> function which is the
  212. preferred way for creating new Tasks.</p>
  213. <p>Save a reference to the result of this function, to avoid
  214. a task disappearing mid-execution.</p>
  215. </div>
  216. <div class="versionchanged">
  217. <p><span class="versionmodified changed">Changed in version 3.5.1: </span>The function accepts any <a class="reference internal" href="../glossary.html#term-awaitable"><span class="xref std std-term">awaitable</span></a> object.</p>
  218. </div>
  219. <div class="deprecated">
  220. <p><span class="versionmodified deprecated">Deprecated since version 3.10: </span>Deprecation warning is emitted if <em>obj</em> is not a Future-like object
  221. and <em>loop</em> is not specified and there is no running event loop.</p>
  222. </div>
  223. </dd></dl>
  224. <dl class="py function">
  225. <dt class="sig sig-object py" id="asyncio.wrap_future">
  226. <span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">wrap_future</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">future</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">loop</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.wrap_future" title="Link to this definition">¶</a></dt>
  227. <dd><p>Wrap a <a class="reference internal" href="concurrent.futures.html#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">concurrent.futures.Future</span></code></a> object in a
  228. <a class="reference internal" href="#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Future</span></code></a> object.</p>
  229. <div class="deprecated">
  230. <p><span class="versionmodified deprecated">Deprecated since version 3.10: </span>Deprecation warning is emitted if <em>future</em> is not a Future-like object
  231. and <em>loop</em> is not specified and there is no running event loop.</p>
  232. </div>
  233. </dd></dl>
  234. </section>
  235. <section id="future-object">
  236. <h2>Future Object<a class="headerlink" href="#future-object" title="Link to this heading">¶</a></h2>
  237. <dl class="py class">
  238. <dt class="sig sig-object py" id="asyncio.Future">
  239. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">Future</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">loop</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Future" title="Link to this definition">¶</a></dt>
  240. <dd><p>A Future represents an eventual result of an asynchronous
  241. operation. Not thread-safe.</p>
  242. <p>Future is an <a class="reference internal" href="../glossary.html#term-awaitable"><span class="xref std std-term">awaitable</span></a> object. Coroutines can await on
  243. Future objects until they either have a result or an exception
  244. set, or until they are cancelled. A Future can be awaited multiple
  245. times and the result is same.</p>
  246. <p>Typically Futures are used to enable low-level
  247. callback-based code (e.g. in protocols implemented using asyncio
  248. <a class="reference internal" href="asyncio-protocol.html#asyncio-transports-protocols"><span class="std std-ref">transports</span></a>)
  249. to interoperate with high-level async/await code.</p>
  250. <p>The rule of thumb is to never expose Future objects in user-facing
  251. APIs, and the recommended way to create a Future object is to call
  252. <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_future" title="asyncio.loop.create_future"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_future()</span></code></a>. This way alternative event loop
  253. implementations can inject their own optimized implementations
  254. of a Future object.</p>
  255. <div class="versionchanged">
  256. <p><span class="versionmodified changed">Changed in version 3.7: </span>Added support for the <a class="reference internal" href="contextvars.html#module-contextvars" title="contextvars: Context Variables"><code class="xref py py-mod docutils literal notranslate"><span class="pre">contextvars</span></code></a> module.</p>
  257. </div>
  258. <div class="deprecated">
  259. <p><span class="versionmodified deprecated">Deprecated since version 3.10: </span>Deprecation warning is emitted if <em>loop</em> is not specified
  260. and there is no running event loop.</p>
  261. </div>
  262. <dl class="py method">
  263. <dt class="sig sig-object py" id="asyncio.Future.result">
  264. <span class="sig-name descname"><span class="pre">result</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Future.result" title="Link to this definition">¶</a></dt>
  265. <dd><p>Return the result of the Future.</p>
  266. <p>If the Future is <em>done</em> and has a result set by the
  267. <a class="reference internal" href="#asyncio.Future.set_result" title="asyncio.Future.set_result"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_result()</span></code></a> method, the result value is returned.</p>
  268. <p>If the Future is <em>done</em> and has an exception set by the
  269. <a class="reference internal" href="#asyncio.Future.set_exception" title="asyncio.Future.set_exception"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_exception()</span></code></a> method, this method raises the exception.</p>
  270. <p>If the Future has been <em>cancelled</em>, this method raises
  271. a <a class="reference internal" href="asyncio-exceptions.html#asyncio.CancelledError" title="asyncio.CancelledError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">CancelledError</span></code></a> exception.</p>
  272. <p>If the Future’s result isn’t yet available, this method raises
  273. a <a class="reference internal" href="asyncio-exceptions.html#asyncio.InvalidStateError" title="asyncio.InvalidStateError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InvalidStateError</span></code></a> exception.</p>
  274. </dd></dl>
  275. <dl class="py method">
  276. <dt class="sig sig-object py" id="asyncio.Future.set_result">
  277. <span class="sig-name descname"><span class="pre">set_result</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">result</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Future.set_result" title="Link to this definition">¶</a></dt>
  278. <dd><p>Mark the Future as <em>done</em> and set its result.</p>
  279. <p>Raises a <a class="reference internal" href="asyncio-exceptions.html#asyncio.InvalidStateError" title="asyncio.InvalidStateError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InvalidStateError</span></code></a> error if the Future is
  280. already <em>done</em>.</p>
  281. </dd></dl>
  282. <dl class="py method">
  283. <dt class="sig sig-object py" id="asyncio.Future.set_exception">
  284. <span class="sig-name descname"><span class="pre">set_exception</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exception</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Future.set_exception" title="Link to this definition">¶</a></dt>
  285. <dd><p>Mark the Future as <em>done</em> and set an exception.</p>
  286. <p>Raises a <a class="reference internal" href="asyncio-exceptions.html#asyncio.InvalidStateError" title="asyncio.InvalidStateError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InvalidStateError</span></code></a> error if the Future is
  287. already <em>done</em>.</p>
  288. </dd></dl>
  289. <dl class="py method">
  290. <dt class="sig sig-object py" id="asyncio.Future.done">
  291. <span class="sig-name descname"><span class="pre">done</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Future.done" title="Link to this definition">¶</a></dt>
  292. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the Future is <em>done</em>.</p>
  293. <p>A Future is <em>done</em> if it was <em>cancelled</em> or if it has a result
  294. or an exception set with <a class="reference internal" href="#asyncio.Future.set_result" title="asyncio.Future.set_result"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_result()</span></code></a> or
  295. <a class="reference internal" href="#asyncio.Future.set_exception" title="asyncio.Future.set_exception"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_exception()</span></code></a> calls.</p>
  296. </dd></dl>
  297. <dl class="py method">
  298. <dt class="sig sig-object py" id="asyncio.Future.cancelled">
  299. <span class="sig-name descname"><span class="pre">cancelled</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Future.cancelled" title="Link to this definition">¶</a></dt>
  300. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the Future was <em>cancelled</em>.</p>
  301. <p>The method is usually used to check if a Future is not
  302. <em>cancelled</em> before setting a result or an exception for it:</p>
  303. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="ow">not</span> <span class="n">fut</span><span class="o">.</span><span class="n">cancelled</span><span class="p">():</span>
  304. <span class="n">fut</span><span class="o">.</span><span class="n">set_result</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
  305. </pre></div>
  306. </div>
  307. </dd></dl>
  308. <dl class="py method">
  309. <dt class="sig sig-object py" id="asyncio.Future.add_done_callback">
  310. <span class="sig-name descname"><span class="pre">add_done_callback</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">callback</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Future.add_done_callback" title="Link to this definition">¶</a></dt>
  311. <dd><p>Add a callback to be run when the Future is <em>done</em>.</p>
  312. <p>The <em>callback</em> is called with the Future object as its only
  313. argument.</p>
  314. <p>If the Future is already <em>done</em> when this method is called,
  315. the callback is scheduled with <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.call_soon" title="asyncio.loop.call_soon"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.call_soon()</span></code></a>.</p>
  316. <p>An optional keyword-only <em>context</em> argument allows specifying a
  317. custom <a class="reference internal" href="contextvars.html#contextvars.Context" title="contextvars.Context"><code class="xref py py-class docutils literal notranslate"><span class="pre">contextvars.Context</span></code></a> for the <em>callback</em> to run in.
  318. The current context is used when no <em>context</em> is provided.</p>
  319. <p><a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a> can be used to pass parameters
  320. to the callback, e.g.:</p>
  321. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Call &#39;print(&quot;Future:&quot;, fut)&#39; when &quot;fut&quot; is done.</span>
  322. <span class="n">fut</span><span class="o">.</span><span class="n">add_done_callback</span><span class="p">(</span>
  323. <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="nb">print</span><span class="p">,</span> <span class="s2">&quot;Future:&quot;</span><span class="p">))</span>
  324. </pre></div>
  325. </div>
  326. <div class="versionchanged">
  327. <p><span class="versionmodified changed">Changed in version 3.7: </span>The <em>context</em> keyword-only parameter was added.
  328. See <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0567/"><strong>PEP 567</strong></a> for more details.</p>
  329. </div>
  330. </dd></dl>
  331. <dl class="py method">
  332. <dt class="sig sig-object py" id="asyncio.Future.remove_done_callback">
  333. <span class="sig-name descname"><span class="pre">remove_done_callback</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">callback</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Future.remove_done_callback" title="Link to this definition">¶</a></dt>
  334. <dd><p>Remove <em>callback</em> from the callbacks list.</p>
  335. <p>Returns the number of callbacks removed, which is typically 1,
  336. unless a callback was added more than once.</p>
  337. </dd></dl>
  338. <dl class="py method">
  339. <dt class="sig sig-object py" id="asyncio.Future.cancel">
  340. <span class="sig-name descname"><span class="pre">cancel</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Future.cancel" title="Link to this definition">¶</a></dt>
  341. <dd><p>Cancel the Future and schedule callbacks.</p>
  342. <p>If the Future is already <em>done</em> or <em>cancelled</em>, return <code class="docutils literal notranslate"><span class="pre">False</span></code>.
  343. Otherwise, change the Future’s state to <em>cancelled</em>,
  344. schedule the callbacks, and return <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  345. <div class="versionchanged">
  346. <p><span class="versionmodified changed">Changed in version 3.9: </span>Added the <em>msg</em> parameter.</p>
  347. </div>
  348. </dd></dl>
  349. <dl class="py method">
  350. <dt class="sig sig-object py" id="asyncio.Future.exception">
  351. <span class="sig-name descname"><span class="pre">exception</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Future.exception" title="Link to this definition">¶</a></dt>
  352. <dd><p>Return the exception that was set on this Future.</p>
  353. <p>The exception (or <code class="docutils literal notranslate"><span class="pre">None</span></code> if no exception was set) is
  354. returned only if the Future is <em>done</em>.</p>
  355. <p>If the Future has been <em>cancelled</em>, this method raises a
  356. <a class="reference internal" href="asyncio-exceptions.html#asyncio.CancelledError" title="asyncio.CancelledError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">CancelledError</span></code></a> exception.</p>
  357. <p>If the Future isn’t <em>done</em> yet, this method raises an
  358. <a class="reference internal" href="asyncio-exceptions.html#asyncio.InvalidStateError" title="asyncio.InvalidStateError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InvalidStateError</span></code></a> exception.</p>
  359. </dd></dl>
  360. <dl class="py method">
  361. <dt class="sig sig-object py" id="asyncio.Future.get_loop">
  362. <span class="sig-name descname"><span class="pre">get_loop</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Future.get_loop" title="Link to this definition">¶</a></dt>
  363. <dd><p>Return the event loop the Future object is bound to.</p>
  364. <div class="versionadded">
  365. <p><span class="versionmodified added">New in version 3.7.</span></p>
  366. </div>
  367. </dd></dl>
  368. </dd></dl>
  369. <p id="asyncio-example-future">This example creates a Future object, creates and schedules an
  370. asynchronous Task to set result for the Future, and waits until
  371. the Future has a result:</p>
  372. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">async</span> <span class="k">def</span> <span class="nf">set_after</span><span class="p">(</span><span class="n">fut</span><span class="p">,</span> <span class="n">delay</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
  373. <span class="c1"># Sleep for *delay* seconds.</span>
  374. <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">delay</span><span class="p">)</span>
  375. <span class="c1"># Set *value* as a result of *fut* Future.</span>
  376. <span class="n">fut</span><span class="o">.</span><span class="n">set_result</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
  377. <span class="k">async</span> <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
  378. <span class="c1"># Get the current event loop.</span>
  379. <span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_running_loop</span><span class="p">()</span>
  380. <span class="c1"># Create a new Future object.</span>
  381. <span class="n">fut</span> <span class="o">=</span> <span class="n">loop</span><span class="o">.</span><span class="n">create_future</span><span class="p">()</span>
  382. <span class="c1"># Run &quot;set_after()&quot; coroutine in a parallel Task.</span>
  383. <span class="c1"># We are using the low-level &quot;loop.create_task()&quot; API here because</span>
  384. <span class="c1"># we already have a reference to the event loop at hand.</span>
  385. <span class="c1"># Otherwise we could have just used &quot;asyncio.create_task()&quot;.</span>
  386. <span class="n">loop</span><span class="o">.</span><span class="n">create_task</span><span class="p">(</span>
  387. <span class="n">set_after</span><span class="p">(</span><span class="n">fut</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;... world&#39;</span><span class="p">))</span>
  388. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;hello ...&#39;</span><span class="p">)</span>
  389. <span class="c1"># Wait until *fut* has a result (1 second) and print it.</span>
  390. <span class="nb">print</span><span class="p">(</span><span class="k">await</span> <span class="n">fut</span><span class="p">)</span>
  391. <span class="n">asyncio</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">main</span><span class="p">())</span>
  392. </pre></div>
  393. </div>
  394. <div class="admonition important">
  395. <p class="admonition-title">Important</p>
  396. <p>The Future object was designed to mimic
  397. <a class="reference internal" href="concurrent.futures.html#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">concurrent.futures.Future</span></code></a>. Key differences include:</p>
  398. <ul class="simple">
  399. <li><p>unlike asyncio Futures, <a class="reference internal" href="concurrent.futures.html#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">concurrent.futures.Future</span></code></a>
  400. instances cannot be awaited.</p></li>
  401. <li><p><a class="reference internal" href="#asyncio.Future.result" title="asyncio.Future.result"><code class="xref py py-meth docutils literal notranslate"><span class="pre">asyncio.Future.result()</span></code></a> and <a class="reference internal" href="#asyncio.Future.exception" title="asyncio.Future.exception"><code class="xref py py-meth docutils literal notranslate"><span class="pre">asyncio.Future.exception()</span></code></a>
  402. do not accept the <em>timeout</em> argument.</p></li>
  403. <li><p><a class="reference internal" href="#asyncio.Future.result" title="asyncio.Future.result"><code class="xref py py-meth docutils literal notranslate"><span class="pre">asyncio.Future.result()</span></code></a> and <a class="reference internal" href="#asyncio.Future.exception" title="asyncio.Future.exception"><code class="xref py py-meth docutils literal notranslate"><span class="pre">asyncio.Future.exception()</span></code></a>
  404. raise an <a class="reference internal" href="asyncio-exceptions.html#asyncio.InvalidStateError" title="asyncio.InvalidStateError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InvalidStateError</span></code></a> exception when the Future is not
  405. <em>done</em>.</p></li>
  406. <li><p>Callbacks registered with <a class="reference internal" href="#asyncio.Future.add_done_callback" title="asyncio.Future.add_done_callback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">asyncio.Future.add_done_callback()</span></code></a>
  407. are not called immediately. They are scheduled with
  408. <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.call_soon" title="asyncio.loop.call_soon"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.call_soon()</span></code></a> instead.</p></li>
  409. <li><p>asyncio Future is not compatible with the
  410. <a class="reference internal" href="concurrent.futures.html#concurrent.futures.wait" title="concurrent.futures.wait"><code class="xref py py-func docutils literal notranslate"><span class="pre">concurrent.futures.wait()</span></code></a> and
  411. <a class="reference internal" href="concurrent.futures.html#concurrent.futures.as_completed" title="concurrent.futures.as_completed"><code class="xref py py-func docutils literal notranslate"><span class="pre">concurrent.futures.as_completed()</span></code></a> functions.</p></li>
  412. <li><p><a class="reference internal" href="#asyncio.Future.cancel" title="asyncio.Future.cancel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">asyncio.Future.cancel()</span></code></a> accepts an optional <code class="docutils literal notranslate"><span class="pre">msg</span></code> argument,
  413. but <a class="reference internal" href="concurrent.futures.html#concurrent.futures.Future.cancel" title="concurrent.futures.Future.cancel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">concurrent.futures.Future.cancel()</span></code></a> does not.</p></li>
  414. </ul>
  415. </div>
  416. </section>
  417. </section>
  418. <div class="clearer"></div>
  419. </div>
  420. </div>
  421. </div>
  422. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  423. <div class="sphinxsidebarwrapper">
  424. <div>
  425. <h3><a href="../contents.html">Table of Contents</a></h3>
  426. <ul>
  427. <li><a class="reference internal" href="#">Futures</a><ul>
  428. <li><a class="reference internal" href="#future-functions">Future Functions</a></li>
  429. <li><a class="reference internal" href="#future-object">Future Object</a></li>
  430. </ul>
  431. </li>
  432. </ul>
  433. </div>
  434. <div>
  435. <h4>Previous topic</h4>
  436. <p class="topless"><a href="asyncio-eventloop.html"
  437. title="previous chapter">Event Loop</a></p>
  438. </div>
  439. <div>
  440. <h4>Next topic</h4>
  441. <p class="topless"><a href="asyncio-protocol.html"
  442. title="next chapter">Transports and Protocols</a></p>
  443. </div>
  444. <div role="note" aria-label="source link">
  445. <h3>This Page</h3>
  446. <ul class="this-page-menu">
  447. <li><a href="../bugs.html">Report a Bug</a></li>
  448. <li>
  449. <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio-future.rst"
  450. rel="nofollow">Show Source
  451. </a>
  452. </li>
  453. </ul>
  454. </div>
  455. </div>
  456. <div id="sidebarbutton" title="Collapse sidebar">
  457. <span>«</span>
  458. </div>
  459. </div>
  460. <div class="clearer"></div>
  461. </div>
  462. <div class="related" role="navigation" aria-label="related navigation">
  463. <h3>Navigation</h3>
  464. <ul>
  465. <li class="right" style="margin-right: 10px">
  466. <a href="../genindex.html" title="General Index"
  467. >index</a></li>
  468. <li class="right" >
  469. <a href="../py-modindex.html" title="Python Module Index"
  470. >modules</a> |</li>
  471. <li class="right" >
  472. <a href="asyncio-protocol.html" title="Transports and Protocols"
  473. >next</a> |</li>
  474. <li class="right" >
  475. <a href="asyncio-eventloop.html" title="Event Loop"
  476. >previous</a> |</li>
  477. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  478. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  479. <li class="switchers">
  480. <div class="language_switcher_placeholder"></div>
  481. <div class="version_switcher_placeholder"></div>
  482. </li>
  483. <li>
  484. </li>
  485. <li id="cpython-language-and-version">
  486. <a href="../index.html">3.12.3 Documentation</a> &#187;
  487. </li>
  488. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  489. <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
  490. <li class="nav-item nav-item-3"><a href="asyncio.html" ><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code> — Asynchronous I/O</a> &#187;</li>
  491. <li class="nav-item nav-item-this"><a href="">Futures</a></li>
  492. <li class="right">
  493. <div class="inline-search" role="search">
  494. <form class="inline-search" action="../search.html" method="get">
  495. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  496. <input type="submit" value="Go" />
  497. </form>
  498. </div>
  499. |
  500. </li>
  501. <li class="right">
  502. <label class="theme-selector-label">
  503. Theme
  504. <select class="theme-selector" oninput="activateTheme(this.value)">
  505. <option value="auto" selected>Auto</option>
  506. <option value="light">Light</option>
  507. <option value="dark">Dark</option>
  508. </select>
  509. </label> |</li>
  510. </ul>
  511. </div>
  512. <div class="footer">
  513. &copy;
  514. <a href="../copyright.html">
  515. Copyright
  516. </a>
  517. 2001-2024, Python Software Foundation.
  518. <br />
  519. This page is licensed under the Python Software Foundation License Version 2.
  520. <br />
  521. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  522. <br />
  523. See <a href="/license.html">History and License</a> for more information.<br />
  524. <br />
  525. The Python Software Foundation is a non-profit corporation.
  526. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  527. <br />
  528. <br />
  529. Last updated on Apr 09, 2024 (13:47 UTC).
  530. <a href="/bugs.html">Found a bug</a>?
  531. <br />
  532. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  533. </div>
  534. </body>
  535. </html>
上海开阖软件有限公司 沪ICP备12045867号-1