gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

954 lines
87KB

  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="concurrent.futures — Launching parallel tasks" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/concurrent.futures.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/concurrent/futures/thread.py and Lib/concurrent/futures/process.py The concurrent.futures module provides a high-level interface for asynchronously executing callables. The asynchr..." />
  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/concurrent/futures/thread.py and Lib/concurrent/futures/process.py The concurrent.futures module provides a high-level interface for asynchronously executing callables. The asynchr..." />
  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>concurrent.futures — Launching parallel tasks &#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="subprocess — Subprocess management" href="subprocess.html" />
  33. <link rel="prev" title="The concurrent package" href="concurrent.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/concurrent.futures.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="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code> — Launching parallel tasks</a><ul>
  86. <li><a class="reference internal" href="#executor-objects">Executor Objects</a></li>
  87. <li><a class="reference internal" href="#threadpoolexecutor">ThreadPoolExecutor</a><ul>
  88. <li><a class="reference internal" href="#threadpoolexecutor-example">ThreadPoolExecutor Example</a></li>
  89. </ul>
  90. </li>
  91. <li><a class="reference internal" href="#processpoolexecutor">ProcessPoolExecutor</a><ul>
  92. <li><a class="reference internal" href="#processpoolexecutor-example">ProcessPoolExecutor Example</a></li>
  93. </ul>
  94. </li>
  95. <li><a class="reference internal" href="#future-objects">Future Objects</a></li>
  96. <li><a class="reference internal" href="#module-functions">Module Functions</a></li>
  97. <li><a class="reference internal" href="#exception-classes">Exception classes</a></li>
  98. </ul>
  99. </li>
  100. </ul>
  101. </div>
  102. <div>
  103. <h4>Previous topic</h4>
  104. <p class="topless"><a href="concurrent.html"
  105. title="previous chapter">The <code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent</span></code> package</a></p>
  106. </div>
  107. <div>
  108. <h4>Next topic</h4>
  109. <p class="topless"><a href="subprocess.html"
  110. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> — Subprocess management</a></p>
  111. </div>
  112. <div role="note" aria-label="source link">
  113. <h3>This Page</h3>
  114. <ul class="this-page-menu">
  115. <li><a href="../bugs.html">Report a Bug</a></li>
  116. <li>
  117. <a href="https://github.com/python/cpython/blob/main/Doc/library/concurrent.futures.rst"
  118. rel="nofollow">Show Source
  119. </a>
  120. </li>
  121. </ul>
  122. </div>
  123. </nav>
  124. </div>
  125. </div>
  126. <div class="related" role="navigation" aria-label="related navigation">
  127. <h3>Navigation</h3>
  128. <ul>
  129. <li class="right" style="margin-right: 10px">
  130. <a href="../genindex.html" title="General Index"
  131. accesskey="I">index</a></li>
  132. <li class="right" >
  133. <a href="../py-modindex.html" title="Python Module Index"
  134. >modules</a> |</li>
  135. <li class="right" >
  136. <a href="subprocess.html" title="subprocess — Subprocess management"
  137. accesskey="N">next</a> |</li>
  138. <li class="right" >
  139. <a href="concurrent.html" title="The concurrent package"
  140. accesskey="P">previous</a> |</li>
  141. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  142. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  143. <li class="switchers">
  144. <div class="language_switcher_placeholder"></div>
  145. <div class="version_switcher_placeholder"></div>
  146. </li>
  147. <li>
  148. </li>
  149. <li id="cpython-language-and-version">
  150. <a href="../index.html">3.12.3 Documentation</a> &#187;
  151. </li>
  152. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  153. <li class="nav-item nav-item-2"><a href="concurrency.html" accesskey="U">Concurrent Execution</a> &#187;</li>
  154. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code> — Launching parallel tasks</a></li>
  155. <li class="right">
  156. <div class="inline-search" role="search">
  157. <form class="inline-search" action="../search.html" method="get">
  158. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  159. <input type="submit" value="Go" />
  160. </form>
  161. </div>
  162. |
  163. </li>
  164. <li class="right">
  165. <label class="theme-selector-label">
  166. Theme
  167. <select class="theme-selector" oninput="activateTheme(this.value)">
  168. <option value="auto" selected>Auto</option>
  169. <option value="light">Light</option>
  170. <option value="dark">Dark</option>
  171. </select>
  172. </label> |</li>
  173. </ul>
  174. </div>
  175. <div class="document">
  176. <div class="documentwrapper">
  177. <div class="bodywrapper">
  178. <div class="body" role="main">
  179. <section id="module-concurrent.futures">
  180. <span id="concurrent-futures-launching-parallel-tasks"></span><h1><a class="reference internal" href="#module-concurrent.futures" title="concurrent.futures: Execute computations concurrently using threads or processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code></a> — Launching parallel tasks<a class="headerlink" href="#module-concurrent.futures" title="Link to this heading">¶</a></h1>
  181. <div class="versionadded">
  182. <p><span class="versionmodified added">New in version 3.2.</span></p>
  183. </div>
  184. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/concurrent/futures/thread.py">Lib/concurrent/futures/thread.py</a>
  185. and <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/concurrent/futures/process.py">Lib/concurrent/futures/process.py</a></p>
  186. <hr class="docutils" />
  187. <p>The <a class="reference internal" href="#module-concurrent.futures" title="concurrent.futures: Execute computations concurrently using threads or processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code></a> module provides a high-level interface for
  188. asynchronously executing callables.</p>
  189. <p>The asynchronous execution can be performed with threads, using
  190. <a class="reference internal" href="#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code></a>, or separate processes, using
  191. <a class="reference internal" href="#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a>. Both implement the same interface, which is
  192. defined by the abstract <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> class.</p>
  193. <div class="availability docutils container">
  194. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not Emscripten, not WASI.</p>
  195. <p>This module does not work or is not available on WebAssembly platforms
  196. <code class="docutils literal notranslate"><span class="pre">wasm32-emscripten</span></code> and <code class="docutils literal notranslate"><span class="pre">wasm32-wasi</span></code>. See
  197. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  198. </div>
  199. <section id="executor-objects">
  200. <h2>Executor Objects<a class="headerlink" href="#executor-objects" title="Link to this heading">¶</a></h2>
  201. <dl class="py class">
  202. <dt class="sig sig-object py" id="concurrent.futures.Executor">
  203. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">concurrent.futures.</span></span><span class="sig-name descname"><span class="pre">Executor</span></span><a class="headerlink" href="#concurrent.futures.Executor" title="Link to this definition">¶</a></dt>
  204. <dd><p>An abstract class that provides methods to execute calls asynchronously. It
  205. should not be used directly, but through its concrete subclasses.</p>
  206. <dl class="py method">
  207. <dt class="sig sig-object py" id="concurrent.futures.Executor.submit">
  208. <span class="sig-name descname"><span class="pre">submit</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fn</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Executor.submit" title="Link to this definition">¶</a></dt>
  209. <dd><p>Schedules the callable, <em>fn</em>, to be executed as <code class="docutils literal notranslate"><span class="pre">fn(*args,</span> <span class="pre">**kwargs)</span></code>
  210. and returns a <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> object representing the execution of the
  211. callable.</p>
  212. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">ThreadPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="k">as</span> <span class="n">executor</span><span class="p">:</span>
  213. <span class="n">future</span> <span class="o">=</span> <span class="n">executor</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="nb">pow</span><span class="p">,</span> <span class="mi">323</span><span class="p">,</span> <span class="mi">1235</span><span class="p">)</span>
  214. <span class="nb">print</span><span class="p">(</span><span class="n">future</span><span class="o">.</span><span class="n">result</span><span class="p">())</span>
  215. </pre></div>
  216. </div>
  217. </dd></dl>
  218. <dl class="py method">
  219. <dt class="sig sig-object py" id="concurrent.futures.Executor.map">
  220. <span class="sig-name descname"><span class="pre">map</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fn</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">iterables</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">chunksize</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Executor.map" title="Link to this definition">¶</a></dt>
  221. <dd><p>Similar to <a class="reference internal" href="functions.html#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map(fn,</span> <span class="pre">*iterables)</span></code></a> except:</p>
  222. <ul class="simple">
  223. <li><p>the <em>iterables</em> are collected immediately rather than lazily;</p></li>
  224. <li><p><em>fn</em> is executed asynchronously and several calls to
  225. <em>fn</em> may be made concurrently.</p></li>
  226. </ul>
  227. <p>The returned iterator raises a <a class="reference internal" href="exceptions.html#TimeoutError" title="TimeoutError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TimeoutError</span></code></a>
  228. if <a class="reference internal" href="stdtypes.html#iterator.__next__" title="iterator.__next__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code></a> is called and the result isn’t available
  229. after <em>timeout</em> seconds from the original call to <a class="reference internal" href="#concurrent.futures.Executor.map" title="concurrent.futures.Executor.map"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Executor.map()</span></code></a>.
  230. <em>timeout</em> can be an int or a float. If <em>timeout</em> is not specified or
  231. <code class="docutils literal notranslate"><span class="pre">None</span></code>, there is no limit to the wait time.</p>
  232. <p>If a <em>fn</em> call raises an exception, then that exception will be
  233. raised when its value is retrieved from the iterator.</p>
  234. <p>When using <a class="reference internal" href="#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a>, this method chops <em>iterables</em>
  235. into a number of chunks which it submits to the pool as separate
  236. tasks. The (approximate) size of these chunks can be specified by
  237. setting <em>chunksize</em> to a positive integer. For very long iterables,
  238. using a large value for <em>chunksize</em> can significantly improve
  239. performance compared to the default size of 1. With
  240. <a class="reference internal" href="#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code></a>, <em>chunksize</em> has no effect.</p>
  241. <div class="versionchanged">
  242. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added the <em>chunksize</em> argument.</p>
  243. </div>
  244. </dd></dl>
  245. <dl class="py method">
  246. <dt class="sig sig-object py" id="concurrent.futures.Executor.shutdown">
  247. <span class="sig-name descname"><span class="pre">shutdown</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">wait</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</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">cancel_futures</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Executor.shutdown" title="Link to this definition">¶</a></dt>
  248. <dd><p>Signal the executor that it should free any resources that it is using
  249. when the currently pending futures are done executing. Calls to
  250. <a class="reference internal" href="#concurrent.futures.Executor.submit" title="concurrent.futures.Executor.submit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Executor.submit()</span></code></a> and <a class="reference internal" href="#concurrent.futures.Executor.map" title="concurrent.futures.Executor.map"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Executor.map()</span></code></a> made after shutdown will
  251. raise <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>.</p>
  252. <p>If <em>wait</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> then this method will not return until all the
  253. pending futures are done executing and the resources associated with the
  254. executor have been freed. If <em>wait</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code> then this method will
  255. return immediately and the resources associated with the executor will be
  256. freed when all pending futures are done executing. Regardless of the
  257. value of <em>wait</em>, the entire Python program will not exit until all
  258. pending futures are done executing.</p>
  259. <p>If <em>cancel_futures</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, this method will cancel all pending
  260. futures that the executor has not started running. Any futures that
  261. are completed or running won’t be cancelled, regardless of the value
  262. of <em>cancel_futures</em>.</p>
  263. <p>If both <em>cancel_futures</em> and <em>wait</em> are <code class="docutils literal notranslate"><span class="pre">True</span></code>, all futures that the
  264. executor has started running will be completed prior to this method
  265. returning. The remaining futures are cancelled.</p>
  266. <p>You can avoid having to call this method explicitly if you use the
  267. <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement, which will shutdown the <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a>
  268. (waiting as if <a class="reference internal" href="#concurrent.futures.Executor.shutdown" title="concurrent.futures.Executor.shutdown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Executor.shutdown()</span></code></a> were called with <em>wait</em> set to
  269. <code class="docutils literal notranslate"><span class="pre">True</span></code>):</p>
  270. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">shutil</span>
  271. <span class="k">with</span> <span class="n">ThreadPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
  272. <span class="n">e</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">,</span> <span class="s1">&#39;src1.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;dest1.txt&#39;</span><span class="p">)</span>
  273. <span class="n">e</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">,</span> <span class="s1">&#39;src2.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;dest2.txt&#39;</span><span class="p">)</span>
  274. <span class="n">e</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">,</span> <span class="s1">&#39;src3.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;dest3.txt&#39;</span><span class="p">)</span>
  275. <span class="n">e</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">,</span> <span class="s1">&#39;src4.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;dest4.txt&#39;</span><span class="p">)</span>
  276. </pre></div>
  277. </div>
  278. <div class="versionchanged">
  279. <p><span class="versionmodified changed">Changed in version 3.9: </span>Added <em>cancel_futures</em>.</p>
  280. </div>
  281. </dd></dl>
  282. </dd></dl>
  283. </section>
  284. <section id="threadpoolexecutor">
  285. <h2>ThreadPoolExecutor<a class="headerlink" href="#threadpoolexecutor" title="Link to this heading">¶</a></h2>
  286. <p><a class="reference internal" href="#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code></a> is an <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> subclass that uses a pool of
  287. threads to execute calls asynchronously.</p>
  288. <p>Deadlocks can occur when the callable associated with a <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> waits on
  289. the results of another <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a>. For example:</p>
  290. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">time</span>
  291. <span class="k">def</span> <span class="nf">wait_on_b</span><span class="p">():</span>
  292. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
  293. <span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">result</span><span class="p">())</span> <span class="c1"># b will never complete because it is waiting on a.</span>
  294. <span class="k">return</span> <span class="mi">5</span>
  295. <span class="k">def</span> <span class="nf">wait_on_a</span><span class="p">():</span>
  296. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
  297. <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">result</span><span class="p">())</span> <span class="c1"># a will never complete because it is waiting on b.</span>
  298. <span class="k">return</span> <span class="mi">6</span>
  299. <span class="n">executor</span> <span class="o">=</span> <span class="n">ThreadPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
  300. <span class="n">a</span> <span class="o">=</span> <span class="n">executor</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">wait_on_b</span><span class="p">)</span>
  301. <span class="n">b</span> <span class="o">=</span> <span class="n">executor</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">wait_on_a</span><span class="p">)</span>
  302. </pre></div>
  303. </div>
  304. <p>And:</p>
  305. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">wait_on_future</span><span class="p">():</span>
  306. <span class="n">f</span> <span class="o">=</span> <span class="n">executor</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="nb">pow</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
  307. <span class="c1"># This will never complete because there is only one worker thread and</span>
  308. <span class="c1"># it is executing this function.</span>
  309. <span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">result</span><span class="p">())</span>
  310. <span class="n">executor</span> <span class="o">=</span> <span class="n">ThreadPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
  311. <span class="n">executor</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">wait_on_future</span><span class="p">)</span>
  312. </pre></div>
  313. </div>
  314. <dl class="py class">
  315. <dt class="sig sig-object py" id="concurrent.futures.ThreadPoolExecutor">
  316. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">concurrent.futures.</span></span><span class="sig-name descname"><span class="pre">ThreadPoolExecutor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">max_workers</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">thread_name_prefix</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">initializer</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">initargs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.ThreadPoolExecutor" title="Link to this definition">¶</a></dt>
  317. <dd><p>An <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> subclass that uses a pool of at most <em>max_workers</em>
  318. threads to execute calls asynchronously.</p>
  319. <p>All threads enqueued to <code class="docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code> will be joined before the
  320. interpreter can exit. Note that the exit handler which does this is
  321. executed <em>before</em> any exit handlers added using <code class="docutils literal notranslate"><span class="pre">atexit</span></code>. This means
  322. exceptions in the main thread must be caught and handled in order to
  323. signal threads to exit gracefully. For this reason, it is recommended
  324. that <code class="docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code> not be used for long-running tasks.</p>
  325. <p><em>initializer</em> is an optional callable that is called at the start of
  326. each worker thread; <em>initargs</em> is a tuple of arguments passed to the
  327. initializer. Should <em>initializer</em> raise an exception, all currently
  328. pending jobs will raise a <a class="reference internal" href="#concurrent.futures.thread.BrokenThreadPool" title="concurrent.futures.thread.BrokenThreadPool"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BrokenThreadPool</span></code></a>,
  329. as well as any attempt to submit more jobs to the pool.</p>
  330. <div class="versionchanged">
  331. <p><span class="versionmodified changed">Changed in version 3.5: </span>If <em>max_workers</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> or
  332. not given, it will default to the number of processors on the machine,
  333. multiplied by <code class="docutils literal notranslate"><span class="pre">5</span></code>, assuming that <a class="reference internal" href="#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code></a> is often
  334. used to overlap I/O instead of CPU work and the number of workers
  335. should be higher than the number of workers
  336. for <a class="reference internal" href="#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a>.</p>
  337. </div>
  338. <div class="versionchanged">
  339. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added the <em>thread_name_prefix</em> parameter to allow users to
  340. control the <a class="reference internal" href="threading.html#threading.Thread" title="threading.Thread"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Thread</span></code></a> names for worker threads created by
  341. the pool for easier debugging.</p>
  342. </div>
  343. <div class="versionchanged">
  344. <p><span class="versionmodified changed">Changed in version 3.7: </span>Added the <em>initializer</em> and <em>initargs</em> arguments.</p>
  345. </div>
  346. <div class="versionchanged">
  347. <p><span class="versionmodified changed">Changed in version 3.8: </span>Default value of <em>max_workers</em> is changed to <code class="docutils literal notranslate"><span class="pre">min(32,</span> <span class="pre">os.cpu_count()</span> <span class="pre">+</span> <span class="pre">4)</span></code>.
  348. This default value preserves at least 5 workers for I/O bound tasks.
  349. It utilizes at most 32 CPU cores for CPU bound tasks which release the GIL.
  350. And it avoids using very large resources implicitly on many-core machines.</p>
  351. <p>ThreadPoolExecutor now reuses idle worker threads before starting
  352. <em>max_workers</em> worker threads too.</p>
  353. </div>
  354. </dd></dl>
  355. <section id="threadpoolexecutor-example">
  356. <span id="id1"></span><h3>ThreadPoolExecutor Example<a class="headerlink" href="#threadpoolexecutor-example" title="Link to this heading">¶</a></h3>
  357. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">concurrent.futures</span>
  358. <span class="kn">import</span> <span class="nn">urllib.request</span>
  359. <span class="n">URLS</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;http://www.foxnews.com/&#39;</span><span class="p">,</span>
  360. <span class="s1">&#39;http://www.cnn.com/&#39;</span><span class="p">,</span>
  361. <span class="s1">&#39;http://europe.wsj.com/&#39;</span><span class="p">,</span>
  362. <span class="s1">&#39;http://www.bbc.co.uk/&#39;</span><span class="p">,</span>
  363. <span class="s1">&#39;http://nonexistant-subdomain.python.org/&#39;</span><span class="p">]</span>
  364. <span class="c1"># Retrieve a single page and report the URL and contents</span>
  365. <span class="k">def</span> <span class="nf">load_url</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">timeout</span><span class="p">):</span>
  366. <span class="k">with</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="n">timeout</span><span class="p">)</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
  367. <span class="k">return</span> <span class="n">conn</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  368. <span class="c1"># We can use a with statement to ensure threads are cleaned up promptly</span>
  369. <span class="k">with</span> <span class="n">concurrent</span><span class="o">.</span><span class="n">futures</span><span class="o">.</span><span class="n">ThreadPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span> <span class="k">as</span> <span class="n">executor</span><span class="p">:</span>
  370. <span class="c1"># Start the load operations and mark each future with its URL</span>
  371. <span class="n">future_to_url</span> <span class="o">=</span> <span class="p">{</span><span class="n">executor</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">load_url</span><span class="p">,</span> <span class="n">url</span><span class="p">,</span> <span class="mi">60</span><span class="p">):</span> <span class="n">url</span> <span class="k">for</span> <span class="n">url</span> <span class="ow">in</span> <span class="n">URLS</span><span class="p">}</span>
  372. <span class="k">for</span> <span class="n">future</span> <span class="ow">in</span> <span class="n">concurrent</span><span class="o">.</span><span class="n">futures</span><span class="o">.</span><span class="n">as_completed</span><span class="p">(</span><span class="n">future_to_url</span><span class="p">):</span>
  373. <span class="n">url</span> <span class="o">=</span> <span class="n">future_to_url</span><span class="p">[</span><span class="n">future</span><span class="p">]</span>
  374. <span class="k">try</span><span class="p">:</span>
  375. <span class="n">data</span> <span class="o">=</span> <span class="n">future</span><span class="o">.</span><span class="n">result</span><span class="p">()</span>
  376. <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
  377. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%r</span><span class="s1"> generated an exception: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">exc</span><span class="p">))</span>
  378. <span class="k">else</span><span class="p">:</span>
  379. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%r</span><span class="s1"> page is </span><span class="si">%d</span><span class="s1"> bytes&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)))</span>
  380. </pre></div>
  381. </div>
  382. </section>
  383. </section>
  384. <section id="processpoolexecutor">
  385. <h2>ProcessPoolExecutor<a class="headerlink" href="#processpoolexecutor" title="Link to this heading">¶</a></h2>
  386. <p>The <a class="reference internal" href="#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a> class is an <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> subclass that
  387. uses a pool of processes to execute calls asynchronously.
  388. <a class="reference internal" href="#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a> uses the <a class="reference internal" href="multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> module, which
  389. allows it to side-step the <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><span class="xref std std-term">Global Interpreter Lock</span></a> but also means that
  390. only picklable objects can be executed and returned.</p>
  391. <p>The <code class="docutils literal notranslate"><span class="pre">__main__</span></code> module must be importable by worker subprocesses. This means
  392. that <a class="reference internal" href="#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a> will not work in the interactive interpreter.</p>
  393. <p>Calling <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> or <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> methods from a callable submitted
  394. to a <a class="reference internal" href="#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a> will result in deadlock.</p>
  395. <dl class="py class">
  396. <dt class="sig sig-object py" id="concurrent.futures.ProcessPoolExecutor">
  397. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">concurrent.futures.</span></span><span class="sig-name descname"><span class="pre">ProcessPoolExecutor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">max_workers</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mp_context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">initializer</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">initargs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">max_tasks_per_child</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="#concurrent.futures.ProcessPoolExecutor" title="Link to this definition">¶</a></dt>
  398. <dd><p>An <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> subclass that executes calls asynchronously using a pool
  399. of at most <em>max_workers</em> processes. If <em>max_workers</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> or not
  400. given, it will default to the number of processors on the machine.
  401. If <em>max_workers</em> is less than or equal to <code class="docutils literal notranslate"><span class="pre">0</span></code>, then a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>
  402. will be raised.
  403. On Windows, <em>max_workers</em> must be less than or equal to <code class="docutils literal notranslate"><span class="pre">61</span></code>. If it is not
  404. then <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> will be raised. If <em>max_workers</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, then
  405. the default chosen will be at most <code class="docutils literal notranslate"><span class="pre">61</span></code>, even if more processors are
  406. available.
  407. <em>mp_context</em> can be a <a class="reference internal" href="multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> context or <code class="docutils literal notranslate"><span class="pre">None</span></code>. It will be
  408. used to launch the workers. If <em>mp_context</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> or not given, the
  409. default <a class="reference internal" href="multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> context is used.
  410. See <a class="reference internal" href="multiprocessing.html#multiprocessing-start-methods"><span class="std std-ref">Contexts and start methods</span></a>.</p>
  411. <p><em>initializer</em> is an optional callable that is called at the start of
  412. each worker process; <em>initargs</em> is a tuple of arguments passed to the
  413. initializer. Should <em>initializer</em> raise an exception, all currently
  414. pending jobs will raise a <a class="reference internal" href="#concurrent.futures.process.BrokenProcessPool" title="concurrent.futures.process.BrokenProcessPool"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BrokenProcessPool</span></code></a>,
  415. as well as any attempt to submit more jobs to the pool.</p>
  416. <p><em>max_tasks_per_child</em> is an optional argument that specifies the maximum
  417. number of tasks a single process can execute before it will exit and be
  418. replaced with a fresh worker process. By default <em>max_tasks_per_child</em> is
  419. <code class="docutils literal notranslate"><span class="pre">None</span></code> which means worker processes will live as long as the pool. When
  420. a max is specified, the “spawn” multiprocessing start method will be used by
  421. default in absence of a <em>mp_context</em> parameter. This feature is incompatible
  422. with the “fork” start method.</p>
  423. <div class="versionchanged">
  424. <p><span class="versionmodified changed">Changed in version 3.3: </span>When one of the worker processes terminates abruptly, a
  425. <a class="reference internal" href="#concurrent.futures.process.BrokenProcessPool" title="concurrent.futures.process.BrokenProcessPool"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BrokenProcessPool</span></code></a> error is now raised.
  426. Previously, behaviour
  427. was undefined but operations on the executor or its futures would often
  428. freeze or deadlock.</p>
  429. </div>
  430. <div class="versionchanged">
  431. <p><span class="versionmodified changed">Changed in version 3.7: </span>The <em>mp_context</em> argument was added to allow users to control the
  432. start_method for worker processes created by the pool.</p>
  433. <p>Added the <em>initializer</em> and <em>initargs</em> arguments.</p>
  434. <div class="admonition note">
  435. <p class="admonition-title">Note</p>
  436. <p>The default <a class="reference internal" href="multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> start method
  437. (see <a class="reference internal" href="multiprocessing.html#multiprocessing-start-methods"><span class="std std-ref">Contexts and start methods</span></a>) will change away from
  438. <em>fork</em> in Python 3.14. Code that requires <em>fork</em> be used for their
  439. <a class="reference internal" href="#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a> should explicitly specify that by
  440. passing a <code class="docutils literal notranslate"><span class="pre">mp_context=multiprocessing.get_context(&quot;fork&quot;)</span></code>
  441. parameter.</p>
  442. </div>
  443. </div>
  444. <div class="versionchanged">
  445. <p><span class="versionmodified changed">Changed in version 3.11: </span>The <em>max_tasks_per_child</em> argument was added to allow users to
  446. control the lifetime of workers in the pool.</p>
  447. </div>
  448. <div class="versionchanged">
  449. <p><span class="versionmodified changed">Changed in version 3.12: </span>On POSIX systems, if your application has multiple threads and the
  450. <a class="reference internal" href="multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> context uses the <code class="docutils literal notranslate"><span class="pre">&quot;fork&quot;</span></code> start method:
  451. The <a class="reference internal" href="os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a> function called internally to spawn workers may raise a
  452. <a class="reference internal" href="exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a>. Pass a <em>mp_context</em> configured to use a
  453. different start method. See the <a class="reference internal" href="os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a> documentation for
  454. further explanation.</p>
  455. </div>
  456. </dd></dl>
  457. <section id="processpoolexecutor-example">
  458. <span id="id2"></span><h3>ProcessPoolExecutor Example<a class="headerlink" href="#processpoolexecutor-example" title="Link to this heading">¶</a></h3>
  459. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">concurrent.futures</span>
  460. <span class="kn">import</span> <span class="nn">math</span>
  461. <span class="n">PRIMES</span> <span class="o">=</span> <span class="p">[</span>
  462. <span class="mi">112272535095293</span><span class="p">,</span>
  463. <span class="mi">112582705942171</span><span class="p">,</span>
  464. <span class="mi">112272535095293</span><span class="p">,</span>
  465. <span class="mi">115280095190773</span><span class="p">,</span>
  466. <span class="mi">115797848077099</span><span class="p">,</span>
  467. <span class="mi">1099726899285419</span><span class="p">]</span>
  468. <span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
  469. <span class="k">if</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
  470. <span class="k">return</span> <span class="kc">False</span>
  471. <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
  472. <span class="k">return</span> <span class="kc">True</span>
  473. <span class="k">if</span> <span class="n">n</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
  474. <span class="k">return</span> <span class="kc">False</span>
  475. <span class="n">sqrt_n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">n</span><span class="p">)))</span>
  476. <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">sqrt_n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span>
  477. <span class="k">if</span> <span class="n">n</span> <span class="o">%</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
  478. <span class="k">return</span> <span class="kc">False</span>
  479. <span class="k">return</span> <span class="kc">True</span>
  480. <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
  481. <span class="k">with</span> <span class="n">concurrent</span><span class="o">.</span><span class="n">futures</span><span class="o">.</span><span class="n">ProcessPoolExecutor</span><span class="p">()</span> <span class="k">as</span> <span class="n">executor</span><span class="p">:</span>
  482. <span class="k">for</span> <span class="n">number</span><span class="p">,</span> <span class="n">prime</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">PRIMES</span><span class="p">,</span> <span class="n">executor</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">is_prime</span><span class="p">,</span> <span class="n">PRIMES</span><span class="p">)):</span>
  483. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%d</span><span class="s1"> is prime: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">number</span><span class="p">,</span> <span class="n">prime</span><span class="p">))</span>
  484. <span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
  485. <span class="n">main</span><span class="p">()</span>
  486. </pre></div>
  487. </div>
  488. </section>
  489. </section>
  490. <section id="future-objects">
  491. <h2>Future Objects<a class="headerlink" href="#future-objects" title="Link to this heading">¶</a></h2>
  492. <p>The <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> class encapsulates the asynchronous execution of a callable.
  493. <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> instances are created by <a class="reference internal" href="#concurrent.futures.Executor.submit" title="concurrent.futures.Executor.submit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Executor.submit()</span></code></a>.</p>
  494. <dl class="py class">
  495. <dt class="sig sig-object py" id="concurrent.futures.Future">
  496. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">concurrent.futures.</span></span><span class="sig-name descname"><span class="pre">Future</span></span><a class="headerlink" href="#concurrent.futures.Future" title="Link to this definition">¶</a></dt>
  497. <dd><p>Encapsulates the asynchronous execution of a callable. <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a>
  498. instances are created by <a class="reference internal" href="#concurrent.futures.Executor.submit" title="concurrent.futures.Executor.submit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Executor.submit()</span></code></a> and should not be created
  499. directly except for testing.</p>
  500. <dl class="py method">
  501. <dt class="sig sig-object py" id="concurrent.futures.Future.cancel">
  502. <span class="sig-name descname"><span class="pre">cancel</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Future.cancel" title="Link to this definition">¶</a></dt>
  503. <dd><p>Attempt to cancel the call. If the call is currently being executed or
  504. finished running and cannot be cancelled then the method will return
  505. <code class="docutils literal notranslate"><span class="pre">False</span></code>, otherwise the call will be cancelled and the method will
  506. return <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  507. </dd></dl>
  508. <dl class="py method">
  509. <dt class="sig sig-object py" id="concurrent.futures.Future.cancelled">
  510. <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="#concurrent.futures.Future.cancelled" title="Link to this definition">¶</a></dt>
  511. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the call was successfully cancelled.</p>
  512. </dd></dl>
  513. <dl class="py method">
  514. <dt class="sig sig-object py" id="concurrent.futures.Future.running">
  515. <span class="sig-name descname"><span class="pre">running</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Future.running" title="Link to this definition">¶</a></dt>
  516. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the call is currently being executed and cannot be
  517. cancelled.</p>
  518. </dd></dl>
  519. <dl class="py method">
  520. <dt class="sig sig-object py" id="concurrent.futures.Future.done">
  521. <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="#concurrent.futures.Future.done" title="Link to this definition">¶</a></dt>
  522. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the call was successfully cancelled or finished
  523. running.</p>
  524. </dd></dl>
  525. <dl class="py method">
  526. <dt class="sig sig-object py" id="concurrent.futures.Future.result">
  527. <span class="sig-name descname"><span class="pre">result</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">timeout</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="#concurrent.futures.Future.result" title="Link to this definition">¶</a></dt>
  528. <dd><p>Return the value returned by the call. If the call hasn’t yet completed
  529. then this method will wait up to <em>timeout</em> seconds. If the call hasn’t
  530. completed in <em>timeout</em> seconds, then a
  531. <a class="reference internal" href="exceptions.html#TimeoutError" title="TimeoutError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TimeoutError</span></code></a> will be raised. <em>timeout</em> can be
  532. an int or float. If <em>timeout</em> is not specified or <code class="docutils literal notranslate"><span class="pre">None</span></code>, there is no
  533. limit to the wait time.</p>
  534. <p>If the future is cancelled before completing then <a class="reference internal" href="#concurrent.futures.CancelledError" title="concurrent.futures.CancelledError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">CancelledError</span></code></a>
  535. will be raised.</p>
  536. <p>If the call raised an exception, this method will raise the same exception.</p>
  537. </dd></dl>
  538. <dl class="py method">
  539. <dt class="sig sig-object py" id="concurrent.futures.Future.exception">
  540. <span class="sig-name descname"><span class="pre">exception</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">timeout</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="#concurrent.futures.Future.exception" title="Link to this definition">¶</a></dt>
  541. <dd><p>Return the exception raised by the call. If the call hasn’t yet
  542. completed then this method will wait up to <em>timeout</em> seconds. If the
  543. call hasn’t completed in <em>timeout</em> seconds, then a
  544. <a class="reference internal" href="exceptions.html#TimeoutError" title="TimeoutError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TimeoutError</span></code></a> will be raised. <em>timeout</em> can be
  545. an int or float. If <em>timeout</em> is not specified or <code class="docutils literal notranslate"><span class="pre">None</span></code>, there is no
  546. limit to the wait time.</p>
  547. <p>If the future is cancelled before completing then <a class="reference internal" href="#concurrent.futures.CancelledError" title="concurrent.futures.CancelledError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">CancelledError</span></code></a>
  548. will be raised.</p>
  549. <p>If the call completed without raising, <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned.</p>
  550. </dd></dl>
  551. <dl class="py method">
  552. <dt class="sig sig-object py" id="concurrent.futures.Future.add_done_callback">
  553. <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">fn</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Future.add_done_callback" title="Link to this definition">¶</a></dt>
  554. <dd><p>Attaches the callable <em>fn</em> to the future. <em>fn</em> will be called, with the
  555. future as its only argument, when the future is cancelled or finishes
  556. running.</p>
  557. <p>Added callables are called in the order that they were added and are
  558. always called in a thread belonging to the process that added them. If
  559. the callable raises an <a class="reference internal" href="exceptions.html#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a> subclass, it will be logged and
  560. ignored. If the callable raises a <a class="reference internal" href="exceptions.html#BaseException" title="BaseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseException</span></code></a> subclass, the
  561. behavior is undefined.</p>
  562. <p>If the future has already completed or been cancelled, <em>fn</em> will be
  563. called immediately.</p>
  564. </dd></dl>
  565. <p>The following <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> methods are meant for use in unit tests and
  566. <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> implementations.</p>
  567. <dl class="py method">
  568. <dt class="sig sig-object py" id="concurrent.futures.Future.set_running_or_notify_cancel">
  569. <span class="sig-name descname"><span class="pre">set_running_or_notify_cancel</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.Future.set_running_or_notify_cancel" title="Link to this definition">¶</a></dt>
  570. <dd><p>This method should only be called by <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> implementations
  571. before executing the work associated with the <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> and by unit
  572. tests.</p>
  573. <p>If the method returns <code class="docutils literal notranslate"><span class="pre">False</span></code> then the <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> was cancelled,
  574. i.e. <a class="reference internal" href="#concurrent.futures.Future.cancel" title="concurrent.futures.Future.cancel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Future.cancel()</span></code></a> was called and returned <code class="docutils literal notranslate"><span class="pre">True</span></code>. Any threads
  575. waiting on the <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> completing (i.e. through
  576. <a class="reference internal" href="#concurrent.futures.as_completed" title="concurrent.futures.as_completed"><code class="xref py py-func docutils literal notranslate"><span class="pre">as_completed()</span></code></a> or <a class="reference internal" href="#concurrent.futures.wait" title="concurrent.futures.wait"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait()</span></code></a>) will be woken up.</p>
  577. <p>If the method returns <code class="docutils literal notranslate"><span class="pre">True</span></code> then the <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> was not cancelled
  578. and has been put in the running state, i.e. calls to
  579. <a class="reference internal" href="#concurrent.futures.Future.running" title="concurrent.futures.Future.running"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Future.running()</span></code></a> will return <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  580. <p>This method can only be called once and cannot be called after
  581. <a class="reference internal" href="#concurrent.futures.Future.set_result" title="concurrent.futures.Future.set_result"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Future.set_result()</span></code></a> or <a class="reference internal" href="#concurrent.futures.Future.set_exception" title="concurrent.futures.Future.set_exception"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Future.set_exception()</span></code></a> have been
  582. called.</p>
  583. </dd></dl>
  584. <dl class="py method">
  585. <dt class="sig sig-object py" id="concurrent.futures.Future.set_result">
  586. <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="#concurrent.futures.Future.set_result" title="Link to this definition">¶</a></dt>
  587. <dd><p>Sets the result of the work associated with the <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> to
  588. <em>result</em>.</p>
  589. <p>This method should only be used by <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> implementations and
  590. unit tests.</p>
  591. <div class="versionchanged">
  592. <p><span class="versionmodified changed">Changed in version 3.8: </span>This method raises
  593. <a class="reference internal" href="#concurrent.futures.InvalidStateError" title="concurrent.futures.InvalidStateError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">concurrent.futures.InvalidStateError</span></code></a> if the <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> is
  594. already done.</p>
  595. </div>
  596. </dd></dl>
  597. <dl class="py method">
  598. <dt class="sig sig-object py" id="concurrent.futures.Future.set_exception">
  599. <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="#concurrent.futures.Future.set_exception" title="Link to this definition">¶</a></dt>
  600. <dd><p>Sets the result of the work associated with the <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> to the
  601. <a class="reference internal" href="exceptions.html#Exception" title="Exception"><code class="xref py py-class docutils literal notranslate"><span class="pre">Exception</span></code></a> <em>exception</em>.</p>
  602. <p>This method should only be used by <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> implementations and
  603. unit tests.</p>
  604. <div class="versionchanged">
  605. <p><span class="versionmodified changed">Changed in version 3.8: </span>This method raises
  606. <a class="reference internal" href="#concurrent.futures.InvalidStateError" title="concurrent.futures.InvalidStateError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">concurrent.futures.InvalidStateError</span></code></a> if the <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> is
  607. already done.</p>
  608. </div>
  609. </dd></dl>
  610. </dd></dl>
  611. </section>
  612. <section id="module-functions">
  613. <h2>Module Functions<a class="headerlink" href="#module-functions" title="Link to this heading">¶</a></h2>
  614. <dl class="py function">
  615. <dt class="sig sig-object py" id="concurrent.futures.wait">
  616. <span class="sig-prename descclassname"><span class="pre">concurrent.futures.</span></span><span class="sig-name descname"><span class="pre">wait</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">return_when</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">ALL_COMPLETED</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#concurrent.futures.wait" title="Link to this definition">¶</a></dt>
  617. <dd><p>Wait for the <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> instances (possibly created by different
  618. <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> instances) given by <em>fs</em> to complete. Duplicate futures
  619. given to <em>fs</em> are removed and will be returned only once. Returns a named
  620. 2-tuple of sets. The first set, named <code class="docutils literal notranslate"><span class="pre">done</span></code>, contains the futures that
  621. completed (finished or cancelled futures) before the wait completed. The
  622. second set, named <code class="docutils literal notranslate"><span class="pre">not_done</span></code>, contains the futures that did not complete
  623. (pending or running futures).</p>
  624. <p><em>timeout</em> can be used to control the maximum number of seconds to wait before
  625. returning. <em>timeout</em> can be an int or float. If <em>timeout</em> is not specified
  626. or <code class="docutils literal notranslate"><span class="pre">None</span></code>, there is no limit to the wait time.</p>
  627. <p><em>return_when</em> indicates when this function should return. It must be one of
  628. the following constants:</p>
  629. <table class="docutils align-default">
  630. <thead>
  631. <tr class="row-odd"><th class="head"><p>Constant</p></th>
  632. <th class="head"><p>Description</p></th>
  633. </tr>
  634. </thead>
  635. <tbody>
  636. <tr class="row-even"><td><dl class="py data">
  637. <dt class="sig sig-object py" id="concurrent.futures.FIRST_COMPLETED">
  638. <span class="sig-prename descclassname"><span class="pre">concurrent.futures.</span></span><span class="sig-name descname"><span class="pre">FIRST_COMPLETED</span></span><a class="headerlink" href="#concurrent.futures.FIRST_COMPLETED" title="Link to this definition">¶</a></dt>
  639. <dd></dd></dl>
  640. </td>
  641. <td><p>The function will return when any future finishes or is cancelled.</p></td>
  642. </tr>
  643. <tr class="row-odd"><td><dl class="py data">
  644. <dt class="sig sig-object py" id="concurrent.futures.FIRST_EXCEPTION">
  645. <span class="sig-prename descclassname"><span class="pre">concurrent.futures.</span></span><span class="sig-name descname"><span class="pre">FIRST_EXCEPTION</span></span><a class="headerlink" href="#concurrent.futures.FIRST_EXCEPTION" title="Link to this definition">¶</a></dt>
  646. <dd></dd></dl>
  647. </td>
  648. <td><p>The function will return when any future finishes by raising an
  649. exception. If no future raises an exception
  650. then it is equivalent to <a class="reference internal" href="#concurrent.futures.ALL_COMPLETED" title="concurrent.futures.ALL_COMPLETED"><code class="xref py py-const docutils literal notranslate"><span class="pre">ALL_COMPLETED</span></code></a>.</p></td>
  651. </tr>
  652. <tr class="row-even"><td><dl class="py data">
  653. <dt class="sig sig-object py" id="concurrent.futures.ALL_COMPLETED">
  654. <span class="sig-prename descclassname"><span class="pre">concurrent.futures.</span></span><span class="sig-name descname"><span class="pre">ALL_COMPLETED</span></span><a class="headerlink" href="#concurrent.futures.ALL_COMPLETED" title="Link to this definition">¶</a></dt>
  655. <dd></dd></dl>
  656. </td>
  657. <td><p>The function will return when all futures finish or are cancelled.</p></td>
  658. </tr>
  659. </tbody>
  660. </table>
  661. </dd></dl>
  662. <dl class="py function">
  663. <dt class="sig sig-object py" id="concurrent.futures.as_completed">
  664. <span class="sig-prename descclassname"><span class="pre">concurrent.futures.</span></span><span class="sig-name descname"><span class="pre">as_completed</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">timeout</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="#concurrent.futures.as_completed" title="Link to this definition">¶</a></dt>
  665. <dd><p>Returns an iterator over the <a class="reference internal" href="#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> instances (possibly created by
  666. different <a class="reference internal" href="#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> instances) given by <em>fs</em> that yields futures as
  667. they complete (finished or cancelled futures). Any futures given by <em>fs</em> that
  668. are duplicated will be returned once. Any futures that completed before
  669. <a class="reference internal" href="#concurrent.futures.as_completed" title="concurrent.futures.as_completed"><code class="xref py py-func docutils literal notranslate"><span class="pre">as_completed()</span></code></a> is called will be yielded first. The returned iterator
  670. raises a <a class="reference internal" href="exceptions.html#TimeoutError" title="TimeoutError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TimeoutError</span></code></a> if <a class="reference internal" href="stdtypes.html#iterator.__next__" title="iterator.__next__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code></a>
  671. is called and the result isn’t available after <em>timeout</em> seconds from the
  672. original call to <a class="reference internal" href="#concurrent.futures.as_completed" title="concurrent.futures.as_completed"><code class="xref py py-func docutils literal notranslate"><span class="pre">as_completed()</span></code></a>. <em>timeout</em> can be an int or float. If
  673. <em>timeout</em> is not specified or <code class="docutils literal notranslate"><span class="pre">None</span></code>, there is no limit to the wait time.</p>
  674. </dd></dl>
  675. <div class="admonition seealso">
  676. <p class="admonition-title">See also</p>
  677. <dl class="simple">
  678. <dt><span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-3148/"><strong>PEP 3148</strong></a> – futures - execute computations asynchronously</dt><dd><p>The proposal which described this feature for inclusion in the Python
  679. standard library.</p>
  680. </dd>
  681. </dl>
  682. </div>
  683. </section>
  684. <section id="exception-classes">
  685. <h2>Exception classes<a class="headerlink" href="#exception-classes" title="Link to this heading">¶</a></h2>
  686. <dl class="py exception">
  687. <dt class="sig sig-object py" id="concurrent.futures.CancelledError">
  688. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">concurrent.futures.</span></span><span class="sig-name descname"><span class="pre">CancelledError</span></span><a class="headerlink" href="#concurrent.futures.CancelledError" title="Link to this definition">¶</a></dt>
  689. <dd><p>Raised when a future is cancelled.</p>
  690. </dd></dl>
  691. <dl class="py exception">
  692. <dt class="sig sig-object py" id="concurrent.futures.TimeoutError">
  693. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">concurrent.futures.</span></span><span class="sig-name descname"><span class="pre">TimeoutError</span></span><a class="headerlink" href="#concurrent.futures.TimeoutError" title="Link to this definition">¶</a></dt>
  694. <dd><p>A deprecated alias of <a class="reference internal" href="exceptions.html#TimeoutError" title="TimeoutError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TimeoutError</span></code></a>,
  695. raised when a future operation exceeds the given timeout.</p>
  696. <div class="versionchanged">
  697. <p><span class="versionmodified changed">Changed in version 3.11: </span>This class was made an alias of <a class="reference internal" href="exceptions.html#TimeoutError" title="TimeoutError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TimeoutError</span></code></a>.</p>
  698. </div>
  699. </dd></dl>
  700. <dl class="py exception">
  701. <dt class="sig sig-object py" id="concurrent.futures.BrokenExecutor">
  702. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">concurrent.futures.</span></span><span class="sig-name descname"><span class="pre">BrokenExecutor</span></span><a class="headerlink" href="#concurrent.futures.BrokenExecutor" title="Link to this definition">¶</a></dt>
  703. <dd><p>Derived from <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>, this exception class is raised
  704. when an executor is broken for some reason, and cannot be used
  705. to submit or execute new tasks.</p>
  706. <div class="versionadded">
  707. <p><span class="versionmodified added">New in version 3.7.</span></p>
  708. </div>
  709. </dd></dl>
  710. <dl class="py exception">
  711. <dt class="sig sig-object py" id="concurrent.futures.InvalidStateError">
  712. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">concurrent.futures.</span></span><span class="sig-name descname"><span class="pre">InvalidStateError</span></span><a class="headerlink" href="#concurrent.futures.InvalidStateError" title="Link to this definition">¶</a></dt>
  713. <dd><p>Raised when an operation is performed on a future that is not allowed
  714. in the current state.</p>
  715. <div class="versionadded">
  716. <p><span class="versionmodified added">New in version 3.8.</span></p>
  717. </div>
  718. </dd></dl>
  719. <dl class="py exception">
  720. <dt class="sig sig-object py" id="concurrent.futures.thread.BrokenThreadPool">
  721. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">concurrent.futures.thread.</span></span><span class="sig-name descname"><span class="pre">BrokenThreadPool</span></span><a class="headerlink" href="#concurrent.futures.thread.BrokenThreadPool" title="Link to this definition">¶</a></dt>
  722. <dd><p>Derived from <a class="reference internal" href="#concurrent.futures.BrokenExecutor" title="concurrent.futures.BrokenExecutor"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BrokenExecutor</span></code></a>, this exception
  723. class is raised when one of the workers
  724. of a <a class="reference internal" href="#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code></a>
  725. has failed initializing.</p>
  726. <div class="versionadded">
  727. <p><span class="versionmodified added">New in version 3.7.</span></p>
  728. </div>
  729. </dd></dl>
  730. <dl class="py exception">
  731. <dt class="sig sig-object py" id="concurrent.futures.process.BrokenProcessPool">
  732. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">concurrent.futures.process.</span></span><span class="sig-name descname"><span class="pre">BrokenProcessPool</span></span><a class="headerlink" href="#concurrent.futures.process.BrokenProcessPool" title="Link to this definition">¶</a></dt>
  733. <dd><p>Derived from <a class="reference internal" href="#concurrent.futures.BrokenExecutor" title="concurrent.futures.BrokenExecutor"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BrokenExecutor</span></code></a> (formerly
  734. <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>), this exception class is raised when one of the
  735. workers of a <a class="reference internal" href="#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a>
  736. has terminated in a non-clean
  737. fashion (for example, if it was killed from the outside).</p>
  738. <div class="versionadded">
  739. <p><span class="versionmodified added">New in version 3.3.</span></p>
  740. </div>
  741. </dd></dl>
  742. </section>
  743. </section>
  744. <div class="clearer"></div>
  745. </div>
  746. </div>
  747. </div>
  748. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  749. <div class="sphinxsidebarwrapper">
  750. <div>
  751. <h3><a href="../contents.html">Table of Contents</a></h3>
  752. <ul>
  753. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code> — Launching parallel tasks</a><ul>
  754. <li><a class="reference internal" href="#executor-objects">Executor Objects</a></li>
  755. <li><a class="reference internal" href="#threadpoolexecutor">ThreadPoolExecutor</a><ul>
  756. <li><a class="reference internal" href="#threadpoolexecutor-example">ThreadPoolExecutor Example</a></li>
  757. </ul>
  758. </li>
  759. <li><a class="reference internal" href="#processpoolexecutor">ProcessPoolExecutor</a><ul>
  760. <li><a class="reference internal" href="#processpoolexecutor-example">ProcessPoolExecutor Example</a></li>
  761. </ul>
  762. </li>
  763. <li><a class="reference internal" href="#future-objects">Future Objects</a></li>
  764. <li><a class="reference internal" href="#module-functions">Module Functions</a></li>
  765. <li><a class="reference internal" href="#exception-classes">Exception classes</a></li>
  766. </ul>
  767. </li>
  768. </ul>
  769. </div>
  770. <div>
  771. <h4>Previous topic</h4>
  772. <p class="topless"><a href="concurrent.html"
  773. title="previous chapter">The <code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent</span></code> package</a></p>
  774. </div>
  775. <div>
  776. <h4>Next topic</h4>
  777. <p class="topless"><a href="subprocess.html"
  778. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> — Subprocess management</a></p>
  779. </div>
  780. <div role="note" aria-label="source link">
  781. <h3>This Page</h3>
  782. <ul class="this-page-menu">
  783. <li><a href="../bugs.html">Report a Bug</a></li>
  784. <li>
  785. <a href="https://github.com/python/cpython/blob/main/Doc/library/concurrent.futures.rst"
  786. rel="nofollow">Show Source
  787. </a>
  788. </li>
  789. </ul>
  790. </div>
  791. </div>
  792. <div id="sidebarbutton" title="Collapse sidebar">
  793. <span>«</span>
  794. </div>
  795. </div>
  796. <div class="clearer"></div>
  797. </div>
  798. <div class="related" role="navigation" aria-label="related navigation">
  799. <h3>Navigation</h3>
  800. <ul>
  801. <li class="right" style="margin-right: 10px">
  802. <a href="../genindex.html" title="General Index"
  803. >index</a></li>
  804. <li class="right" >
  805. <a href="../py-modindex.html" title="Python Module Index"
  806. >modules</a> |</li>
  807. <li class="right" >
  808. <a href="subprocess.html" title="subprocess — Subprocess management"
  809. >next</a> |</li>
  810. <li class="right" >
  811. <a href="concurrent.html" title="The concurrent package"
  812. >previous</a> |</li>
  813. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  814. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  815. <li class="switchers">
  816. <div class="language_switcher_placeholder"></div>
  817. <div class="version_switcher_placeholder"></div>
  818. </li>
  819. <li>
  820. </li>
  821. <li id="cpython-language-and-version">
  822. <a href="../index.html">3.12.3 Documentation</a> &#187;
  823. </li>
  824. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  825. <li class="nav-item nav-item-2"><a href="concurrency.html" >Concurrent Execution</a> &#187;</li>
  826. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code> — Launching parallel tasks</a></li>
  827. <li class="right">
  828. <div class="inline-search" role="search">
  829. <form class="inline-search" action="../search.html" method="get">
  830. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  831. <input type="submit" value="Go" />
  832. </form>
  833. </div>
  834. |
  835. </li>
  836. <li class="right">
  837. <label class="theme-selector-label">
  838. Theme
  839. <select class="theme-selector" oninput="activateTheme(this.value)">
  840. <option value="auto" selected>Auto</option>
  841. <option value="light">Light</option>
  842. <option value="dark">Dark</option>
  843. </select>
  844. </label> |</li>
  845. </ul>
  846. </div>
  847. <div class="footer">
  848. &copy;
  849. <a href="../copyright.html">
  850. Copyright
  851. </a>
  852. 2001-2024, Python Software Foundation.
  853. <br />
  854. This page is licensed under the Python Software Foundation License Version 2.
  855. <br />
  856. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  857. <br />
  858. See <a href="/license.html">History and License</a> for more information.<br />
  859. <br />
  860. The Python Software Foundation is a non-profit corporation.
  861. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  862. <br />
  863. <br />
  864. Last updated on Apr 09, 2024 (13:47 UTC).
  865. <a href="/bugs.html">Found a bug</a>?
  866. <br />
  867. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  868. </div>
  869. </body>
  870. </html>
上海开阖软件有限公司 沪ICP备12045867号-1