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.

3755 lines
418KB

  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="multiprocessing — Process-based parallelism" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/multiprocessing.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/multiprocessing/ Availability: not Emscripten, not WASI. This module does not work or is not available on WebAssembly platforms wasm32-emscripten and wasm32-wasi. See WebAssembly p..." />
  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/multiprocessing/ Availability: not Emscripten, not WASI. This module does not work or is not available on WebAssembly platforms wasm32-emscripten and wasm32-wasi. See WebAssembly p..." />
  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>multiprocessing — Process-based parallelism &#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="multiprocessing.shared_memory — Shared memory for direct access across processes" href="multiprocessing.shared_memory.html" />
  33. <link rel="prev" title="threading — Thread-based parallelism" href="threading.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/multiprocessing.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">multiprocessing</span></code> — Process-based parallelism</a><ul>
  86. <li><a class="reference internal" href="#introduction">Introduction</a><ul>
  87. <li><a class="reference internal" href="#the-process-class">The <code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code> class</a></li>
  88. <li><a class="reference internal" href="#contexts-and-start-methods">Contexts and start methods</a></li>
  89. <li><a class="reference internal" href="#exchanging-objects-between-processes">Exchanging objects between processes</a></li>
  90. <li><a class="reference internal" href="#synchronization-between-processes">Synchronization between processes</a></li>
  91. <li><a class="reference internal" href="#sharing-state-between-processes">Sharing state between processes</a></li>
  92. <li><a class="reference internal" href="#using-a-pool-of-workers">Using a pool of workers</a></li>
  93. </ul>
  94. </li>
  95. <li><a class="reference internal" href="#reference">Reference</a><ul>
  96. <li><a class="reference internal" href="#process-and-exceptions"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code> and exceptions</a></li>
  97. <li><a class="reference internal" href="#pipes-and-queues">Pipes and Queues</a></li>
  98. <li><a class="reference internal" href="#miscellaneous">Miscellaneous</a></li>
  99. <li><a class="reference internal" href="#connection-objects">Connection Objects</a></li>
  100. <li><a class="reference internal" href="#synchronization-primitives">Synchronization primitives</a></li>
  101. <li><a class="reference internal" href="#shared-ctypes-objects">Shared <code class="xref py py-mod docutils literal notranslate"><span class="pre">ctypes</span></code> Objects</a><ul>
  102. <li><a class="reference internal" href="#module-multiprocessing.sharedctypes">The <code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing.sharedctypes</span></code> module</a></li>
  103. </ul>
  104. </li>
  105. <li><a class="reference internal" href="#managers">Managers</a><ul>
  106. <li><a class="reference internal" href="#customized-managers">Customized managers</a></li>
  107. <li><a class="reference internal" href="#using-a-remote-manager">Using a remote manager</a></li>
  108. </ul>
  109. </li>
  110. <li><a class="reference internal" href="#proxy-objects">Proxy Objects</a><ul>
  111. <li><a class="reference internal" href="#cleanup">Cleanup</a></li>
  112. </ul>
  113. </li>
  114. <li><a class="reference internal" href="#module-multiprocessing.pool">Process Pools</a></li>
  115. <li><a class="reference internal" href="#module-multiprocessing.connection">Listeners and Clients</a><ul>
  116. <li><a class="reference internal" href="#address-formats">Address Formats</a></li>
  117. </ul>
  118. </li>
  119. <li><a class="reference internal" href="#authentication-keys">Authentication keys</a></li>
  120. <li><a class="reference internal" href="#logging">Logging</a></li>
  121. <li><a class="reference internal" href="#module-multiprocessing.dummy">The <code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing.dummy</span></code> module</a></li>
  122. </ul>
  123. </li>
  124. <li><a class="reference internal" href="#programming-guidelines">Programming guidelines</a><ul>
  125. <li><a class="reference internal" href="#all-start-methods">All start methods</a></li>
  126. <li><a class="reference internal" href="#the-spawn-and-forkserver-start-methods">The <em>spawn</em> and <em>forkserver</em> start methods</a></li>
  127. </ul>
  128. </li>
  129. <li><a class="reference internal" href="#examples">Examples</a></li>
  130. </ul>
  131. </li>
  132. </ul>
  133. </div>
  134. <div>
  135. <h4>Previous topic</h4>
  136. <p class="topless"><a href="threading.html"
  137. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code> — Thread-based parallelism</a></p>
  138. </div>
  139. <div>
  140. <h4>Next topic</h4>
  141. <p class="topless"><a href="multiprocessing.shared_memory.html"
  142. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing.shared_memory</span></code> — Shared memory for direct access across processes</a></p>
  143. </div>
  144. <div role="note" aria-label="source link">
  145. <h3>This Page</h3>
  146. <ul class="this-page-menu">
  147. <li><a href="../bugs.html">Report a Bug</a></li>
  148. <li>
  149. <a href="https://github.com/python/cpython/blob/main/Doc/library/multiprocessing.rst"
  150. rel="nofollow">Show Source
  151. </a>
  152. </li>
  153. </ul>
  154. </div>
  155. </nav>
  156. </div>
  157. </div>
  158. <div class="related" role="navigation" aria-label="related navigation">
  159. <h3>Navigation</h3>
  160. <ul>
  161. <li class="right" style="margin-right: 10px">
  162. <a href="../genindex.html" title="General Index"
  163. accesskey="I">index</a></li>
  164. <li class="right" >
  165. <a href="../py-modindex.html" title="Python Module Index"
  166. >modules</a> |</li>
  167. <li class="right" >
  168. <a href="multiprocessing.shared_memory.html" title="multiprocessing.shared_memory — Shared memory for direct access across processes"
  169. accesskey="N">next</a> |</li>
  170. <li class="right" >
  171. <a href="threading.html" title="threading — Thread-based parallelism"
  172. accesskey="P">previous</a> |</li>
  173. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  174. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  175. <li class="switchers">
  176. <div class="language_switcher_placeholder"></div>
  177. <div class="version_switcher_placeholder"></div>
  178. </li>
  179. <li>
  180. </li>
  181. <li id="cpython-language-and-version">
  182. <a href="../index.html">3.12.3 Documentation</a> &#187;
  183. </li>
  184. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  185. <li class="nav-item nav-item-2"><a href="concurrency.html" accesskey="U">Concurrent Execution</a> &#187;</li>
  186. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code> — Process-based parallelism</a></li>
  187. <li class="right">
  188. <div class="inline-search" role="search">
  189. <form class="inline-search" action="../search.html" method="get">
  190. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  191. <input type="submit" value="Go" />
  192. </form>
  193. </div>
  194. |
  195. </li>
  196. <li class="right">
  197. <label class="theme-selector-label">
  198. Theme
  199. <select class="theme-selector" oninput="activateTheme(this.value)">
  200. <option value="auto" selected>Auto</option>
  201. <option value="light">Light</option>
  202. <option value="dark">Dark</option>
  203. </select>
  204. </label> |</li>
  205. </ul>
  206. </div>
  207. <div class="document">
  208. <div class="documentwrapper">
  209. <div class="bodywrapper">
  210. <div class="body" role="main">
  211. <section id="module-multiprocessing">
  212. <span id="multiprocessing-process-based-parallelism"></span><h1><a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> — Process-based parallelism<a class="headerlink" href="#module-multiprocessing" title="Link to this heading">¶</a></h1>
  213. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/multiprocessing/">Lib/multiprocessing/</a></p>
  214. <hr class="docutils" />
  215. <div class="availability docutils container">
  216. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not Emscripten, not WASI.</p>
  217. <p>This module does not work or is not available on WebAssembly platforms
  218. <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
  219. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  220. </div>
  221. <section id="introduction">
  222. <h2>Introduction<a class="headerlink" href="#introduction" title="Link to this heading">¶</a></h2>
  223. <p><a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> is a package that supports spawning processes using an
  224. API similar to the <a class="reference internal" href="threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module. The <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> package
  225. offers both local and remote concurrency, effectively side-stepping the
  226. <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><span class="xref std std-term">Global Interpreter Lock</span></a> by using
  227. subprocesses instead of threads. Due
  228. to this, the <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> module allows the programmer to fully
  229. leverage multiple processors on a given machine. It runs on both POSIX and
  230. Windows.</p>
  231. <p>The <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> module also introduces APIs which do not have
  232. analogs in the <a class="reference internal" href="threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module. A prime example of this is the
  233. <a class="reference internal" href="#multiprocessing.pool.Pool" title="multiprocessing.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a> object which offers a convenient means of
  234. parallelizing the execution of a function across multiple input values,
  235. distributing the input data across processes (data parallelism). The following
  236. example demonstrates the common practice of defining such functions in a module
  237. so that child processes can successfully import that module. This basic example
  238. of data parallelism using <a class="reference internal" href="#multiprocessing.pool.Pool" title="multiprocessing.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a>,</p>
  239. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Pool</span>
  240. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
  241. <span class="k">return</span> <span class="n">x</span><span class="o">*</span><span class="n">x</span>
  242. <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>
  243. <span class="k">with</span> <span class="n">Pool</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="k">as</span> <span class="n">p</span><span class="p">:</span>
  244. <span class="nb">print</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]))</span>
  245. </pre></div>
  246. </div>
  247. <p>will print to standard output</p>
  248. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">9</span><span class="p">]</span>
  249. </pre></div>
  250. </div>
  251. <div class="admonition seealso">
  252. <p class="admonition-title">See also</p>
  253. <p><a class="reference internal" href="concurrent.futures.html#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">concurrent.futures.ProcessPoolExecutor</span></code></a> offers a higher level interface
  254. to push tasks to a background process without blocking execution of the
  255. calling process. Compared to using the <a class="reference internal" href="#multiprocessing.pool.Pool" title="multiprocessing.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a>
  256. interface directly, the <a class="reference internal" href="concurrent.futures.html#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> API more readily allows
  257. the submission of work to the underlying process pool to be separated from
  258. waiting for the results.</p>
  259. </div>
  260. <section id="the-process-class">
  261. <h3>The <a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> class<a class="headerlink" href="#the-process-class" title="Link to this heading">¶</a></h3>
  262. <p>In <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a>, processes are spawned by creating a <a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a>
  263. object and then calling its <a class="reference internal" href="#multiprocessing.Process.start" title="multiprocessing.Process.start"><code class="xref py py-meth docutils literal notranslate"><span class="pre">start()</span></code></a> method. <a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a>
  264. follows the API of <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>. A trivial example of a
  265. multiprocess program is</p>
  266. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span>
  267. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
  268. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;hello&#39;</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
  269. <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>
  270. <span class="n">p</span> <span class="o">=</span> <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">f</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;bob&#39;</span><span class="p">,))</span>
  271. <span class="n">p</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  272. <span class="n">p</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
  273. </pre></div>
  274. </div>
  275. <p>To show the individual process IDs involved, here is an expanded example:</p>
  276. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span>
  277. <span class="kn">import</span> <span class="nn">os</span>
  278. <span class="k">def</span> <span class="nf">info</span><span class="p">(</span><span class="n">title</span><span class="p">):</span>
  279. <span class="nb">print</span><span class="p">(</span><span class="n">title</span><span class="p">)</span>
  280. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;module name:&#39;</span><span class="p">,</span> <span class="vm">__name__</span><span class="p">)</span>
  281. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;parent process:&#39;</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">getppid</span><span class="p">())</span>
  282. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;process id:&#39;</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">())</span>
  283. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
  284. <span class="n">info</span><span class="p">(</span><span class="s1">&#39;function f&#39;</span><span class="p">)</span>
  285. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;hello&#39;</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
  286. <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>
  287. <span class="n">info</span><span class="p">(</span><span class="s1">&#39;main line&#39;</span><span class="p">)</span>
  288. <span class="n">p</span> <span class="o">=</span> <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">f</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;bob&#39;</span><span class="p">,))</span>
  289. <span class="n">p</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  290. <span class="n">p</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
  291. </pre></div>
  292. </div>
  293. <p>For an explanation of why the <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">__name__</span> <span class="pre">==</span> <span class="pre">'__main__'</span></code> part is
  294. necessary, see <a class="reference internal" href="#multiprocessing-programming"><span class="std std-ref">Programming guidelines</span></a>.</p>
  295. </section>
  296. <section id="contexts-and-start-methods">
  297. <span id="multiprocessing-start-methods"></span><h3>Contexts and start methods<a class="headerlink" href="#contexts-and-start-methods" title="Link to this heading">¶</a></h3>
  298. <p>Depending on the platform, <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> supports three ways
  299. to start a process. These <em>start methods</em> are</p>
  300. <blockquote>
  301. <div><dl>
  302. <dt><em>spawn</em></dt><dd><p>The parent process starts a fresh Python interpreter process. The
  303. child process will only inherit those resources necessary to run
  304. the process object’s <a class="reference internal" href="#multiprocessing.Process.run" title="multiprocessing.Process.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method. In particular,
  305. unnecessary file descriptors and handles from the parent process
  306. will not be inherited. Starting a process using this method is
  307. rather slow compared to using <em>fork</em> or <em>forkserver</em>.</p>
  308. <p>Available on POSIX and Windows platforms. The default on Windows and macOS.</p>
  309. </dd>
  310. <dt><em>fork</em></dt><dd><p>The parent process uses <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> to fork the Python
  311. interpreter. The child process, when it begins, is effectively
  312. identical to the parent process. All resources of the parent are
  313. inherited by the child process. Note that safely forking a
  314. multithreaded process is problematic.</p>
  315. <p>Available on POSIX systems. Currently the default on POSIX except macOS.</p>
  316. <div class="admonition note">
  317. <p class="admonition-title">Note</p>
  318. <p>The default start method will change away from <em>fork</em> in Python 3.14.
  319. Code that requires <em>fork</em> should explicitly specify that via
  320. <a class="reference internal" href="#multiprocessing.get_context" title="multiprocessing.get_context"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_context()</span></code></a> or <a class="reference internal" href="#multiprocessing.set_start_method" title="multiprocessing.set_start_method"><code class="xref py py-func docutils literal notranslate"><span class="pre">set_start_method()</span></code></a>.</p>
  321. </div>
  322. <div class="versionchanged">
  323. <p><span class="versionmodified changed">Changed in version 3.12: </span>If Python is able to detect that your process has multiple threads, the
  324. <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 that this start method calls internally will
  325. raise a <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>. Use a different start method.
  326. 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 further explanation.</p>
  327. </div>
  328. </dd>
  329. <dt><em>forkserver</em></dt><dd><p>When the program starts and selects the <em>forkserver</em> start method,
  330. a server process is spawned. From then on, whenever a new process
  331. is needed, the parent process connects to the server and requests
  332. that it fork a new process. The fork server process is single threaded
  333. unless system libraries or preloaded imports spawn threads as a
  334. side-effect so it is generally safe for it to use <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>.
  335. No unnecessary resources are inherited.</p>
  336. <p>Available on POSIX platforms which support passing file descriptors
  337. over Unix pipes such as Linux.</p>
  338. </dd>
  339. </dl>
  340. </div></blockquote>
  341. <div class="versionchanged">
  342. <p><span class="versionmodified changed">Changed in version 3.4: </span><em>spawn</em> added on all POSIX platforms, and <em>forkserver</em> added for
  343. some POSIX platforms.
  344. Child processes no longer inherit all of the parents inheritable
  345. handles on Windows.</p>
  346. </div>
  347. <div class="versionchanged">
  348. <p><span class="versionmodified changed">Changed in version 3.8: </span>On macOS, the <em>spawn</em> start method is now the default. The <em>fork</em> start
  349. method should be considered unsafe as it can lead to crashes of the
  350. subprocess as macOS system libraries may start threads. See <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=33725">bpo-33725</a>.</p>
  351. </div>
  352. <p>On POSIX using the <em>spawn</em> or <em>forkserver</em> start methods will also
  353. start a <em>resource tracker</em> process which tracks the unlinked named
  354. system resources (such as named semaphores or
  355. <a class="reference internal" href="multiprocessing.shared_memory.html#multiprocessing.shared_memory.SharedMemory" title="multiprocessing.shared_memory.SharedMemory"><code class="xref py py-class docutils literal notranslate"><span class="pre">SharedMemory</span></code></a> objects) created
  356. by processes of the program. When all processes
  357. have exited the resource tracker unlinks any remaining tracked object.
  358. Usually there should be none, but if a process was killed by a signal
  359. there may be some “leaked” resources. (Neither leaked semaphores nor shared
  360. memory segments will be automatically unlinked until the next reboot. This is
  361. problematic for both objects because the system allows only a limited number of
  362. named semaphores, and shared memory segments occupy some space in the main
  363. memory.)</p>
  364. <p>To select a start method you use the <a class="reference internal" href="#multiprocessing.set_start_method" title="multiprocessing.set_start_method"><code class="xref py py-func docutils literal notranslate"><span class="pre">set_start_method()</span></code></a> in
  365. the <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">__name__</span> <span class="pre">==</span> <span class="pre">'__main__'</span></code> clause of the main module. For
  366. example:</p>
  367. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">multiprocessing</span> <span class="k">as</span> <span class="nn">mp</span>
  368. <span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">q</span><span class="p">):</span>
  369. <span class="n">q</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="s1">&#39;hello&#39;</span><span class="p">)</span>
  370. <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>
  371. <span class="n">mp</span><span class="o">.</span><span class="n">set_start_method</span><span class="p">(</span><span class="s1">&#39;spawn&#39;</span><span class="p">)</span>
  372. <span class="n">q</span> <span class="o">=</span> <span class="n">mp</span><span class="o">.</span><span class="n">Queue</span><span class="p">()</span>
  373. <span class="n">p</span> <span class="o">=</span> <span class="n">mp</span><span class="o">.</span><span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">foo</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">q</span><span class="p">,))</span>
  374. <span class="n">p</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  375. <span class="nb">print</span><span class="p">(</span><span class="n">q</span><span class="o">.</span><span class="n">get</span><span class="p">())</span>
  376. <span class="n">p</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
  377. </pre></div>
  378. </div>
  379. <p><a class="reference internal" href="#multiprocessing.set_start_method" title="multiprocessing.set_start_method"><code class="xref py py-func docutils literal notranslate"><span class="pre">set_start_method()</span></code></a> should not be used more than once in the
  380. program.</p>
  381. <p>Alternatively, you can use <a class="reference internal" href="#multiprocessing.get_context" title="multiprocessing.get_context"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_context()</span></code></a> to obtain a context
  382. object. Context objects have the same API as the multiprocessing
  383. module, and allow one to use multiple start methods in the same
  384. program.</p>
  385. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">multiprocessing</span> <span class="k">as</span> <span class="nn">mp</span>
  386. <span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">q</span><span class="p">):</span>
  387. <span class="n">q</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="s1">&#39;hello&#39;</span><span class="p">)</span>
  388. <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>
  389. <span class="n">ctx</span> <span class="o">=</span> <span class="n">mp</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="s1">&#39;spawn&#39;</span><span class="p">)</span>
  390. <span class="n">q</span> <span class="o">=</span> <span class="n">ctx</span><span class="o">.</span><span class="n">Queue</span><span class="p">()</span>
  391. <span class="n">p</span> <span class="o">=</span> <span class="n">ctx</span><span class="o">.</span><span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">foo</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">q</span><span class="p">,))</span>
  392. <span class="n">p</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  393. <span class="nb">print</span><span class="p">(</span><span class="n">q</span><span class="o">.</span><span class="n">get</span><span class="p">())</span>
  394. <span class="n">p</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
  395. </pre></div>
  396. </div>
  397. <p>Note that objects related to one context may not be compatible with
  398. processes for a different context. In particular, locks created using
  399. the <em>fork</em> context cannot be passed to processes started using the
  400. <em>spawn</em> or <em>forkserver</em> start methods.</p>
  401. <p>A library which wants to use a particular start method should probably
  402. use <a class="reference internal" href="#multiprocessing.get_context" title="multiprocessing.get_context"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_context()</span></code></a> to avoid interfering with the choice of the
  403. library user.</p>
  404. <div class="admonition warning">
  405. <p class="admonition-title">Warning</p>
  406. <p>The <code class="docutils literal notranslate"><span class="pre">'spawn'</span></code> and <code class="docutils literal notranslate"><span class="pre">'forkserver'</span></code> start methods generally cannot
  407. be used with “frozen” executables (i.e., binaries produced by
  408. packages like <strong>PyInstaller</strong> and <strong>cx_Freeze</strong>) on POSIX systems.
  409. The <code class="docutils literal notranslate"><span class="pre">'fork'</span></code> start method may work if code does not use threads.</p>
  410. </div>
  411. </section>
  412. <section id="exchanging-objects-between-processes">
  413. <h3>Exchanging objects between processes<a class="headerlink" href="#exchanging-objects-between-processes" title="Link to this heading">¶</a></h3>
  414. <p><a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> supports two types of communication channel between
  415. processes:</p>
  416. <p><strong>Queues</strong></p>
  417. <blockquote>
  418. <div><p>The <a class="reference internal" href="#multiprocessing.Queue" title="multiprocessing.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a> class is a near clone of <a class="reference internal" href="queue.html#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">queue.Queue</span></code></a>. For
  419. example:</p>
  420. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">Queue</span>
  421. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">q</span><span class="p">):</span>
  422. <span class="n">q</span><span class="o">.</span><span class="n">put</span><span class="p">([</span><span class="mi">42</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="s1">&#39;hello&#39;</span><span class="p">])</span>
  423. <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>
  424. <span class="n">q</span> <span class="o">=</span> <span class="n">Queue</span><span class="p">()</span>
  425. <span class="n">p</span> <span class="o">=</span> <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">f</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">q</span><span class="p">,))</span>
  426. <span class="n">p</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  427. <span class="nb">print</span><span class="p">(</span><span class="n">q</span><span class="o">.</span><span class="n">get</span><span class="p">())</span> <span class="c1"># prints &quot;[42, None, &#39;hello&#39;]&quot;</span>
  428. <span class="n">p</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
  429. </pre></div>
  430. </div>
  431. <p>Queues are thread and process safe.</p>
  432. </div></blockquote>
  433. <p><strong>Pipes</strong></p>
  434. <blockquote>
  435. <div><p>The <a class="reference internal" href="#multiprocessing.Pipe" title="multiprocessing.Pipe"><code class="xref py py-func docutils literal notranslate"><span class="pre">Pipe()</span></code></a> function returns a pair of connection objects connected by a
  436. pipe which by default is duplex (two-way). For example:</p>
  437. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">Pipe</span>
  438. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">conn</span><span class="p">):</span>
  439. <span class="n">conn</span><span class="o">.</span><span class="n">send</span><span class="p">([</span><span class="mi">42</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="s1">&#39;hello&#39;</span><span class="p">])</span>
  440. <span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  441. <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>
  442. <span class="n">parent_conn</span><span class="p">,</span> <span class="n">child_conn</span> <span class="o">=</span> <span class="n">Pipe</span><span class="p">()</span>
  443. <span class="n">p</span> <span class="o">=</span> <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">f</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">child_conn</span><span class="p">,))</span>
  444. <span class="n">p</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  445. <span class="nb">print</span><span class="p">(</span><span class="n">parent_conn</span><span class="o">.</span><span class="n">recv</span><span class="p">())</span> <span class="c1"># prints &quot;[42, None, &#39;hello&#39;]&quot;</span>
  446. <span class="n">p</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
  447. </pre></div>
  448. </div>
  449. <p>The two connection objects returned by <a class="reference internal" href="#multiprocessing.Pipe" title="multiprocessing.Pipe"><code class="xref py py-func docutils literal notranslate"><span class="pre">Pipe()</span></code></a> represent the two ends of
  450. the pipe. Each connection object has <code class="xref py py-meth docutils literal notranslate"><span class="pre">send()</span></code> and
  451. <code class="xref py py-meth docutils literal notranslate"><span class="pre">recv()</span></code> methods (among others). Note that data in a pipe
  452. may become corrupted if two processes (or threads) try to read from or write
  453. to the <em>same</em> end of the pipe at the same time. Of course there is no risk
  454. of corruption from processes using different ends of the pipe at the same
  455. time.</p>
  456. </div></blockquote>
  457. </section>
  458. <section id="synchronization-between-processes">
  459. <h3>Synchronization between processes<a class="headerlink" href="#synchronization-between-processes" title="Link to this heading">¶</a></h3>
  460. <p><a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> contains equivalents of all the synchronization
  461. primitives from <a class="reference internal" href="threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a>. For instance one can use a lock to ensure
  462. that only one process prints to standard output at a time:</p>
  463. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">Lock</span>
  464. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
  465. <span class="n">l</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
  466. <span class="k">try</span><span class="p">:</span>
  467. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;hello world&#39;</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
  468. <span class="k">finally</span><span class="p">:</span>
  469. <span class="n">l</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
  470. <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>
  471. <span class="n">lock</span> <span class="o">=</span> <span class="n">Lock</span><span class="p">()</span>
  472. <span class="k">for</span> <span class="n">num</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
  473. <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">f</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">lock</span><span class="p">,</span> <span class="n">num</span><span class="p">))</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  474. </pre></div>
  475. </div>
  476. <p>Without using the lock output from the different processes is liable to get all
  477. mixed up.</p>
  478. </section>
  479. <section id="sharing-state-between-processes">
  480. <h3>Sharing state between processes<a class="headerlink" href="#sharing-state-between-processes" title="Link to this heading">¶</a></h3>
  481. <p>As mentioned above, when doing concurrent programming it is usually best to
  482. avoid using shared state as far as possible. This is particularly true when
  483. using multiple processes.</p>
  484. <p>However, if you really do need to use some shared data then
  485. <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> provides a couple of ways of doing so.</p>
  486. <p><strong>Shared memory</strong></p>
  487. <blockquote>
  488. <div><p>Data can be stored in a shared memory map using <a class="reference internal" href="#multiprocessing.Value" title="multiprocessing.Value"><code class="xref py py-class docutils literal notranslate"><span class="pre">Value</span></code></a> or
  489. <a class="reference internal" href="#multiprocessing.Array" title="multiprocessing.Array"><code class="xref py py-class docutils literal notranslate"><span class="pre">Array</span></code></a>. For example, the following code</p>
  490. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">Value</span><span class="p">,</span> <span class="n">Array</span>
  491. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">a</span><span class="p">):</span>
  492. <span class="n">n</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="mf">3.1415927</span>
  493. <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="nb">len</span><span class="p">(</span><span class="n">a</span><span class="p">)):</span>
  494. <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
  495. <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>
  496. <span class="n">num</span> <span class="o">=</span> <span class="n">Value</span><span class="p">(</span><span class="s1">&#39;d&#39;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
  497. <span class="n">arr</span> <span class="o">=</span> <span class="n">Array</span><span class="p">(</span><span class="s1">&#39;i&#39;</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>
  498. <span class="n">p</span> <span class="o">=</span> <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">f</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">num</span><span class="p">,</span> <span class="n">arr</span><span class="p">))</span>
  499. <span class="n">p</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  500. <span class="n">p</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
  501. <span class="nb">print</span><span class="p">(</span><span class="n">num</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
  502. <span class="nb">print</span><span class="p">(</span><span class="n">arr</span><span class="p">[:])</span>
  503. </pre></div>
  504. </div>
  505. <p>will print</p>
  506. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mf">3.1415927</span>
  507. <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mi">4</span><span class="p">,</span> <span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="o">-</span><span class="mi">6</span><span class="p">,</span> <span class="o">-</span><span class="mi">7</span><span class="p">,</span> <span class="o">-</span><span class="mi">8</span><span class="p">,</span> <span class="o">-</span><span class="mi">9</span><span class="p">]</span>
  508. </pre></div>
  509. </div>
  510. <p>The <code class="docutils literal notranslate"><span class="pre">'d'</span></code> and <code class="docutils literal notranslate"><span class="pre">'i'</span></code> arguments used when creating <code class="docutils literal notranslate"><span class="pre">num</span></code> and <code class="docutils literal notranslate"><span class="pre">arr</span></code> are
  511. typecodes of the kind used by the <a class="reference internal" href="array.html#module-array" title="array: Space efficient arrays of uniformly typed numeric values."><code class="xref py py-mod docutils literal notranslate"><span class="pre">array</span></code></a> module: <code class="docutils literal notranslate"><span class="pre">'d'</span></code> indicates a
  512. double precision float and <code class="docutils literal notranslate"><span class="pre">'i'</span></code> indicates a signed integer. These shared
  513. objects will be process and thread-safe.</p>
  514. <p>For more flexibility in using shared memory one can use the
  515. <a class="reference internal" href="#module-multiprocessing.sharedctypes" title="multiprocessing.sharedctypes: Allocate ctypes objects from shared memory."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing.sharedctypes</span></code></a> module which supports the creation of
  516. arbitrary ctypes objects allocated from shared memory.</p>
  517. </div></blockquote>
  518. <p><strong>Server process</strong></p>
  519. <blockquote>
  520. <div><p>A manager object returned by <a class="reference internal" href="#multiprocessing.Manager" title="multiprocessing.Manager"><code class="xref py py-func docutils literal notranslate"><span class="pre">Manager()</span></code></a> controls a server process which
  521. holds Python objects and allows other processes to manipulate them using
  522. proxies.</p>
  523. <p>A manager returned by <a class="reference internal" href="#multiprocessing.Manager" title="multiprocessing.Manager"><code class="xref py py-func docutils literal notranslate"><span class="pre">Manager()</span></code></a> will support types
  524. <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a>, <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>, <a class="reference internal" href="#multiprocessing.managers.Namespace" title="multiprocessing.managers.Namespace"><code class="xref py py-class docutils literal notranslate"><span class="pre">Namespace</span></code></a>, <a class="reference internal" href="#multiprocessing.Lock" title="multiprocessing.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">Lock</span></code></a>,
  525. <a class="reference internal" href="#multiprocessing.RLock" title="multiprocessing.RLock"><code class="xref py py-class docutils literal notranslate"><span class="pre">RLock</span></code></a>, <a class="reference internal" href="#multiprocessing.Semaphore" title="multiprocessing.Semaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">Semaphore</span></code></a>, <a class="reference internal" href="#multiprocessing.BoundedSemaphore" title="multiprocessing.BoundedSemaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">BoundedSemaphore</span></code></a>,
  526. <a class="reference internal" href="#multiprocessing.Condition" title="multiprocessing.Condition"><code class="xref py py-class docutils literal notranslate"><span class="pre">Condition</span></code></a>, <a class="reference internal" href="#multiprocessing.Event" title="multiprocessing.Event"><code class="xref py py-class docutils literal notranslate"><span class="pre">Event</span></code></a>, <a class="reference internal" href="#multiprocessing.Barrier" title="multiprocessing.Barrier"><code class="xref py py-class docutils literal notranslate"><span class="pre">Barrier</span></code></a>,
  527. <a class="reference internal" href="#multiprocessing.Queue" title="multiprocessing.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a>, <a class="reference internal" href="#multiprocessing.Value" title="multiprocessing.Value"><code class="xref py py-class docutils literal notranslate"><span class="pre">Value</span></code></a> and <a class="reference internal" href="#multiprocessing.Array" title="multiprocessing.Array"><code class="xref py py-class docutils literal notranslate"><span class="pre">Array</span></code></a>. For example,</p>
  528. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">Manager</span>
  529. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">l</span><span class="p">):</span>
  530. <span class="n">d</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;1&#39;</span>
  531. <span class="n">d</span><span class="p">[</span><span class="s1">&#39;2&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span>
  532. <span class="n">d</span><span class="p">[</span><span class="mf">0.25</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
  533. <span class="n">l</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>
  534. <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>
  535. <span class="k">with</span> <span class="n">Manager</span><span class="p">()</span> <span class="k">as</span> <span class="n">manager</span><span class="p">:</span>
  536. <span class="n">d</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">dict</span><span class="p">()</span>
  537. <span class="n">l</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>
  538. <span class="n">p</span> <span class="o">=</span> <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">f</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">l</span><span class="p">))</span>
  539. <span class="n">p</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  540. <span class="n">p</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
  541. <span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
  542. <span class="nb">print</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
  543. </pre></div>
  544. </div>
  545. <p>will print</p>
  546. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">{</span><span class="mf">0.25</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span> <span class="mi">1</span><span class="p">:</span> <span class="s1">&#39;1&#39;</span><span class="p">,</span> <span class="s1">&#39;2&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">}</span>
  547. <span class="p">[</span><span class="mi">9</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
  548. </pre></div>
  549. </div>
  550. <p>Server process managers are more flexible than using shared memory objects
  551. because they can be made to support arbitrary object types. Also, a single
  552. manager can be shared by processes on different computers over a network.
  553. They are, however, slower than using shared memory.</p>
  554. </div></blockquote>
  555. </section>
  556. <section id="using-a-pool-of-workers">
  557. <h3>Using a pool of workers<a class="headerlink" href="#using-a-pool-of-workers" title="Link to this heading">¶</a></h3>
  558. <p>The <a class="reference internal" href="#multiprocessing.pool.Pool" title="multiprocessing.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a> class represents a pool of worker
  559. processes. It has methods which allows tasks to be offloaded to the worker
  560. processes in a few different ways.</p>
  561. <p>For example:</p>
  562. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Pool</span><span class="p">,</span> <span class="ne">TimeoutError</span>
  563. <span class="kn">import</span> <span class="nn">time</span>
  564. <span class="kn">import</span> <span class="nn">os</span>
  565. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
  566. <span class="k">return</span> <span class="n">x</span><span class="o">*</span><span class="n">x</span>
  567. <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>
  568. <span class="c1"># start 4 worker processes</span>
  569. <span class="k">with</span> <span class="n">Pool</span><span class="p">(</span><span class="n">processes</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span> <span class="k">as</span> <span class="n">pool</span><span class="p">:</span>
  570. <span class="c1"># print &quot;[0, 1, 4,..., 81]&quot;</span>
  571. <span class="nb">print</span><span class="p">(</span><span class="n">pool</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)))</span>
  572. <span class="c1"># print same numbers in arbitrary order</span>
  573. <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">pool</span><span class="o">.</span><span class="n">imap_unordered</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)):</span>
  574. <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
  575. <span class="c1"># evaluate &quot;f(20)&quot; asynchronously</span>
  576. <span class="n">res</span> <span class="o">=</span> <span class="n">pool</span><span class="o">.</span><span class="n">apply_async</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="p">(</span><span class="mi">20</span><span class="p">,))</span> <span class="c1"># runs in *only* one process</span>
  577. <span class="nb">print</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">timeout</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span> <span class="c1"># prints &quot;400&quot;</span>
  578. <span class="c1"># evaluate &quot;os.getpid()&quot; asynchronously</span>
  579. <span class="n">res</span> <span class="o">=</span> <span class="n">pool</span><span class="o">.</span><span class="n">apply_async</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">,</span> <span class="p">())</span> <span class="c1"># runs in *only* one process</span>
  580. <span class="nb">print</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">timeout</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span> <span class="c1"># prints the PID of that process</span>
  581. <span class="c1"># launching multiple evaluations asynchronously *may* use more processes</span>
  582. <span class="n">multiple_results</span> <span class="o">=</span> <span class="p">[</span><span class="n">pool</span><span class="o">.</span><span class="n">apply_async</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">,</span> <span class="p">())</span> <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">4</span><span class="p">)]</span>
  583. <span class="nb">print</span><span class="p">([</span><span class="n">res</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">timeout</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="k">for</span> <span class="n">res</span> <span class="ow">in</span> <span class="n">multiple_results</span><span class="p">])</span>
  584. <span class="c1"># make a single worker sleep for 10 seconds</span>
  585. <span class="n">res</span> <span class="o">=</span> <span class="n">pool</span><span class="o">.</span><span class="n">apply_async</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">,</span> <span class="p">(</span><span class="mi">10</span><span class="p">,))</span>
  586. <span class="k">try</span><span class="p">:</span>
  587. <span class="nb">print</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">timeout</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
  588. <span class="k">except</span> <span class="ne">TimeoutError</span><span class="p">:</span>
  589. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;We lacked patience and got a multiprocessing.TimeoutError&quot;</span><span class="p">)</span>
  590. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;For the moment, the pool remains available for more work&quot;</span><span class="p">)</span>
  591. <span class="c1"># exiting the &#39;with&#39;-block has stopped the pool</span>
  592. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Now the pool is closed and no longer available&quot;</span><span class="p">)</span>
  593. </pre></div>
  594. </div>
  595. <p>Note that the methods of a pool should only ever be used by the
  596. process which created it.</p>
  597. <div class="admonition note">
  598. <p class="admonition-title">Note</p>
  599. <p>Functionality within this package requires that the <code class="docutils literal notranslate"><span class="pre">__main__</span></code> module be
  600. importable by the children. This is covered in <a class="reference internal" href="#multiprocessing-programming"><span class="std std-ref">Programming guidelines</span></a>
  601. however it is worth pointing out here. This means that some examples, such
  602. as the <a class="reference internal" href="#multiprocessing.pool.Pool" title="multiprocessing.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">multiprocessing.pool.Pool</span></code></a> examples will not work in the
  603. interactive interpreter. For example:</p>
  604. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Pool</span>
  605. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Pool</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
  606. <span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
  607. <span class="gp">... </span> <span class="k">return</span> <span class="n">x</span><span class="o">*</span><span class="n">x</span>
  608. <span class="gp">...</span>
  609. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">p</span><span class="p">:</span>
  610. <span class="gp">... </span> <span class="n">p</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span>
  611. <span class="go">Process PoolWorker-1:</span>
  612. <span class="go">Process PoolWorker-2:</span>
  613. <span class="go">Process PoolWorker-3:</span>
  614. <span class="gt">Traceback (most recent call last):</span>
  615. <span class="gt">Traceback (most recent call last):</span>
  616. <span class="gt">Traceback (most recent call last):</span>
  617. <span class="gr">AttributeError</span>: <span class="n">Can&#39;t get attribute &#39;f&#39; on &lt;module &#39;__main__&#39; (&lt;class &#39;_frozen_importlib.BuiltinImporter&#39;&gt;)&gt;</span>
  618. <span class="x">AttributeError: Can&#39;t get attribute &#39;f&#39; on &lt;module &#39;__main__&#39; (&lt;class &#39;_frozen_importlib.BuiltinImporter&#39;&gt;)&gt;</span>
  619. <span class="x">AttributeError: Can&#39;t get attribute &#39;f&#39; on &lt;module &#39;__main__&#39; (&lt;class &#39;_frozen_importlib.BuiltinImporter&#39;&gt;)&gt;</span>
  620. </pre></div>
  621. </div>
  622. <p>(If you try this it will actually output three full tracebacks
  623. interleaved in a semi-random fashion, and then you may have to
  624. stop the parent process somehow.)</p>
  625. </div>
  626. </section>
  627. </section>
  628. <section id="reference">
  629. <h2>Reference<a class="headerlink" href="#reference" title="Link to this heading">¶</a></h2>
  630. <p>The <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> package mostly replicates the API of the
  631. <a class="reference internal" href="threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module.</p>
  632. <section id="process-and-exceptions">
  633. <h3><a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> and exceptions<a class="headerlink" href="#process-and-exceptions" title="Link to this heading">¶</a></h3>
  634. <dl class="py class">
  635. <dt class="sig sig-object py" id="multiprocessing.Process">
  636. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">Process</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">group</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">target</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">name</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">args</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">kwargs</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="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">daemon</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="#multiprocessing.Process" title="Link to this definition">¶</a></dt>
  637. <dd><p>Process objects represent activity that is run in a separate process. The
  638. <a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> class has equivalents of all the methods of
  639. <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>.</p>
  640. <p>The constructor should always be called with keyword arguments. <em>group</em>
  641. should always be <code class="docutils literal notranslate"><span class="pre">None</span></code>; it exists solely for compatibility with
  642. <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>. <em>target</em> is the callable object to be invoked by
  643. the <a class="reference internal" href="#multiprocessing.Process.run" title="multiprocessing.Process.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method. It defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code>, meaning nothing is
  644. called. <em>name</em> is the process name (see <a class="reference internal" href="#multiprocessing.Process.name" title="multiprocessing.Process.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a> for more details).
  645. <em>args</em> is the argument tuple for the target invocation. <em>kwargs</em> is a
  646. dictionary of keyword arguments for the target invocation. If provided,
  647. the keyword-only <em>daemon</em> argument sets the process <a class="reference internal" href="#multiprocessing.Process.daemon" title="multiprocessing.Process.daemon"><code class="xref py py-attr docutils literal notranslate"><span class="pre">daemon</span></code></a> flag
  648. to <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code>. If <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default), this flag will be
  649. inherited from the creating process.</p>
  650. <p>By default, no arguments are passed to <em>target</em>. The <em>args</em> argument,
  651. which defaults to <code class="docutils literal notranslate"><span class="pre">()</span></code>, can be used to specify a list or tuple of the arguments
  652. to pass to <em>target</em>.</p>
  653. <p>If a subclass overrides the constructor, it must make sure it invokes the
  654. base class constructor (<code class="xref py py-meth docutils literal notranslate"><span class="pre">Process.__init__()</span></code>) before doing anything else
  655. to the process.</p>
  656. <div class="versionchanged">
  657. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>daemon</em> parameter.</p>
  658. </div>
  659. <dl class="py method">
  660. <dt class="sig sig-object py" id="multiprocessing.Process.run">
  661. <span class="sig-name descname"><span class="pre">run</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Process.run" title="Link to this definition">¶</a></dt>
  662. <dd><p>Method representing the process’s activity.</p>
  663. <p>You may override this method in a subclass. The standard <a class="reference internal" href="#multiprocessing.Process.run" title="multiprocessing.Process.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a>
  664. method invokes the callable object passed to the object’s constructor as
  665. the target argument, if any, with sequential and keyword arguments taken
  666. from the <em>args</em> and <em>kwargs</em> arguments, respectively.</p>
  667. <p>Using a list or tuple as the <em>args</em> argument passed to <a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a>
  668. achieves the same effect.</p>
  669. <p>Example:</p>
  670. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span>
  671. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="nb">print</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
  672. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
  673. <span class="go">1</span>
  674. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="nb">print</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="mi">1</span><span class="p">,))</span>
  675. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
  676. <span class="go">1</span>
  677. </pre></div>
  678. </div>
  679. </dd></dl>
  680. <dl class="py method">
  681. <dt class="sig sig-object py" id="multiprocessing.Process.start">
  682. <span class="sig-name descname"><span class="pre">start</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Process.start" title="Link to this definition">¶</a></dt>
  683. <dd><p>Start the process’s activity.</p>
  684. <p>This must be called at most once per process object. It arranges for the
  685. object’s <a class="reference internal" href="#multiprocessing.Process.run" title="multiprocessing.Process.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method to be invoked in a separate process.</p>
  686. </dd></dl>
  687. <dl class="py method">
  688. <dt class="sig sig-object py" id="multiprocessing.Process.join">
  689. <span class="sig-name descname"><span class="pre">join</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">timeout</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Process.join" title="Link to this definition">¶</a></dt>
  690. <dd><p>If the optional argument <em>timeout</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default), the method
  691. blocks until the process whose <a class="reference internal" href="#multiprocessing.Process.join" title="multiprocessing.Process.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> method is called terminates.
  692. If <em>timeout</em> is a positive number, it blocks at most <em>timeout</em> seconds.
  693. Note that the method returns <code class="docutils literal notranslate"><span class="pre">None</span></code> if its process terminates or if the
  694. method times out. Check the process’s <a class="reference internal" href="#multiprocessing.Process.exitcode" title="multiprocessing.Process.exitcode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">exitcode</span></code></a> to determine if
  695. it terminated.</p>
  696. <p>A process can be joined many times.</p>
  697. <p>A process cannot join itself because this would cause a deadlock. It is
  698. an error to attempt to join a process before it has been started.</p>
  699. </dd></dl>
  700. <dl class="py attribute">
  701. <dt class="sig sig-object py" id="multiprocessing.Process.name">
  702. <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#multiprocessing.Process.name" title="Link to this definition">¶</a></dt>
  703. <dd><p>The process’s name. The name is a string used for identification purposes
  704. only. It has no semantics. Multiple processes may be given the same
  705. name.</p>
  706. <p>The initial name is set by the constructor. If no explicit name is
  707. provided to the constructor, a name of the form
  708. ‘Process-N<sub>1</sub>:N<sub>2</sub>:…:N<sub>k</sub>’ is constructed, where
  709. each N<sub>k</sub> is the N-th child of its parent.</p>
  710. </dd></dl>
  711. <dl class="py method">
  712. <dt class="sig sig-object py" id="multiprocessing.Process.is_alive">
  713. <span class="sig-name descname"><span class="pre">is_alive</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Process.is_alive" title="Link to this definition">¶</a></dt>
  714. <dd><p>Return whether the process is alive.</p>
  715. <p>Roughly, a process object is alive from the moment the <a class="reference internal" href="#multiprocessing.Process.start" title="multiprocessing.Process.start"><code class="xref py py-meth docutils literal notranslate"><span class="pre">start()</span></code></a>
  716. method returns until the child process terminates.</p>
  717. </dd></dl>
  718. <dl class="py attribute">
  719. <dt class="sig sig-object py" id="multiprocessing.Process.daemon">
  720. <span class="sig-name descname"><span class="pre">daemon</span></span><a class="headerlink" href="#multiprocessing.Process.daemon" title="Link to this definition">¶</a></dt>
  721. <dd><p>The process’s daemon flag, a Boolean value. This must be set before
  722. <a class="reference internal" href="#multiprocessing.Process.start" title="multiprocessing.Process.start"><code class="xref py py-meth docutils literal notranslate"><span class="pre">start()</span></code></a> is called.</p>
  723. <p>The initial value is inherited from the creating process.</p>
  724. <p>When a process exits, it attempts to terminate all of its daemonic child
  725. processes.</p>
  726. <p>Note that a daemonic process is not allowed to create child processes.
  727. Otherwise a daemonic process would leave its children orphaned if it gets
  728. terminated when its parent process exits. Additionally, these are <strong>not</strong>
  729. Unix daemons or services, they are normal processes that will be
  730. terminated (and not joined) if non-daemonic processes have exited.</p>
  731. </dd></dl>
  732. <p>In addition to 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> API, <a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> objects
  733. also support the following attributes and methods:</p>
  734. <dl class="py attribute">
  735. <dt class="sig sig-object py" id="multiprocessing.Process.pid">
  736. <span class="sig-name descname"><span class="pre">pid</span></span><a class="headerlink" href="#multiprocessing.Process.pid" title="Link to this definition">¶</a></dt>
  737. <dd><p>Return the process ID. Before the process is spawned, this will be
  738. <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  739. </dd></dl>
  740. <dl class="py attribute">
  741. <dt class="sig sig-object py" id="multiprocessing.Process.exitcode">
  742. <span class="sig-name descname"><span class="pre">exitcode</span></span><a class="headerlink" href="#multiprocessing.Process.exitcode" title="Link to this definition">¶</a></dt>
  743. <dd><p>The child’s exit code. This will be <code class="docutils literal notranslate"><span class="pre">None</span></code> if the process has not yet
  744. terminated.</p>
  745. <p>If the child’s <a class="reference internal" href="#multiprocessing.Process.run" title="multiprocessing.Process.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method returned normally, the exit code
  746. will be 0. If it terminated via <a class="reference internal" href="sys.html#sys.exit" title="sys.exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exit()</span></code></a> with an integer
  747. argument <em>N</em>, the exit code will be <em>N</em>.</p>
  748. <p>If the child terminated due to an exception not caught within
  749. <a class="reference internal" href="#multiprocessing.Process.run" title="multiprocessing.Process.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a>, the exit code will be 1. If it was terminated by
  750. signal <em>N</em>, the exit code will be the negative value <em>-N</em>.</p>
  751. </dd></dl>
  752. <dl class="py attribute">
  753. <dt class="sig sig-object py" id="multiprocessing.Process.authkey">
  754. <span class="sig-name descname"><span class="pre">authkey</span></span><a class="headerlink" href="#multiprocessing.Process.authkey" title="Link to this definition">¶</a></dt>
  755. <dd><p>The process’s authentication key (a byte string).</p>
  756. <p>When <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> is initialized the main process is assigned a
  757. random string using <a class="reference internal" href="os.html#os.urandom" title="os.urandom"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.urandom()</span></code></a>.</p>
  758. <p>When a <a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> object is created, it will inherit the
  759. authentication key of its parent process, although this may be changed by
  760. setting <a class="reference internal" href="#multiprocessing.Process.authkey" title="multiprocessing.Process.authkey"><code class="xref py py-attr docutils literal notranslate"><span class="pre">authkey</span></code></a> to another byte string.</p>
  761. <p>See <a class="reference internal" href="#multiprocessing-auth-keys"><span class="std std-ref">Authentication keys</span></a>.</p>
  762. </dd></dl>
  763. <dl class="py attribute">
  764. <dt class="sig sig-object py" id="multiprocessing.Process.sentinel">
  765. <span class="sig-name descname"><span class="pre">sentinel</span></span><a class="headerlink" href="#multiprocessing.Process.sentinel" title="Link to this definition">¶</a></dt>
  766. <dd><p>A numeric handle of a system object which will become “ready” when
  767. the process ends.</p>
  768. <p>You can use this value if you want to wait on several events at
  769. once using <a class="reference internal" href="#multiprocessing.connection.wait" title="multiprocessing.connection.wait"><code class="xref py py-func docutils literal notranslate"><span class="pre">multiprocessing.connection.wait()</span></code></a>. Otherwise
  770. calling <a class="reference internal" href="#multiprocessing.Process.join" title="multiprocessing.Process.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> is simpler.</p>
  771. <p>On Windows, this is an OS handle usable with the <code class="docutils literal notranslate"><span class="pre">WaitForSingleObject</span></code>
  772. and <code class="docutils literal notranslate"><span class="pre">WaitForMultipleObjects</span></code> family of API calls. On POSIX, this is
  773. a file descriptor usable with primitives from the <a class="reference internal" href="select.html#module-select" title="select: Wait for I/O completion on multiple streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">select</span></code></a> module.</p>
  774. <div class="versionadded">
  775. <p><span class="versionmodified added">New in version 3.3.</span></p>
  776. </div>
  777. </dd></dl>
  778. <dl class="py method">
  779. <dt class="sig sig-object py" id="multiprocessing.Process.terminate">
  780. <span class="sig-name descname"><span class="pre">terminate</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Process.terminate" title="Link to this definition">¶</a></dt>
  781. <dd><p>Terminate the process. On POSIX this is done using the <a class="reference internal" href="signal.html#signal.SIGTERM" title="signal.SIGTERM"><code class="xref py py-const docutils literal notranslate"><span class="pre">SIGTERM</span></code></a> signal;
  782. on Windows <code class="xref c c-func docutils literal notranslate"><span class="pre">TerminateProcess()</span></code> is used. Note that exit handlers and
  783. finally clauses, etc., will not be executed.</p>
  784. <p>Note that descendant processes of the process will <em>not</em> be terminated –
  785. they will simply become orphaned.</p>
  786. <div class="admonition warning">
  787. <p class="admonition-title">Warning</p>
  788. <p>If this method is used when the associated process is using a pipe or
  789. queue then the pipe or queue is liable to become corrupted and may
  790. become unusable by other process. Similarly, if the process has
  791. acquired a lock or semaphore etc. then terminating it is liable to
  792. cause other processes to deadlock.</p>
  793. </div>
  794. </dd></dl>
  795. <dl class="py method">
  796. <dt class="sig sig-object py" id="multiprocessing.Process.kill">
  797. <span class="sig-name descname"><span class="pre">kill</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Process.kill" title="Link to this definition">¶</a></dt>
  798. <dd><p>Same as <a class="reference internal" href="#multiprocessing.Process.terminate" title="multiprocessing.Process.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">terminate()</span></code></a> but using the <code class="docutils literal notranslate"><span class="pre">SIGKILL</span></code> signal on POSIX.</p>
  799. <div class="versionadded">
  800. <p><span class="versionmodified added">New in version 3.7.</span></p>
  801. </div>
  802. </dd></dl>
  803. <dl class="py method">
  804. <dt class="sig sig-object py" id="multiprocessing.Process.close">
  805. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Process.close" title="Link to this definition">¶</a></dt>
  806. <dd><p>Close the <a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> object, releasing all resources associated
  807. with it. <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> is raised if the underlying process
  808. is still running. Once <a class="reference internal" href="#multiprocessing.Process.close" title="multiprocessing.Process.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> returns successfully, most
  809. other methods and attributes of the <a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> object will
  810. raise <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>.</p>
  811. <div class="versionadded">
  812. <p><span class="versionmodified added">New in version 3.7.</span></p>
  813. </div>
  814. </dd></dl>
  815. <p>Note that the <a class="reference internal" href="#multiprocessing.Process.start" title="multiprocessing.Process.start"><code class="xref py py-meth docutils literal notranslate"><span class="pre">start()</span></code></a>, <a class="reference internal" href="#multiprocessing.Process.join" title="multiprocessing.Process.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a>, <a class="reference internal" href="#multiprocessing.Process.is_alive" title="multiprocessing.Process.is_alive"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_alive()</span></code></a>,
  816. <a class="reference internal" href="#multiprocessing.Process.terminate" title="multiprocessing.Process.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">terminate()</span></code></a> and <a class="reference internal" href="#multiprocessing.Process.exitcode" title="multiprocessing.Process.exitcode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">exitcode</span></code></a> methods should only be called by
  817. the process that created the process object.</p>
  818. <p>Example usage of some of the methods of <a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a>:</p>
  819. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">multiprocessing</span><span class="o">,</span> <span class="nn">time</span><span class="o">,</span> <span class="nn">signal</span>
  820. <span class="gp">&gt;&gt;&gt; </span><span class="n">mp_context</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="s1">&#39;spawn&#39;</span><span class="p">)</span>
  821. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">mp_context</span><span class="o">.</span><span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="mi">1000</span><span class="p">,))</span>
  822. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">is_alive</span><span class="p">())</span>
  823. <span class="go">&lt;...Process ... initial&gt; False</span>
  824. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  825. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">is_alive</span><span class="p">())</span>
  826. <span class="go">&lt;...Process ... started&gt; True</span>
  827. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">terminate</span><span class="p">()</span>
  828. <span class="gp">&gt;&gt;&gt; </span><span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
  829. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">is_alive</span><span class="p">())</span>
  830. <span class="go">&lt;...Process ... stopped exitcode=-SIGTERM&gt; False</span>
  831. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">exitcode</span> <span class="o">==</span> <span class="o">-</span><span class="n">signal</span><span class="o">.</span><span class="n">SIGTERM</span>
  832. <span class="go">True</span>
  833. </pre></div>
  834. </div>
  835. </dd></dl>
  836. <dl class="py exception">
  837. <dt class="sig sig-object py" id="multiprocessing.ProcessError">
  838. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">ProcessError</span></span><a class="headerlink" href="#multiprocessing.ProcessError" title="Link to this definition">¶</a></dt>
  839. <dd><p>The base class of all <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> exceptions.</p>
  840. </dd></dl>
  841. <dl class="py exception">
  842. <dt class="sig sig-object py" id="multiprocessing.BufferTooShort">
  843. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">BufferTooShort</span></span><a class="headerlink" href="#multiprocessing.BufferTooShort" title="Link to this definition">¶</a></dt>
  844. <dd><p>Exception raised by <code class="xref py py-meth docutils literal notranslate"><span class="pre">Connection.recv_bytes_into()</span></code> when the supplied
  845. buffer object is too small for the message read.</p>
  846. <p>If <code class="docutils literal notranslate"><span class="pre">e</span></code> is an instance of <a class="reference internal" href="#multiprocessing.BufferTooShort" title="multiprocessing.BufferTooShort"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BufferTooShort</span></code></a> then <code class="docutils literal notranslate"><span class="pre">e.args[0]</span></code> will give
  847. the message as a byte string.</p>
  848. </dd></dl>
  849. <dl class="py exception">
  850. <dt class="sig sig-object py" id="multiprocessing.AuthenticationError">
  851. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">AuthenticationError</span></span><a class="headerlink" href="#multiprocessing.AuthenticationError" title="Link to this definition">¶</a></dt>
  852. <dd><p>Raised when there is an authentication error.</p>
  853. </dd></dl>
  854. <dl class="py exception">
  855. <dt class="sig sig-object py" id="multiprocessing.TimeoutError">
  856. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">TimeoutError</span></span><a class="headerlink" href="#multiprocessing.TimeoutError" title="Link to this definition">¶</a></dt>
  857. <dd><p>Raised by methods with a timeout when the timeout expires.</p>
  858. </dd></dl>
  859. </section>
  860. <section id="pipes-and-queues">
  861. <h3>Pipes and Queues<a class="headerlink" href="#pipes-and-queues" title="Link to this heading">¶</a></h3>
  862. <p>When using multiple processes, one generally uses message passing for
  863. communication between processes and avoids having to use any synchronization
  864. primitives like locks.</p>
  865. <p>For passing messages one can use <a class="reference internal" href="#multiprocessing.Pipe" title="multiprocessing.Pipe"><code class="xref py py-func docutils literal notranslate"><span class="pre">Pipe()</span></code></a> (for a connection between two
  866. processes) or a queue (which allows multiple producers and consumers).</p>
  867. <p>The <a class="reference internal" href="#multiprocessing.Queue" title="multiprocessing.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a>, <a class="reference internal" href="#multiprocessing.SimpleQueue" title="multiprocessing.SimpleQueue"><code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleQueue</span></code></a> and <a class="reference internal" href="#multiprocessing.JoinableQueue" title="multiprocessing.JoinableQueue"><code class="xref py py-class docutils literal notranslate"><span class="pre">JoinableQueue</span></code></a> types
  868. are multi-producer, multi-consumer <abbr title="first-in, first-out">FIFO</abbr>
  869. queues modelled on the <a class="reference internal" href="queue.html#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">queue.Queue</span></code></a> class in the
  870. standard library. They differ in that <a class="reference internal" href="#multiprocessing.Queue" title="multiprocessing.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a> lacks the
  871. <a class="reference internal" href="queue.html#queue.Queue.task_done" title="queue.Queue.task_done"><code class="xref py py-meth docutils literal notranslate"><span class="pre">task_done()</span></code></a> and <a class="reference internal" href="queue.html#queue.Queue.join" title="queue.Queue.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> methods introduced
  872. into Python 2.5’s <a class="reference internal" href="queue.html#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">queue.Queue</span></code></a> class.</p>
  873. <p>If you use <a class="reference internal" href="#multiprocessing.JoinableQueue" title="multiprocessing.JoinableQueue"><code class="xref py py-class docutils literal notranslate"><span class="pre">JoinableQueue</span></code></a> then you <strong>must</strong> call
  874. <a class="reference internal" href="#multiprocessing.JoinableQueue.task_done" title="multiprocessing.JoinableQueue.task_done"><code class="xref py py-meth docutils literal notranslate"><span class="pre">JoinableQueue.task_done()</span></code></a> for each task removed from the queue or else the
  875. semaphore used to count the number of unfinished tasks may eventually overflow,
  876. raising an exception.</p>
  877. <p>Note that one can also create a shared queue by using a manager object – see
  878. <a class="reference internal" href="#multiprocessing-managers"><span class="std std-ref">Managers</span></a>.</p>
  879. <div class="admonition note">
  880. <p class="admonition-title">Note</p>
  881. <p><a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> uses the usual <a class="reference internal" href="queue.html#queue.Empty" title="queue.Empty"><code class="xref py py-exc docutils literal notranslate"><span class="pre">queue.Empty</span></code></a> and
  882. <a class="reference internal" href="queue.html#queue.Full" title="queue.Full"><code class="xref py py-exc docutils literal notranslate"><span class="pre">queue.Full</span></code></a> exceptions to signal a timeout. They are not available in
  883. the <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> namespace so you need to import them from
  884. <a class="reference internal" href="queue.html#module-queue" title="queue: A synchronized queue class."><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code></a>.</p>
  885. </div>
  886. <div class="admonition note">
  887. <p class="admonition-title">Note</p>
  888. <p>When an object is put on a queue, the object is pickled and a
  889. background thread later flushes the pickled data to an underlying
  890. pipe. This has some consequences which are a little surprising,
  891. but should not cause any practical difficulties – if they really
  892. bother you then you can instead use a queue created with a
  893. <a class="reference internal" href="#multiprocessing-managers"><span class="std std-ref">manager</span></a>.</p>
  894. <ol class="arabic simple">
  895. <li><p>After putting an object on an empty queue there may be an
  896. infinitesimal delay before the queue’s <a class="reference internal" href="#multiprocessing.Queue.empty" title="multiprocessing.Queue.empty"><code class="xref py py-meth docutils literal notranslate"><span class="pre">empty()</span></code></a>
  897. method returns <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> and <a class="reference internal" href="#multiprocessing.Queue.get_nowait" title="multiprocessing.Queue.get_nowait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_nowait()</span></code></a> can
  898. return without raising <a class="reference internal" href="queue.html#queue.Empty" title="queue.Empty"><code class="xref py py-exc docutils literal notranslate"><span class="pre">queue.Empty</span></code></a>.</p></li>
  899. <li><p>If multiple processes are enqueuing objects, it is possible for
  900. the objects to be received at the other end out-of-order.
  901. However, objects enqueued by the same process will always be in
  902. the expected order with respect to each other.</p></li>
  903. </ol>
  904. </div>
  905. <div class="admonition warning">
  906. <p class="admonition-title">Warning</p>
  907. <p>If a process is killed using <a class="reference internal" href="#multiprocessing.Process.terminate" title="multiprocessing.Process.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Process.terminate()</span></code></a> or <a class="reference internal" href="os.html#os.kill" title="os.kill"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.kill()</span></code></a>
  908. while it is trying to use a <a class="reference internal" href="#multiprocessing.Queue" title="multiprocessing.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a>, then the data in the queue is
  909. likely to become corrupted. This may cause any other process to get an
  910. exception when it tries to use the queue later on.</p>
  911. </div>
  912. <div class="admonition warning">
  913. <p class="admonition-title">Warning</p>
  914. <p>As mentioned above, if a child process has put items on a queue (and it has
  915. not used <a class="reference internal" href="#multiprocessing.Queue.cancel_join_thread" title="multiprocessing.Queue.cancel_join_thread"><code class="xref py py-meth docutils literal notranslate"><span class="pre">JoinableQueue.cancel_join_thread</span></code></a>), then that process will
  916. not terminate until all buffered items have been flushed to the pipe.</p>
  917. <p>This means that if you try joining that process you may get a deadlock unless
  918. you are sure that all items which have been put on the queue have been
  919. consumed. Similarly, if the child process is non-daemonic then the parent
  920. process may hang on exit when it tries to join all its non-daemonic children.</p>
  921. <p>Note that a queue created using a manager does not have this issue. See
  922. <a class="reference internal" href="#multiprocessing-programming"><span class="std std-ref">Programming guidelines</span></a>.</p>
  923. </div>
  924. <p>For an example of the usage of queues for interprocess communication see
  925. <a class="reference internal" href="#multiprocessing-examples"><span class="std std-ref">Examples</span></a>.</p>
  926. <dl class="py function">
  927. <dt class="sig sig-object py" id="multiprocessing.Pipe">
  928. <span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">Pipe</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">duplex</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Pipe" title="Link to this definition">¶</a></dt>
  929. <dd><p>Returns a pair <code class="docutils literal notranslate"><span class="pre">(conn1,</span> <span class="pre">conn2)</span></code> of
  930. <a class="reference internal" href="#multiprocessing.connection.Connection" title="multiprocessing.connection.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> objects representing the
  931. ends of a pipe.</p>
  932. <p>If <em>duplex</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default) then the pipe is bidirectional. If
  933. <em>duplex</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code> then the pipe is unidirectional: <code class="docutils literal notranslate"><span class="pre">conn1</span></code> can only be
  934. used for receiving messages and <code class="docutils literal notranslate"><span class="pre">conn2</span></code> can only be used for sending
  935. messages.</p>
  936. </dd></dl>
  937. <dl class="py class">
  938. <dt class="sig sig-object py" id="multiprocessing.Queue">
  939. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">Queue</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">maxsize</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Queue" title="Link to this definition">¶</a></dt>
  940. <dd><p>Returns a process shared queue implemented using a pipe and a few
  941. locks/semaphores. When a process first puts an item on the queue a feeder
  942. thread is started which transfers objects from a buffer into the pipe.</p>
  943. <p>The usual <a class="reference internal" href="queue.html#queue.Empty" title="queue.Empty"><code class="xref py py-exc docutils literal notranslate"><span class="pre">queue.Empty</span></code></a> and <a class="reference internal" href="queue.html#queue.Full" title="queue.Full"><code class="xref py py-exc docutils literal notranslate"><span class="pre">queue.Full</span></code></a> exceptions from the
  944. standard library’s <a class="reference internal" href="queue.html#module-queue" title="queue: A synchronized queue class."><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code></a> module are raised to signal timeouts.</p>
  945. <p><a class="reference internal" href="#multiprocessing.Queue" title="multiprocessing.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a> implements all the methods of <a class="reference internal" href="queue.html#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">queue.Queue</span></code></a> except for
  946. <a class="reference internal" href="queue.html#queue.Queue.task_done" title="queue.Queue.task_done"><code class="xref py py-meth docutils literal notranslate"><span class="pre">task_done()</span></code></a> and <a class="reference internal" href="queue.html#queue.Queue.join" title="queue.Queue.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a>.</p>
  947. <dl class="py method">
  948. <dt class="sig sig-object py" id="multiprocessing.Queue.qsize">
  949. <span class="sig-name descname"><span class="pre">qsize</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Queue.qsize" title="Link to this definition">¶</a></dt>
  950. <dd><p>Return the approximate size of the queue. Because of
  951. multithreading/multiprocessing semantics, this number is not reliable.</p>
  952. <p>Note that this may raise <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> on platforms like
  953. macOS where <code class="docutils literal notranslate"><span class="pre">sem_getvalue()</span></code> is not implemented.</p>
  954. </dd></dl>
  955. <dl class="py method">
  956. <dt class="sig sig-object py" id="multiprocessing.Queue.empty">
  957. <span class="sig-name descname"><span class="pre">empty</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Queue.empty" title="Link to this definition">¶</a></dt>
  958. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the queue is empty, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise. Because of
  959. multithreading/multiprocessing semantics, this is not reliable.</p>
  960. </dd></dl>
  961. <dl class="py method">
  962. <dt class="sig sig-object py" id="multiprocessing.Queue.full">
  963. <span class="sig-name descname"><span class="pre">full</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Queue.full" title="Link to this definition">¶</a></dt>
  964. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the queue is full, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise. Because of
  965. multithreading/multiprocessing semantics, this is not reliable.</p>
  966. </dd></dl>
  967. <dl class="py method">
  968. <dt class="sig sig-object py" id="multiprocessing.Queue.put">
  969. <span class="sig-name descname"><span class="pre">put</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">block</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Queue.put" title="Link to this definition">¶</a></dt>
  970. <dd><p>Put obj into the queue. If the optional argument <em>block</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>
  971. (the default) and <em>timeout</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default), block if necessary until
  972. a free slot is available. If <em>timeout</em> is a positive number, it blocks at
  973. most <em>timeout</em> seconds and raises the <a class="reference internal" href="queue.html#queue.Full" title="queue.Full"><code class="xref py py-exc docutils literal notranslate"><span class="pre">queue.Full</span></code></a> exception if no
  974. free slot was available within that time. Otherwise (<em>block</em> is
  975. <code class="docutils literal notranslate"><span class="pre">False</span></code>), put an item on the queue if a free slot is immediately
  976. available, else raise the <a class="reference internal" href="queue.html#queue.Full" title="queue.Full"><code class="xref py py-exc docutils literal notranslate"><span class="pre">queue.Full</span></code></a> exception (<em>timeout</em> is
  977. ignored in that case).</p>
  978. <div class="versionchanged">
  979. <p><span class="versionmodified changed">Changed in version 3.8: </span>If the queue is closed, <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> is raised instead of
  980. <a class="reference internal" href="exceptions.html#AssertionError" title="AssertionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AssertionError</span></code></a>.</p>
  981. </div>
  982. </dd></dl>
  983. <dl class="py method">
  984. <dt class="sig sig-object py" id="multiprocessing.Queue.put_nowait">
  985. <span class="sig-name descname"><span class="pre">put_nowait</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Queue.put_nowait" title="Link to this definition">¶</a></dt>
  986. <dd><p>Equivalent to <code class="docutils literal notranslate"><span class="pre">put(obj,</span> <span class="pre">False)</span></code>.</p>
  987. </dd></dl>
  988. <dl class="py method">
  989. <dt class="sig sig-object py" id="multiprocessing.Queue.get">
  990. <span class="sig-name descname"><span class="pre">get</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">block</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Queue.get" title="Link to this definition">¶</a></dt>
  991. <dd><p>Remove and return an item from the queue. If optional args <em>block</em> is
  992. <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default) and <em>timeout</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default), block if
  993. necessary until an item is available. If <em>timeout</em> is a positive number,
  994. it blocks at most <em>timeout</em> seconds and raises the <a class="reference internal" href="queue.html#queue.Empty" title="queue.Empty"><code class="xref py py-exc docutils literal notranslate"><span class="pre">queue.Empty</span></code></a>
  995. exception if no item was available within that time. Otherwise (block is
  996. <code class="docutils literal notranslate"><span class="pre">False</span></code>), return an item if one is immediately available, else raise the
  997. <a class="reference internal" href="queue.html#queue.Empty" title="queue.Empty"><code class="xref py py-exc docutils literal notranslate"><span class="pre">queue.Empty</span></code></a> exception (<em>timeout</em> is ignored in that case).</p>
  998. <div class="versionchanged">
  999. <p><span class="versionmodified changed">Changed in version 3.8: </span>If the queue is closed, <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> is raised instead of
  1000. <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>.</p>
  1001. </div>
  1002. </dd></dl>
  1003. <dl class="py method">
  1004. <dt class="sig sig-object py" id="multiprocessing.Queue.get_nowait">
  1005. <span class="sig-name descname"><span class="pre">get_nowait</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Queue.get_nowait" title="Link to this definition">¶</a></dt>
  1006. <dd><p>Equivalent to <code class="docutils literal notranslate"><span class="pre">get(False)</span></code>.</p>
  1007. </dd></dl>
  1008. <p><a class="reference internal" href="#multiprocessing.Queue" title="multiprocessing.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">multiprocessing.Queue</span></code></a> has a few additional methods not found in
  1009. <a class="reference internal" href="queue.html#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">queue.Queue</span></code></a>. These methods are usually unnecessary for most
  1010. code:</p>
  1011. <dl class="py method">
  1012. <dt class="sig sig-object py" id="multiprocessing.Queue.close">
  1013. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Queue.close" title="Link to this definition">¶</a></dt>
  1014. <dd><p>Indicate that no more data will be put on this queue by the current
  1015. process. The background thread will quit once it has flushed all buffered
  1016. data to the pipe. This is called automatically when the queue is garbage
  1017. collected.</p>
  1018. </dd></dl>
  1019. <dl class="py method">
  1020. <dt class="sig sig-object py" id="multiprocessing.Queue.join_thread">
  1021. <span class="sig-name descname"><span class="pre">join_thread</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Queue.join_thread" title="Link to this definition">¶</a></dt>
  1022. <dd><p>Join the background thread. This can only be used after <a class="reference internal" href="#multiprocessing.Queue.close" title="multiprocessing.Queue.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> has
  1023. been called. It blocks until the background thread exits, ensuring that
  1024. all data in the buffer has been flushed to the pipe.</p>
  1025. <p>By default if a process is not the creator of the queue then on exit it
  1026. will attempt to join the queue’s background thread. The process can call
  1027. <a class="reference internal" href="#multiprocessing.Queue.cancel_join_thread" title="multiprocessing.Queue.cancel_join_thread"><code class="xref py py-meth docutils literal notranslate"><span class="pre">cancel_join_thread()</span></code></a> to make <a class="reference internal" href="#multiprocessing.Queue.join_thread" title="multiprocessing.Queue.join_thread"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join_thread()</span></code></a> do nothing.</p>
  1028. </dd></dl>
  1029. <dl class="py method">
  1030. <dt class="sig sig-object py" id="multiprocessing.Queue.cancel_join_thread">
  1031. <span class="sig-name descname"><span class="pre">cancel_join_thread</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Queue.cancel_join_thread" title="Link to this definition">¶</a></dt>
  1032. <dd><p>Prevent <a class="reference internal" href="#multiprocessing.Queue.join_thread" title="multiprocessing.Queue.join_thread"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join_thread()</span></code></a> from blocking. In particular, this prevents
  1033. the background thread from being joined automatically when the process
  1034. exits – see <a class="reference internal" href="#multiprocessing.Queue.join_thread" title="multiprocessing.Queue.join_thread"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join_thread()</span></code></a>.</p>
  1035. <p>A better name for this method might be
  1036. <code class="docutils literal notranslate"><span class="pre">allow_exit_without_flush()</span></code>. It is likely to cause enqueued
  1037. data to be lost, and you almost certainly will not need to use it.
  1038. It is really only there if you need the current process to exit
  1039. immediately without waiting to flush enqueued data to the
  1040. underlying pipe, and you don’t care about lost data.</p>
  1041. </dd></dl>
  1042. <div class="admonition note">
  1043. <p class="admonition-title">Note</p>
  1044. <p>This class’s functionality requires a functioning shared semaphore
  1045. implementation on the host operating system. Without one, the
  1046. functionality in this class will be disabled, and attempts to
  1047. instantiate a <a class="reference internal" href="#multiprocessing.Queue" title="multiprocessing.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a> will result in an <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a>. See
  1048. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3770">bpo-3770</a> for additional information. The same holds true for any
  1049. of the specialized queue types listed below.</p>
  1050. </div>
  1051. </dd></dl>
  1052. <dl class="py class">
  1053. <dt class="sig sig-object py" id="multiprocessing.SimpleQueue">
  1054. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">SimpleQueue</span></span><a class="headerlink" href="#multiprocessing.SimpleQueue" title="Link to this definition">¶</a></dt>
  1055. <dd><p>It is a simplified <a class="reference internal" href="#multiprocessing.Queue" title="multiprocessing.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a> type, very close to a locked <a class="reference internal" href="#multiprocessing.Pipe" title="multiprocessing.Pipe"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pipe</span></code></a>.</p>
  1056. <dl class="py method">
  1057. <dt class="sig sig-object py" id="multiprocessing.SimpleQueue.close">
  1058. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.SimpleQueue.close" title="Link to this definition">¶</a></dt>
  1059. <dd><p>Close the queue: release internal resources.</p>
  1060. <p>A queue must not be used anymore after it is closed. For example,
  1061. <a class="reference internal" href="#multiprocessing.SimpleQueue.get" title="multiprocessing.SimpleQueue.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code></a>, <a class="reference internal" href="#multiprocessing.SimpleQueue.put" title="multiprocessing.SimpleQueue.put"><code class="xref py py-meth docutils literal notranslate"><span class="pre">put()</span></code></a> and <a class="reference internal" href="#multiprocessing.SimpleQueue.empty" title="multiprocessing.SimpleQueue.empty"><code class="xref py py-meth docutils literal notranslate"><span class="pre">empty()</span></code></a> methods must no longer be
  1062. called.</p>
  1063. <div class="versionadded">
  1064. <p><span class="versionmodified added">New in version 3.9.</span></p>
  1065. </div>
  1066. </dd></dl>
  1067. <dl class="py method">
  1068. <dt class="sig sig-object py" id="multiprocessing.SimpleQueue.empty">
  1069. <span class="sig-name descname"><span class="pre">empty</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.SimpleQueue.empty" title="Link to this definition">¶</a></dt>
  1070. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the queue is empty, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
  1071. </dd></dl>
  1072. <dl class="py method">
  1073. <dt class="sig sig-object py" id="multiprocessing.SimpleQueue.get">
  1074. <span class="sig-name descname"><span class="pre">get</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.SimpleQueue.get" title="Link to this definition">¶</a></dt>
  1075. <dd><p>Remove and return an item from the queue.</p>
  1076. </dd></dl>
  1077. <dl class="py method">
  1078. <dt class="sig sig-object py" id="multiprocessing.SimpleQueue.put">
  1079. <span class="sig-name descname"><span class="pre">put</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">item</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.SimpleQueue.put" title="Link to this definition">¶</a></dt>
  1080. <dd><p>Put <em>item</em> into the queue.</p>
  1081. </dd></dl>
  1082. </dd></dl>
  1083. <dl class="py class">
  1084. <dt class="sig sig-object py" id="multiprocessing.JoinableQueue">
  1085. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">JoinableQueue</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">maxsize</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.JoinableQueue" title="Link to this definition">¶</a></dt>
  1086. <dd><p><a class="reference internal" href="#multiprocessing.JoinableQueue" title="multiprocessing.JoinableQueue"><code class="xref py py-class docutils literal notranslate"><span class="pre">JoinableQueue</span></code></a>, a <a class="reference internal" href="#multiprocessing.Queue" title="multiprocessing.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a> subclass, is a queue which
  1087. additionally has <a class="reference internal" href="#multiprocessing.JoinableQueue.task_done" title="multiprocessing.JoinableQueue.task_done"><code class="xref py py-meth docutils literal notranslate"><span class="pre">task_done()</span></code></a> and <a class="reference internal" href="#multiprocessing.JoinableQueue.join" title="multiprocessing.JoinableQueue.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> methods.</p>
  1088. <dl class="py method">
  1089. <dt class="sig sig-object py" id="multiprocessing.JoinableQueue.task_done">
  1090. <span class="sig-name descname"><span class="pre">task_done</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.JoinableQueue.task_done" title="Link to this definition">¶</a></dt>
  1091. <dd><p>Indicate that a formerly enqueued task is complete. Used by queue
  1092. consumers. For each <a class="reference internal" href="#multiprocessing.Queue.get" title="multiprocessing.Queue.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code></a> used to fetch a task, a subsequent
  1093. call to <a class="reference internal" href="#multiprocessing.JoinableQueue.task_done" title="multiprocessing.JoinableQueue.task_done"><code class="xref py py-meth docutils literal notranslate"><span class="pre">task_done()</span></code></a> tells the queue that the processing on the task
  1094. is complete.</p>
  1095. <p>If a <a class="reference internal" href="queue.html#queue.Queue.join" title="queue.Queue.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> is currently blocking, it will resume when all
  1096. items have been processed (meaning that a <a class="reference internal" href="#multiprocessing.JoinableQueue.task_done" title="multiprocessing.JoinableQueue.task_done"><code class="xref py py-meth docutils literal notranslate"><span class="pre">task_done()</span></code></a> call was
  1097. received for every item that had been <a class="reference internal" href="#multiprocessing.Queue.put" title="multiprocessing.Queue.put"><code class="xref py py-meth docutils literal notranslate"><span class="pre">put()</span></code></a> into the queue).</p>
  1098. <p>Raises 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> if called more times than there were items
  1099. placed in the queue.</p>
  1100. </dd></dl>
  1101. <dl class="py method">
  1102. <dt class="sig sig-object py" id="multiprocessing.JoinableQueue.join">
  1103. <span class="sig-name descname"><span class="pre">join</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.JoinableQueue.join" title="Link to this definition">¶</a></dt>
  1104. <dd><p>Block until all items in the queue have been gotten and processed.</p>
  1105. <p>The count of unfinished tasks goes up whenever an item is added to the
  1106. queue. The count goes down whenever a consumer calls
  1107. <a class="reference internal" href="#multiprocessing.JoinableQueue.task_done" title="multiprocessing.JoinableQueue.task_done"><code class="xref py py-meth docutils literal notranslate"><span class="pre">task_done()</span></code></a> to indicate that the item was retrieved and all work on
  1108. it is complete. When the count of unfinished tasks drops to zero,
  1109. <a class="reference internal" href="queue.html#queue.Queue.join" title="queue.Queue.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> unblocks.</p>
  1110. </dd></dl>
  1111. </dd></dl>
  1112. </section>
  1113. <section id="miscellaneous">
  1114. <h3>Miscellaneous<a class="headerlink" href="#miscellaneous" title="Link to this heading">¶</a></h3>
  1115. <dl class="py function">
  1116. <dt class="sig sig-object py" id="multiprocessing.active_children">
  1117. <span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">active_children</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.active_children" title="Link to this definition">¶</a></dt>
  1118. <dd><p>Return list of all live children of the current process.</p>
  1119. <p>Calling this has the side effect of “joining” any processes which have
  1120. already finished.</p>
  1121. </dd></dl>
  1122. <dl class="py function">
  1123. <dt class="sig sig-object py" id="multiprocessing.cpu_count">
  1124. <span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">cpu_count</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.cpu_count" title="Link to this definition">¶</a></dt>
  1125. <dd><p>Return the number of CPUs in the system.</p>
  1126. <p>This number is not equivalent to the number of CPUs the current process can
  1127. use. The number of usable CPUs can be obtained with
  1128. <code class="docutils literal notranslate"><span class="pre">len(os.sched_getaffinity(0))</span></code></p>
  1129. <p>When the number of CPUs cannot be determined a <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>
  1130. is raised.</p>
  1131. <div class="admonition seealso">
  1132. <p class="admonition-title">See also</p>
  1133. <p><a class="reference internal" href="os.html#os.cpu_count" title="os.cpu_count"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.cpu_count()</span></code></a></p>
  1134. </div>
  1135. </dd></dl>
  1136. <dl class="py function">
  1137. <dt class="sig sig-object py" id="multiprocessing.current_process">
  1138. <span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">current_process</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.current_process" title="Link to this definition">¶</a></dt>
  1139. <dd><p>Return the <a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> object corresponding to the current process.</p>
  1140. <p>An analogue of <a class="reference internal" href="threading.html#threading.current_thread" title="threading.current_thread"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.current_thread()</span></code></a>.</p>
  1141. </dd></dl>
  1142. <dl class="py function">
  1143. <dt class="sig sig-object py" id="multiprocessing.parent_process">
  1144. <span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">parent_process</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.parent_process" title="Link to this definition">¶</a></dt>
  1145. <dd><p>Return the <a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> object corresponding to the parent process of
  1146. the <a class="reference internal" href="#multiprocessing.current_process" title="multiprocessing.current_process"><code class="xref py py-func docutils literal notranslate"><span class="pre">current_process()</span></code></a>. For the main process, <code class="docutils literal notranslate"><span class="pre">parent_process</span></code> will
  1147. be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1148. <div class="versionadded">
  1149. <p><span class="versionmodified added">New in version 3.8.</span></p>
  1150. </div>
  1151. </dd></dl>
  1152. <dl class="py function">
  1153. <dt class="sig sig-object py" id="multiprocessing.freeze_support">
  1154. <span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">freeze_support</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.freeze_support" title="Link to this definition">¶</a></dt>
  1155. <dd><p>Add support for when a program which uses <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> has been
  1156. frozen to produce a Windows executable. (Has been tested with <strong>py2exe</strong>,
  1157. <strong>PyInstaller</strong> and <strong>cx_Freeze</strong>.)</p>
  1158. <p>One needs to call this function straight after the <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">__name__</span> <span class="pre">==</span>
  1159. <span class="pre">'__main__'</span></code> line of the main module. For example:</p>
  1160. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">freeze_support</span>
  1161. <span class="k">def</span> <span class="nf">f</span><span class="p">():</span>
  1162. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;hello world!&#39;</span><span class="p">)</span>
  1163. <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>
  1164. <span class="n">freeze_support</span><span class="p">()</span>
  1165. <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">f</span><span class="p">)</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  1166. </pre></div>
  1167. </div>
  1168. <p>If the <code class="docutils literal notranslate"><span class="pre">freeze_support()</span></code> line is omitted then trying to run the frozen
  1169. executable will 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>
  1170. <p>Calling <code class="docutils literal notranslate"><span class="pre">freeze_support()</span></code> has no effect when invoked on any operating
  1171. system other than Windows. In addition, if the module is being run
  1172. normally by the Python interpreter on Windows (the program has not been
  1173. frozen), then <code class="docutils literal notranslate"><span class="pre">freeze_support()</span></code> has no effect.</p>
  1174. </dd></dl>
  1175. <dl class="py function">
  1176. <dt class="sig sig-object py" id="multiprocessing.get_all_start_methods">
  1177. <span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">get_all_start_methods</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.get_all_start_methods" title="Link to this definition">¶</a></dt>
  1178. <dd><p>Returns a list of the supported start methods, the first of which
  1179. is the default. The possible start methods are <code class="docutils literal notranslate"><span class="pre">'fork'</span></code>,
  1180. <code class="docutils literal notranslate"><span class="pre">'spawn'</span></code> and <code class="docutils literal notranslate"><span class="pre">'forkserver'</span></code>. Not all platforms support all
  1181. methods. See <a class="reference internal" href="#multiprocessing-start-methods"><span class="std std-ref">Contexts and start methods</span></a>.</p>
  1182. <div class="versionadded">
  1183. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1184. </div>
  1185. </dd></dl>
  1186. <dl class="py function">
  1187. <dt class="sig sig-object py" id="multiprocessing.get_context">
  1188. <span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">get_context</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">method</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="#multiprocessing.get_context" title="Link to this definition">¶</a></dt>
  1189. <dd><p>Return a context object which has the same attributes as the
  1190. <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> module.</p>
  1191. <p>If <em>method</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> then the default context is returned.
  1192. Otherwise <em>method</em> should be <code class="docutils literal notranslate"><span class="pre">'fork'</span></code>, <code class="docutils literal notranslate"><span class="pre">'spawn'</span></code>,
  1193. <code class="docutils literal notranslate"><span class="pre">'forkserver'</span></code>. <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> is raised if the specified
  1194. start method is not available. See <a class="reference internal" href="#multiprocessing-start-methods"><span class="std std-ref">Contexts and start methods</span></a>.</p>
  1195. <div class="versionadded">
  1196. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1197. </div>
  1198. </dd></dl>
  1199. <dl class="py function">
  1200. <dt class="sig sig-object py" id="multiprocessing.get_start_method">
  1201. <span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">get_start_method</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">allow_none</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="#multiprocessing.get_start_method" title="Link to this definition">¶</a></dt>
  1202. <dd><p>Return the name of start method used for starting processes.</p>
  1203. <p>If the start method has not been fixed and <em>allow_none</em> is false,
  1204. then the start method is fixed to the default and the name is
  1205. returned. If the start method has not been fixed and <em>allow_none</em>
  1206. is true then <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned.</p>
  1207. <p>The return value can be <code class="docutils literal notranslate"><span class="pre">'fork'</span></code>, <code class="docutils literal notranslate"><span class="pre">'spawn'</span></code>, <code class="docutils literal notranslate"><span class="pre">'forkserver'</span></code>
  1208. or <code class="docutils literal notranslate"><span class="pre">None</span></code>. See <a class="reference internal" href="#multiprocessing-start-methods"><span class="std std-ref">Contexts and start methods</span></a>.</p>
  1209. <div class="versionadded">
  1210. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1211. </div>
  1212. <div class="versionchanged">
  1213. <p><span class="versionmodified changed">Changed in version 3.8: </span>On macOS, the <em>spawn</em> start method is now the default. The <em>fork</em> start
  1214. method should be considered unsafe as it can lead to crashes of the
  1215. subprocess. See <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=33725">bpo-33725</a>.</p>
  1216. </div>
  1217. </dd></dl>
  1218. <dl class="py function">
  1219. <dt class="sig sig-object py" id="multiprocessing.set_executable">
  1220. <span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">set_executable</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">executable</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.set_executable" title="Link to this definition">¶</a></dt>
  1221. <dd><p>Set the path of the Python interpreter to use when starting a child process.
  1222. (By default <a class="reference internal" href="sys.html#sys.executable" title="sys.executable"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.executable</span></code></a> is used). Embedders will probably need to
  1223. do some thing like</p>
  1224. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">set_executable</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">exec_prefix</span><span class="p">,</span> <span class="s1">&#39;pythonw.exe&#39;</span><span class="p">))</span>
  1225. </pre></div>
  1226. </div>
  1227. <p>before they can create child processes.</p>
  1228. <div class="versionchanged">
  1229. <p><span class="versionmodified changed">Changed in version 3.4: </span>Now supported on POSIX when the <code class="docutils literal notranslate"><span class="pre">'spawn'</span></code> start method is used.</p>
  1230. </div>
  1231. <div class="versionchanged">
  1232. <p><span class="versionmodified changed">Changed in version 3.11: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  1233. </div>
  1234. </dd></dl>
  1235. <dl class="py function">
  1236. <dt class="sig sig-object py" id="multiprocessing.set_forkserver_preload">
  1237. <span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">set_forkserver_preload</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module_names</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.set_forkserver_preload" title="Link to this definition">¶</a></dt>
  1238. <dd><p>Set a list of module names for the forkserver main process to attempt to
  1239. import so that their already imported state is inherited by forked
  1240. processes. Any <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> when doing so is silently ignored.
  1241. This can be used as a performance enhancement to avoid repeated work
  1242. in every process.</p>
  1243. <p>For this to work, it must be called before the forkserver process has been
  1244. launched (before creating a <code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code> or starting a <a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a>).</p>
  1245. <p>Only meaningful when using the <code class="docutils literal notranslate"><span class="pre">'forkserver'</span></code> start method.
  1246. See <a class="reference internal" href="#multiprocessing-start-methods"><span class="std std-ref">Contexts and start methods</span></a>.</p>
  1247. <div class="versionadded">
  1248. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1249. </div>
  1250. </dd></dl>
  1251. <dl class="py function">
  1252. <dt class="sig sig-object py" id="multiprocessing.set_start_method">
  1253. <span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">set_start_method</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">method</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">force</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="#multiprocessing.set_start_method" title="Link to this definition">¶</a></dt>
  1254. <dd><p>Set the method which should be used to start child processes.
  1255. The <em>method</em> argument can be <code class="docutils literal notranslate"><span class="pre">'fork'</span></code>, <code class="docutils literal notranslate"><span class="pre">'spawn'</span></code> or <code class="docutils literal notranslate"><span class="pre">'forkserver'</span></code>.
  1256. Raises <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> if the start method has already been set and <em>force</em>
  1257. is not <code class="docutils literal notranslate"><span class="pre">True</span></code>. If <em>method</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> and <em>force</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> then the start
  1258. method is set to <code class="docutils literal notranslate"><span class="pre">None</span></code>. If <em>method</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> and <em>force</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>
  1259. then the context is set to the default context.</p>
  1260. <p>Note that this should be called at most once, and it should be
  1261. protected inside the <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">__name__</span> <span class="pre">==</span> <span class="pre">'__main__'</span></code> clause of the
  1262. main module.</p>
  1263. <p>See <a class="reference internal" href="#multiprocessing-start-methods"><span class="std std-ref">Contexts and start methods</span></a>.</p>
  1264. <div class="versionadded">
  1265. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1266. </div>
  1267. </dd></dl>
  1268. <div class="admonition note">
  1269. <p class="admonition-title">Note</p>
  1270. <p><a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> contains no analogues of
  1271. <a class="reference internal" href="threading.html#threading.active_count" title="threading.active_count"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.active_count()</span></code></a>, <a class="reference internal" href="threading.html#threading.enumerate" title="threading.enumerate"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.enumerate()</span></code></a>,
  1272. <a class="reference internal" href="threading.html#threading.settrace" title="threading.settrace"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.settrace()</span></code></a>, <a class="reference internal" href="threading.html#threading.setprofile" title="threading.setprofile"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.setprofile()</span></code></a>,
  1273. <a class="reference internal" href="threading.html#threading.Timer" title="threading.Timer"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Timer</span></code></a>, or <a class="reference internal" href="threading.html#threading.local" title="threading.local"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.local</span></code></a>.</p>
  1274. </div>
  1275. </section>
  1276. <section id="connection-objects">
  1277. <h3>Connection Objects<a class="headerlink" href="#connection-objects" title="Link to this heading">¶</a></h3>
  1278. <p>Connection objects allow the sending and receiving of picklable objects or
  1279. strings. They can be thought of as message oriented connected sockets.</p>
  1280. <p>Connection objects are usually created using
  1281. <a class="reference internal" href="#multiprocessing.Pipe" title="multiprocessing.Pipe"><code class="xref py py-func docutils literal notranslate"><span class="pre">Pipe</span></code></a> – see also
  1282. <a class="reference internal" href="#multiprocessing-listeners-clients"><span class="std std-ref">Listeners and Clients</span></a>.</p>
  1283. <dl class="py class">
  1284. <dt class="sig sig-object py" id="multiprocessing.connection.Connection">
  1285. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.connection.</span></span><span class="sig-name descname"><span class="pre">Connection</span></span><a class="headerlink" href="#multiprocessing.connection.Connection" title="Link to this definition">¶</a></dt>
  1286. <dd><dl class="py method">
  1287. <dt class="sig sig-object py" id="multiprocessing.connection.Connection.send">
  1288. <span class="sig-name descname"><span class="pre">send</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.connection.Connection.send" title="Link to this definition">¶</a></dt>
  1289. <dd><p>Send an object to the other end of the connection which should be read
  1290. using <a class="reference internal" href="#multiprocessing.connection.Connection.recv" title="multiprocessing.connection.Connection.recv"><code class="xref py py-meth docutils literal notranslate"><span class="pre">recv()</span></code></a>.</p>
  1291. <p>The object must be picklable. Very large pickles (approximately 32 MiB+,
  1292. though it depends on the OS) may raise 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> exception.</p>
  1293. </dd></dl>
  1294. <dl class="py method">
  1295. <dt class="sig sig-object py" id="multiprocessing.connection.Connection.recv">
  1296. <span class="sig-name descname"><span class="pre">recv</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.connection.Connection.recv" title="Link to this definition">¶</a></dt>
  1297. <dd><p>Return an object sent from the other end of the connection using
  1298. <a class="reference internal" href="#multiprocessing.connection.Connection.send" title="multiprocessing.connection.Connection.send"><code class="xref py py-meth docutils literal notranslate"><span class="pre">send()</span></code></a>. Blocks until there is something to receive. Raises
  1299. <a class="reference internal" href="exceptions.html#EOFError" title="EOFError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EOFError</span></code></a> if there is nothing left to receive
  1300. and the other end was closed.</p>
  1301. </dd></dl>
  1302. <dl class="py method">
  1303. <dt class="sig sig-object py" id="multiprocessing.connection.Connection.fileno">
  1304. <span class="sig-name descname"><span class="pre">fileno</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.connection.Connection.fileno" title="Link to this definition">¶</a></dt>
  1305. <dd><p>Return the file descriptor or handle used by the connection.</p>
  1306. </dd></dl>
  1307. <dl class="py method">
  1308. <dt class="sig sig-object py" id="multiprocessing.connection.Connection.close">
  1309. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.connection.Connection.close" title="Link to this definition">¶</a></dt>
  1310. <dd><p>Close the connection.</p>
  1311. <p>This is called automatically when the connection is garbage collected.</p>
  1312. </dd></dl>
  1313. <dl class="py method">
  1314. <dt class="sig sig-object py" id="multiprocessing.connection.Connection.poll">
  1315. <span class="sig-name descname"><span class="pre">poll</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">timeout</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.connection.Connection.poll" title="Link to this definition">¶</a></dt>
  1316. <dd><p>Return whether there is any data available to be read.</p>
  1317. <p>If <em>timeout</em> is not specified then it will return immediately. If
  1318. <em>timeout</em> is a number then this specifies the maximum time in seconds to
  1319. block. If <em>timeout</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> then an infinite timeout is used.</p>
  1320. <p>Note that multiple connection objects may be polled at once by
  1321. using <a class="reference internal" href="#multiprocessing.connection.wait" title="multiprocessing.connection.wait"><code class="xref py py-func docutils literal notranslate"><span class="pre">multiprocessing.connection.wait()</span></code></a>.</p>
  1322. </dd></dl>
  1323. <dl class="py method">
  1324. <dt class="sig sig-object py" id="multiprocessing.connection.Connection.send_bytes">
  1325. <span class="sig-name descname"><span class="pre">send_bytes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">buffer</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">size</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.connection.Connection.send_bytes" title="Link to this definition">¶</a></dt>
  1326. <dd><p>Send byte data from a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> as a complete message.</p>
  1327. <p>If <em>offset</em> is given then data is read from that position in <em>buffer</em>. If
  1328. <em>size</em> is given then that many bytes will be read from buffer. Very large
  1329. buffers (approximately 32 MiB+, though it depends on the OS) may raise a
  1330. <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> exception</p>
  1331. </dd></dl>
  1332. <dl class="py method">
  1333. <dt class="sig sig-object py" id="multiprocessing.connection.Connection.recv_bytes">
  1334. <span class="sig-name descname"><span class="pre">recv_bytes</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">maxlength</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.connection.Connection.recv_bytes" title="Link to this definition">¶</a></dt>
  1335. <dd><p>Return a complete message of byte data sent from the other end of the
  1336. connection as a string. Blocks until there is something to receive.
  1337. Raises <a class="reference internal" href="exceptions.html#EOFError" title="EOFError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EOFError</span></code></a> if there is nothing left
  1338. to receive and the other end has closed.</p>
  1339. <p>If <em>maxlength</em> is specified and the message is longer than <em>maxlength</em>
  1340. then <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised and the connection will no longer be
  1341. readable.</p>
  1342. <div class="versionchanged">
  1343. <p><span class="versionmodified changed">Changed in version 3.3: </span>This function used to raise <a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a>, which is now an
  1344. alias of <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>.</p>
  1345. </div>
  1346. </dd></dl>
  1347. <dl class="py method">
  1348. <dt class="sig sig-object py" id="multiprocessing.connection.Connection.recv_bytes_into">
  1349. <span class="sig-name descname"><span class="pre">recv_bytes_into</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">buffer</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.connection.Connection.recv_bytes_into" title="Link to this definition">¶</a></dt>
  1350. <dd><p>Read into <em>buffer</em> a complete message of byte data sent from the other end
  1351. of the connection and return the number of bytes in the message. Blocks
  1352. until there is something to receive. Raises
  1353. <a class="reference internal" href="exceptions.html#EOFError" title="EOFError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EOFError</span></code></a> if there is nothing left to receive and the other end was
  1354. closed.</p>
  1355. <p><em>buffer</em> must be a writable <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>. If
  1356. <em>offset</em> is given then the message will be written into the buffer from
  1357. that position. Offset must be a non-negative integer less than the
  1358. length of <em>buffer</em> (in bytes).</p>
  1359. <p>If the buffer is too short then a <code class="xref py py-exc docutils literal notranslate"><span class="pre">BufferTooShort</span></code> exception is
  1360. raised and the complete message is available as <code class="docutils literal notranslate"><span class="pre">e.args[0]</span></code> where <code class="docutils literal notranslate"><span class="pre">e</span></code>
  1361. is the exception instance.</p>
  1362. </dd></dl>
  1363. <div class="versionchanged">
  1364. <p><span class="versionmodified changed">Changed in version 3.3: </span>Connection objects themselves can now be transferred between processes
  1365. using <a class="reference internal" href="#multiprocessing.connection.Connection.send" title="multiprocessing.connection.Connection.send"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Connection.send()</span></code></a> and <a class="reference internal" href="#multiprocessing.connection.Connection.recv" title="multiprocessing.connection.Connection.recv"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Connection.recv()</span></code></a>.</p>
  1366. <p>Connection objects also now support the context management protocol – see
  1367. <a class="reference internal" href="stdtypes.html#typecontextmanager"><span class="std std-ref">Context Manager Types</span></a>. <a class="reference internal" href="stdtypes.html#contextmanager.__enter__" title="contextmanager.__enter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__enter__()</span></code></a> returns the
  1368. connection object, and <a class="reference internal" href="stdtypes.html#contextmanager.__exit__" title="contextmanager.__exit__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__exit__()</span></code></a> calls <a class="reference internal" href="#multiprocessing.connection.Connection.close" title="multiprocessing.connection.Connection.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a>.</p>
  1369. </div>
  1370. </dd></dl>
  1371. <p>For example:</p>
  1372. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Pipe</span>
  1373. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">Pipe</span><span class="p">()</span>
  1374. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">send</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;hello&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">])</span>
  1375. <span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">recv</span><span class="p">()</span>
  1376. <span class="go">[1, &#39;hello&#39;, None]</span>
  1377. <span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">send_bytes</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;thank you&#39;</span><span class="p">)</span>
  1378. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">recv_bytes</span><span class="p">()</span>
  1379. <span class="go">b&#39;thank you&#39;</span>
  1380. <span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">array</span>
  1381. <span class="gp">&gt;&gt;&gt; </span><span class="n">arr1</span> <span class="o">=</span> <span class="n">array</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="s1">&#39;i&#39;</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span>
  1382. <span class="gp">&gt;&gt;&gt; </span><span class="n">arr2</span> <span class="o">=</span> <span class="n">array</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="s1">&#39;i&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="mi">10</span><span class="p">)</span>
  1383. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">send_bytes</span><span class="p">(</span><span class="n">arr1</span><span class="p">)</span>
  1384. <span class="gp">&gt;&gt;&gt; </span><span class="n">count</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">recv_bytes_into</span><span class="p">(</span><span class="n">arr2</span><span class="p">)</span>
  1385. <span class="gp">&gt;&gt;&gt; </span><span class="k">assert</span> <span class="n">count</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">arr1</span><span class="p">)</span> <span class="o">*</span> <span class="n">arr1</span><span class="o">.</span><span class="n">itemsize</span>
  1386. <span class="gp">&gt;&gt;&gt; </span><span class="n">arr2</span>
  1387. <span class="go">array(&#39;i&#39;, [0, 1, 2, 3, 4, 0, 0, 0, 0, 0])</span>
  1388. </pre></div>
  1389. </div>
  1390. <div class="admonition warning" id="multiprocessing-recv-pickle-security">
  1391. <p class="admonition-title">Warning</p>
  1392. <p>The <a class="reference internal" href="#multiprocessing.connection.Connection.recv" title="multiprocessing.connection.Connection.recv"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Connection.recv()</span></code></a> method automatically unpickles the data it
  1393. receives, which can be a security risk unless you can trust the process
  1394. which sent the message.</p>
  1395. <p>Therefore, unless the connection object was produced using <code class="xref py py-func docutils literal notranslate"><span class="pre">Pipe()</span></code> you
  1396. should only use the <a class="reference internal" href="#multiprocessing.connection.Connection.recv" title="multiprocessing.connection.Connection.recv"><code class="xref py py-meth docutils literal notranslate"><span class="pre">recv()</span></code></a> and <a class="reference internal" href="#multiprocessing.connection.Connection.send" title="multiprocessing.connection.Connection.send"><code class="xref py py-meth docutils literal notranslate"><span class="pre">send()</span></code></a>
  1397. methods after performing some sort of authentication. See
  1398. <a class="reference internal" href="#multiprocessing-auth-keys"><span class="std std-ref">Authentication keys</span></a>.</p>
  1399. </div>
  1400. <div class="admonition warning">
  1401. <p class="admonition-title">Warning</p>
  1402. <p>If a process is killed while it is trying to read or write to a pipe then
  1403. the data in the pipe is likely to become corrupted, because it may become
  1404. impossible to be sure where the message boundaries lie.</p>
  1405. </div>
  1406. </section>
  1407. <section id="synchronization-primitives">
  1408. <h3>Synchronization primitives<a class="headerlink" href="#synchronization-primitives" title="Link to this heading">¶</a></h3>
  1409. <p>Generally synchronization primitives are not as necessary in a multiprocess
  1410. program as they are in a multithreaded program. See the documentation for
  1411. <a class="reference internal" href="threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module.</p>
  1412. <p>Note that one can also create synchronization primitives by using a manager
  1413. object – see <a class="reference internal" href="#multiprocessing-managers"><span class="std std-ref">Managers</span></a>.</p>
  1414. <dl class="py class">
  1415. <dt class="sig sig-object py" id="multiprocessing.Barrier">
  1416. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">Barrier</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">parties</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">action</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Barrier" title="Link to this definition">¶</a></dt>
  1417. <dd><p>A barrier object: a clone of <a class="reference internal" href="threading.html#threading.Barrier" title="threading.Barrier"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Barrier</span></code></a>.</p>
  1418. <div class="versionadded">
  1419. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1420. </div>
  1421. </dd></dl>
  1422. <dl class="py class">
  1423. <dt class="sig sig-object py" id="multiprocessing.BoundedSemaphore">
  1424. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">BoundedSemaphore</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.BoundedSemaphore" title="Link to this definition">¶</a></dt>
  1425. <dd><p>A bounded semaphore object: a close analog of
  1426. <a class="reference internal" href="threading.html#threading.BoundedSemaphore" title="threading.BoundedSemaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.BoundedSemaphore</span></code></a>.</p>
  1427. <p>A solitary difference from its close analog exists: its <code class="docutils literal notranslate"><span class="pre">acquire</span></code> method’s
  1428. first argument is named <em>block</em>, as is consistent with <a class="reference internal" href="#multiprocessing.Lock.acquire" title="multiprocessing.Lock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Lock.acquire()</span></code></a>.</p>
  1429. <div class="admonition note">
  1430. <p class="admonition-title">Note</p>
  1431. <p>On macOS, this is indistinguishable from <a class="reference internal" href="#multiprocessing.Semaphore" title="multiprocessing.Semaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">Semaphore</span></code></a> because
  1432. <code class="docutils literal notranslate"><span class="pre">sem_getvalue()</span></code> is not implemented on that platform.</p>
  1433. </div>
  1434. </dd></dl>
  1435. <dl class="py class">
  1436. <dt class="sig sig-object py" id="multiprocessing.Condition">
  1437. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">Condition</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">lock</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Condition" title="Link to this definition">¶</a></dt>
  1438. <dd><p>A condition variable: an alias for <a class="reference internal" href="threading.html#threading.Condition" title="threading.Condition"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Condition</span></code></a>.</p>
  1439. <p>If <em>lock</em> is specified then it should be a <a class="reference internal" href="#multiprocessing.Lock" title="multiprocessing.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">Lock</span></code></a> or <a class="reference internal" href="#multiprocessing.RLock" title="multiprocessing.RLock"><code class="xref py py-class docutils literal notranslate"><span class="pre">RLock</span></code></a>
  1440. object from <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a>.</p>
  1441. <div class="versionchanged">
  1442. <p><span class="versionmodified changed">Changed in version 3.3: </span>The <a class="reference internal" href="threading.html#threading.Condition.wait_for" title="threading.Condition.wait_for"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait_for()</span></code></a> method was added.</p>
  1443. </div>
  1444. </dd></dl>
  1445. <dl class="py class">
  1446. <dt class="sig sig-object py" id="multiprocessing.Event">
  1447. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">Event</span></span><a class="headerlink" href="#multiprocessing.Event" title="Link to this definition">¶</a></dt>
  1448. <dd><p>A clone of <a class="reference internal" href="threading.html#threading.Event" title="threading.Event"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Event</span></code></a>.</p>
  1449. </dd></dl>
  1450. <dl class="py class">
  1451. <dt class="sig sig-object py" id="multiprocessing.Lock">
  1452. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">Lock</span></span><a class="headerlink" href="#multiprocessing.Lock" title="Link to this definition">¶</a></dt>
  1453. <dd><p>A non-recursive lock object: a close analog of <a class="reference internal" href="threading.html#threading.Lock" title="threading.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Lock</span></code></a>.
  1454. Once a process or thread has acquired a lock, subsequent attempts to
  1455. acquire it from any process or thread will block until it is released;
  1456. any process or thread may release it. The concepts and behaviors of
  1457. <a class="reference internal" href="threading.html#threading.Lock" title="threading.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Lock</span></code></a> as it applies to threads are replicated here in
  1458. <a class="reference internal" href="#multiprocessing.Lock" title="multiprocessing.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">multiprocessing.Lock</span></code></a> as it applies to either processes or threads,
  1459. except as noted.</p>
  1460. <p>Note that <a class="reference internal" href="#multiprocessing.Lock" title="multiprocessing.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">Lock</span></code></a> is actually a factory function which returns an
  1461. instance of <code class="docutils literal notranslate"><span class="pre">multiprocessing.synchronize.Lock</span></code> initialized with a
  1462. default context.</p>
  1463. <p><a class="reference internal" href="#multiprocessing.Lock" title="multiprocessing.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">Lock</span></code></a> supports the <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> protocol and thus may be
  1464. used in <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> statements.</p>
  1465. <dl class="py method">
  1466. <dt class="sig sig-object py" id="multiprocessing.Lock.acquire">
  1467. <span class="sig-name descname"><span class="pre">acquire</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">block</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="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="#multiprocessing.Lock.acquire" title="Link to this definition">¶</a></dt>
  1468. <dd><p>Acquire a lock, blocking or non-blocking.</p>
  1469. <p>With the <em>block</em> argument set to <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default), the method call
  1470. will block until the lock is in an unlocked state, then set it to locked
  1471. and return <code class="docutils literal notranslate"><span class="pre">True</span></code>. Note that the name of this first argument differs
  1472. from that in <a class="reference internal" href="threading.html#threading.Lock.acquire" title="threading.Lock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">threading.Lock.acquire()</span></code></a>.</p>
  1473. <p>With the <em>block</em> argument set to <code class="docutils literal notranslate"><span class="pre">False</span></code>, the method call does not
  1474. block. If the lock is currently in a locked state, return <code class="docutils literal notranslate"><span class="pre">False</span></code>;
  1475. otherwise set the lock to a locked state and return <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  1476. <p>When invoked with a positive, floating-point value for <em>timeout</em>, block
  1477. for at most the number of seconds specified by <em>timeout</em> as long as
  1478. the lock can not be acquired. Invocations with a negative value for
  1479. <em>timeout</em> are equivalent to a <em>timeout</em> of zero. Invocations with a
  1480. <em>timeout</em> value of <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default) set the timeout period to
  1481. infinite. Note that the treatment of negative or <code class="docutils literal notranslate"><span class="pre">None</span></code> values for
  1482. <em>timeout</em> differs from the implemented behavior in
  1483. <a class="reference internal" href="threading.html#threading.Lock.acquire" title="threading.Lock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">threading.Lock.acquire()</span></code></a>. The <em>timeout</em> argument has no practical
  1484. implications if the <em>block</em> argument is set to <code class="docutils literal notranslate"><span class="pre">False</span></code> and is thus
  1485. ignored. Returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if the lock has been acquired or <code class="docutils literal notranslate"><span class="pre">False</span></code> if
  1486. the timeout period has elapsed.</p>
  1487. </dd></dl>
  1488. <dl class="py method">
  1489. <dt class="sig sig-object py" id="multiprocessing.Lock.release">
  1490. <span class="sig-name descname"><span class="pre">release</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Lock.release" title="Link to this definition">¶</a></dt>
  1491. <dd><p>Release a lock. This can be called from any process or thread, not only
  1492. the process or thread which originally acquired the lock.</p>
  1493. <p>Behavior is the same as in <a class="reference internal" href="threading.html#threading.Lock.release" title="threading.Lock.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">threading.Lock.release()</span></code></a> except that
  1494. when invoked on an unlocked lock, 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> is raised.</p>
  1495. </dd></dl>
  1496. </dd></dl>
  1497. <dl class="py class">
  1498. <dt class="sig sig-object py" id="multiprocessing.RLock">
  1499. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">RLock</span></span><a class="headerlink" href="#multiprocessing.RLock" title="Link to this definition">¶</a></dt>
  1500. <dd><p>A recursive lock object: a close analog of <a class="reference internal" href="threading.html#threading.RLock" title="threading.RLock"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.RLock</span></code></a>. A
  1501. recursive lock must be released by the process or thread that acquired it.
  1502. Once a process or thread has acquired a recursive lock, the same process
  1503. or thread may acquire it again without blocking; that process or thread
  1504. must release it once for each time it has been acquired.</p>
  1505. <p>Note that <a class="reference internal" href="#multiprocessing.RLock" title="multiprocessing.RLock"><code class="xref py py-class docutils literal notranslate"><span class="pre">RLock</span></code></a> is actually a factory function which returns an
  1506. instance of <code class="docutils literal notranslate"><span class="pre">multiprocessing.synchronize.RLock</span></code> initialized with a
  1507. default context.</p>
  1508. <p><a class="reference internal" href="#multiprocessing.RLock" title="multiprocessing.RLock"><code class="xref py py-class docutils literal notranslate"><span class="pre">RLock</span></code></a> supports the <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> protocol and thus may be
  1509. used in <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> statements.</p>
  1510. <dl class="py method">
  1511. <dt class="sig sig-object py" id="multiprocessing.RLock.acquire">
  1512. <span class="sig-name descname"><span class="pre">acquire</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">block</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="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="#multiprocessing.RLock.acquire" title="Link to this definition">¶</a></dt>
  1513. <dd><p>Acquire a lock, blocking or non-blocking.</p>
  1514. <p>When invoked with the <em>block</em> argument set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, block until the
  1515. lock is in an unlocked state (not owned by any process or thread) unless
  1516. the lock is already owned by the current process or thread. The current
  1517. process or thread then takes ownership of the lock (if it does not
  1518. already have ownership) and the recursion level inside the lock increments
  1519. by one, resulting in a return value of <code class="docutils literal notranslate"><span class="pre">True</span></code>. Note that there are
  1520. several differences in this first argument’s behavior compared to the
  1521. implementation of <a class="reference internal" href="threading.html#threading.RLock.acquire" title="threading.RLock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">threading.RLock.acquire()</span></code></a>, starting with the name
  1522. of the argument itself.</p>
  1523. <p>When invoked with the <em>block</em> argument set to <code class="docutils literal notranslate"><span class="pre">False</span></code>, do not block.
  1524. If the lock has already been acquired (and thus is owned) by another
  1525. process or thread, the current process or thread does not take ownership
  1526. and the recursion level within the lock is not changed, resulting in
  1527. a return value of <code class="docutils literal notranslate"><span class="pre">False</span></code>. If the lock is in an unlocked state, the
  1528. current process or thread takes ownership and the recursion level is
  1529. incremented, resulting in a return value of <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  1530. <p>Use and behaviors of the <em>timeout</em> argument are the same as in
  1531. <a class="reference internal" href="#multiprocessing.Lock.acquire" title="multiprocessing.Lock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Lock.acquire()</span></code></a>. Note that some of these behaviors of <em>timeout</em>
  1532. differ from the implemented behaviors in <a class="reference internal" href="threading.html#threading.RLock.acquire" title="threading.RLock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">threading.RLock.acquire()</span></code></a>.</p>
  1533. </dd></dl>
  1534. <dl class="py method">
  1535. <dt class="sig sig-object py" id="multiprocessing.RLock.release">
  1536. <span class="sig-name descname"><span class="pre">release</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.RLock.release" title="Link to this definition">¶</a></dt>
  1537. <dd><p>Release a lock, decrementing the recursion level. If after the
  1538. decrement the recursion level is zero, reset the lock to unlocked (not
  1539. owned by any process or thread) and if any other processes or threads
  1540. are blocked waiting for the lock to become unlocked, allow exactly one
  1541. of them to proceed. If after the decrement the recursion level is still
  1542. nonzero, the lock remains locked and owned by the calling process or
  1543. thread.</p>
  1544. <p>Only call this method when the calling process or thread owns the lock.
  1545. An <a class="reference internal" href="exceptions.html#AssertionError" title="AssertionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AssertionError</span></code></a> is raised if this method is called by a process
  1546. or thread other than the owner or if the lock is in an unlocked (unowned)
  1547. state. Note that the type of exception raised in this situation
  1548. differs from the implemented behavior in <a class="reference internal" href="threading.html#threading.RLock.release" title="threading.RLock.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">threading.RLock.release()</span></code></a>.</p>
  1549. </dd></dl>
  1550. </dd></dl>
  1551. <dl class="py class">
  1552. <dt class="sig sig-object py" id="multiprocessing.Semaphore">
  1553. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">Semaphore</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Semaphore" title="Link to this definition">¶</a></dt>
  1554. <dd><p>A semaphore object: a close analog of <a class="reference internal" href="threading.html#threading.Semaphore" title="threading.Semaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Semaphore</span></code></a>.</p>
  1555. <p>A solitary difference from its close analog exists: its <code class="docutils literal notranslate"><span class="pre">acquire</span></code> method’s
  1556. first argument is named <em>block</em>, as is consistent with <a class="reference internal" href="#multiprocessing.Lock.acquire" title="multiprocessing.Lock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Lock.acquire()</span></code></a>.</p>
  1557. </dd></dl>
  1558. <div class="admonition note">
  1559. <p class="admonition-title">Note</p>
  1560. <p>On macOS, <code class="docutils literal notranslate"><span class="pre">sem_timedwait</span></code> is unsupported, so calling <code class="docutils literal notranslate"><span class="pre">acquire()</span></code> with
  1561. a timeout will emulate that function’s behavior using a sleeping loop.</p>
  1562. </div>
  1563. <div class="admonition note">
  1564. <p class="admonition-title">Note</p>
  1565. <p>If the SIGINT signal generated by <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Ctrl</kbd>-<kbd class="kbd docutils literal notranslate">C</kbd></kbd> arrives while the main thread is
  1566. blocked by a call to <code class="xref py py-meth docutils literal notranslate"><span class="pre">BoundedSemaphore.acquire()</span></code>, <a class="reference internal" href="#multiprocessing.Lock.acquire" title="multiprocessing.Lock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Lock.acquire()</span></code></a>,
  1567. <a class="reference internal" href="#multiprocessing.RLock.acquire" title="multiprocessing.RLock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">RLock.acquire()</span></code></a>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">Semaphore.acquire()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">Condition.acquire()</span></code>
  1568. or <code class="xref py py-meth docutils literal notranslate"><span class="pre">Condition.wait()</span></code> then the call will be immediately interrupted and
  1569. <a class="reference internal" href="exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a> will be raised.</p>
  1570. <p>This differs from the behaviour of <a class="reference internal" href="threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> where SIGINT will be
  1571. ignored while the equivalent blocking calls are in progress.</p>
  1572. </div>
  1573. <div class="admonition note">
  1574. <p class="admonition-title">Note</p>
  1575. <p>Some of this package’s functionality requires a functioning shared semaphore
  1576. implementation on the host operating system. Without one, the
  1577. <code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing.synchronize</span></code> module will be disabled, and attempts to
  1578. import it will result in an <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a>. See
  1579. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3770">bpo-3770</a> for additional information.</p>
  1580. </div>
  1581. </section>
  1582. <section id="shared-ctypes-objects">
  1583. <h3>Shared <a class="reference internal" href="ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ctypes</span></code></a> Objects<a class="headerlink" href="#shared-ctypes-objects" title="Link to this heading">¶</a></h3>
  1584. <p>It is possible to create shared objects using shared memory which can be
  1585. inherited by child processes.</p>
  1586. <dl class="py function">
  1587. <dt class="sig sig-object py" id="multiprocessing.Value">
  1588. <span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">Value</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">typecode_or_type</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="n"><span class="pre">lock</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Value" title="Link to this definition">¶</a></dt>
  1589. <dd><p>Return a <a class="reference internal" href="ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ctypes</span></code></a> object allocated from shared memory. By default the
  1590. return value is actually a synchronized wrapper for the object. The object
  1591. itself can be accessed via the <em>value</em> attribute of a <a class="reference internal" href="#multiprocessing.Value" title="multiprocessing.Value"><code class="xref py py-class docutils literal notranslate"><span class="pre">Value</span></code></a>.</p>
  1592. <p><em>typecode_or_type</em> determines the type of the returned object: it is either a
  1593. ctypes type or a one character typecode of the kind used by the <a class="reference internal" href="array.html#module-array" title="array: Space efficient arrays of uniformly typed numeric values."><code class="xref py py-mod docutils literal notranslate"><span class="pre">array</span></code></a>
  1594. module. <em>*args</em> is passed on to the constructor for the type.</p>
  1595. <p>If <em>lock</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default) then a new recursive lock
  1596. object is created to synchronize access to the value. If <em>lock</em> is
  1597. a <a class="reference internal" href="#multiprocessing.Lock" title="multiprocessing.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">Lock</span></code></a> or <a class="reference internal" href="#multiprocessing.RLock" title="multiprocessing.RLock"><code class="xref py py-class docutils literal notranslate"><span class="pre">RLock</span></code></a> object then that will be used to
  1598. synchronize access to the value. If <em>lock</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code> then
  1599. access to the returned object will not be automatically protected
  1600. by a lock, so it will not necessarily be “process-safe”.</p>
  1601. <p>Operations like <code class="docutils literal notranslate"><span class="pre">+=</span></code> which involve a read and write are not
  1602. atomic. So if, for instance, you want to atomically increment a
  1603. shared value it is insufficient to just do</p>
  1604. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">counter</span><span class="o">.</span><span class="n">value</span> <span class="o">+=</span> <span class="mi">1</span>
  1605. </pre></div>
  1606. </div>
  1607. <p>Assuming the associated lock is recursive (which it is by default)
  1608. you can instead do</p>
  1609. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">counter</span><span class="o">.</span><span class="n">get_lock</span><span class="p">():</span>
  1610. <span class="n">counter</span><span class="o">.</span><span class="n">value</span> <span class="o">+=</span> <span class="mi">1</span>
  1611. </pre></div>
  1612. </div>
  1613. <p>Note that <em>lock</em> is a keyword-only argument.</p>
  1614. </dd></dl>
  1615. <dl class="py function">
  1616. <dt class="sig sig-object py" id="multiprocessing.Array">
  1617. <span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">Array</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">typecode_or_type</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">size_or_initializer</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">lock</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Array" title="Link to this definition">¶</a></dt>
  1618. <dd><p>Return a ctypes array allocated from shared memory. By default the return
  1619. value is actually a synchronized wrapper for the array.</p>
  1620. <p><em>typecode_or_type</em> determines the type of the elements of the returned array:
  1621. it is either a ctypes type or a one character typecode of the kind used by
  1622. the <a class="reference internal" href="array.html#module-array" title="array: Space efficient arrays of uniformly typed numeric values."><code class="xref py py-mod docutils literal notranslate"><span class="pre">array</span></code></a> module. If <em>size_or_initializer</em> is an integer, then it
  1623. determines the length of the array, and the array will be initially zeroed.
  1624. Otherwise, <em>size_or_initializer</em> is a sequence which is used to initialize
  1625. the array and whose length determines the length of the array.</p>
  1626. <p>If <em>lock</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default) then a new lock object is created to
  1627. synchronize access to the value. If <em>lock</em> is a <a class="reference internal" href="#multiprocessing.Lock" title="multiprocessing.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">Lock</span></code></a> or
  1628. <a class="reference internal" href="#multiprocessing.RLock" title="multiprocessing.RLock"><code class="xref py py-class docutils literal notranslate"><span class="pre">RLock</span></code></a> object then that will be used to synchronize access to the
  1629. value. If <em>lock</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code> then access to the returned object will not be
  1630. automatically protected by a lock, so it will not necessarily be
  1631. “process-safe”.</p>
  1632. <p>Note that <em>lock</em> is a keyword only argument.</p>
  1633. <p>Note that an array of <a class="reference internal" href="ctypes.html#ctypes.c_char" title="ctypes.c_char"><code class="xref py py-data docutils literal notranslate"><span class="pre">ctypes.c_char</span></code></a> has <em>value</em> and <em>raw</em>
  1634. attributes which allow one to use it to store and retrieve strings.</p>
  1635. </dd></dl>
  1636. <section id="module-multiprocessing.sharedctypes">
  1637. <span id="the-multiprocessing-sharedctypes-module"></span><h4>The <a class="reference internal" href="#module-multiprocessing.sharedctypes" title="multiprocessing.sharedctypes: Allocate ctypes objects from shared memory."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing.sharedctypes</span></code></a> module<a class="headerlink" href="#module-multiprocessing.sharedctypes" title="Link to this heading">¶</a></h4>
  1638. <p>The <a class="reference internal" href="#module-multiprocessing.sharedctypes" title="multiprocessing.sharedctypes: Allocate ctypes objects from shared memory."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing.sharedctypes</span></code></a> module provides functions for allocating
  1639. <a class="reference internal" href="ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ctypes</span></code></a> objects from shared memory which can be inherited by child
  1640. processes.</p>
  1641. <div class="admonition note">
  1642. <p class="admonition-title">Note</p>
  1643. <p>Although it is possible to store a pointer in shared memory remember that
  1644. this will refer to a location in the address space of a specific process.
  1645. However, the pointer is quite likely to be invalid in the context of a second
  1646. process and trying to dereference the pointer from the second process may
  1647. cause a crash.</p>
  1648. </div>
  1649. <dl class="py function">
  1650. <dt class="sig sig-object py" id="multiprocessing.sharedctypes.RawArray">
  1651. <span class="sig-prename descclassname"><span class="pre">multiprocessing.sharedctypes.</span></span><span class="sig-name descname"><span class="pre">RawArray</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">typecode_or_type</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">size_or_initializer</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.sharedctypes.RawArray" title="Link to this definition">¶</a></dt>
  1652. <dd><p>Return a ctypes array allocated from shared memory.</p>
  1653. <p><em>typecode_or_type</em> determines the type of the elements of the returned array:
  1654. it is either a ctypes type or a one character typecode of the kind used by
  1655. the <a class="reference internal" href="array.html#module-array" title="array: Space efficient arrays of uniformly typed numeric values."><code class="xref py py-mod docutils literal notranslate"><span class="pre">array</span></code></a> module. If <em>size_or_initializer</em> is an integer then it
  1656. determines the length of the array, and the array will be initially zeroed.
  1657. Otherwise <em>size_or_initializer</em> is a sequence which is used to initialize the
  1658. array and whose length determines the length of the array.</p>
  1659. <p>Note that setting and getting an element is potentially non-atomic – use
  1660. <a class="reference internal" href="#multiprocessing.sharedctypes.Array" title="multiprocessing.sharedctypes.Array"><code class="xref py py-func docutils literal notranslate"><span class="pre">Array()</span></code></a> instead to make sure that access is automatically synchronized
  1661. using a lock.</p>
  1662. </dd></dl>
  1663. <dl class="py function">
  1664. <dt class="sig sig-object py" id="multiprocessing.sharedctypes.RawValue">
  1665. <span class="sig-prename descclassname"><span class="pre">multiprocessing.sharedctypes.</span></span><span class="sig-name descname"><span class="pre">RawValue</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">typecode_or_type</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><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.sharedctypes.RawValue" title="Link to this definition">¶</a></dt>
  1666. <dd><p>Return a ctypes object allocated from shared memory.</p>
  1667. <p><em>typecode_or_type</em> determines the type of the returned object: it is either a
  1668. ctypes type or a one character typecode of the kind used by the <a class="reference internal" href="array.html#module-array" title="array: Space efficient arrays of uniformly typed numeric values."><code class="xref py py-mod docutils literal notranslate"><span class="pre">array</span></code></a>
  1669. module. <em>*args</em> is passed on to the constructor for the type.</p>
  1670. <p>Note that setting and getting the value is potentially non-atomic – use
  1671. <a class="reference internal" href="#multiprocessing.sharedctypes.Value" title="multiprocessing.sharedctypes.Value"><code class="xref py py-func docutils literal notranslate"><span class="pre">Value()</span></code></a> instead to make sure that access is automatically synchronized
  1672. using a lock.</p>
  1673. <p>Note that an array of <a class="reference internal" href="ctypes.html#ctypes.c_char" title="ctypes.c_char"><code class="xref py py-data docutils literal notranslate"><span class="pre">ctypes.c_char</span></code></a> has <code class="docutils literal notranslate"><span class="pre">value</span></code> and <code class="docutils literal notranslate"><span class="pre">raw</span></code>
  1674. attributes which allow one to use it to store and retrieve strings – see
  1675. documentation for <a class="reference internal" href="ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ctypes</span></code></a>.</p>
  1676. </dd></dl>
  1677. <dl class="py function">
  1678. <dt class="sig sig-object py" id="multiprocessing.sharedctypes.Array">
  1679. <span class="sig-prename descclassname"><span class="pre">multiprocessing.sharedctypes.</span></span><span class="sig-name descname"><span class="pre">Array</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">typecode_or_type</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">size_or_initializer</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">lock</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.sharedctypes.Array" title="Link to this definition">¶</a></dt>
  1680. <dd><p>The same as <a class="reference internal" href="#multiprocessing.sharedctypes.RawArray" title="multiprocessing.sharedctypes.RawArray"><code class="xref py py-func docutils literal notranslate"><span class="pre">RawArray()</span></code></a> except that depending on the value of <em>lock</em> a
  1681. process-safe synchronization wrapper may be returned instead of a raw ctypes
  1682. array.</p>
  1683. <p>If <em>lock</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default) then a new lock object is created to
  1684. synchronize access to the value. If <em>lock</em> is a
  1685. <a class="reference internal" href="#multiprocessing.Lock" title="multiprocessing.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">Lock</span></code></a> or <a class="reference internal" href="#multiprocessing.RLock" title="multiprocessing.RLock"><code class="xref py py-class docutils literal notranslate"><span class="pre">RLock</span></code></a> object
  1686. then that will be used to synchronize access to the
  1687. value. If <em>lock</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code> then access to the returned object will not be
  1688. automatically protected by a lock, so it will not necessarily be
  1689. “process-safe”.</p>
  1690. <p>Note that <em>lock</em> is a keyword-only argument.</p>
  1691. </dd></dl>
  1692. <dl class="py function">
  1693. <dt class="sig sig-object py" id="multiprocessing.sharedctypes.Value">
  1694. <span class="sig-prename descclassname"><span class="pre">multiprocessing.sharedctypes.</span></span><span class="sig-name descname"><span class="pre">Value</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">typecode_or_type</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="n"><span class="pre">lock</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.sharedctypes.Value" title="Link to this definition">¶</a></dt>
  1695. <dd><p>The same as <a class="reference internal" href="#multiprocessing.sharedctypes.RawValue" title="multiprocessing.sharedctypes.RawValue"><code class="xref py py-func docutils literal notranslate"><span class="pre">RawValue()</span></code></a> except that depending on the value of <em>lock</em> a
  1696. process-safe synchronization wrapper may be returned instead of a raw ctypes
  1697. object.</p>
  1698. <p>If <em>lock</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default) then a new lock object is created to
  1699. synchronize access to the value. If <em>lock</em> is a <a class="reference internal" href="#multiprocessing.Lock" title="multiprocessing.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">Lock</span></code></a> or
  1700. <a class="reference internal" href="#multiprocessing.RLock" title="multiprocessing.RLock"><code class="xref py py-class docutils literal notranslate"><span class="pre">RLock</span></code></a> object then that will be used to synchronize access to the
  1701. value. If <em>lock</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code> then access to the returned object will not be
  1702. automatically protected by a lock, so it will not necessarily be
  1703. “process-safe”.</p>
  1704. <p>Note that <em>lock</em> is a keyword-only argument.</p>
  1705. </dd></dl>
  1706. <dl class="py function">
  1707. <dt class="sig sig-object py" id="multiprocessing.sharedctypes.copy">
  1708. <span class="sig-prename descclassname"><span class="pre">multiprocessing.sharedctypes.</span></span><span class="sig-name descname"><span class="pre">copy</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.sharedctypes.copy" title="Link to this definition">¶</a></dt>
  1709. <dd><p>Return a ctypes object allocated from shared memory which is a copy of the
  1710. ctypes object <em>obj</em>.</p>
  1711. </dd></dl>
  1712. <dl class="py function">
  1713. <dt class="sig sig-object py" id="multiprocessing.sharedctypes.synchronized">
  1714. <span class="sig-prename descclassname"><span class="pre">multiprocessing.sharedctypes.</span></span><span class="sig-name descname"><span class="pre">synchronized</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">lock</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.sharedctypes.synchronized" title="Link to this definition">¶</a></dt>
  1715. <dd><p>Return a process-safe wrapper object for a ctypes object which uses <em>lock</em> to
  1716. synchronize access. If <em>lock</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default) then a
  1717. <a class="reference internal" href="#multiprocessing.RLock" title="multiprocessing.RLock"><code class="xref py py-class docutils literal notranslate"><span class="pre">multiprocessing.RLock</span></code></a> object is created automatically.</p>
  1718. <p>A synchronized wrapper will have two methods in addition to those of the
  1719. object it wraps: <code class="xref py py-meth docutils literal notranslate"><span class="pre">get_obj()</span></code> returns the wrapped object and
  1720. <code class="xref py py-meth docutils literal notranslate"><span class="pre">get_lock()</span></code> returns the lock object used for synchronization.</p>
  1721. <p>Note that accessing the ctypes object through the wrapper can be a lot slower
  1722. than accessing the raw ctypes object.</p>
  1723. <div class="versionchanged">
  1724. <p><span class="versionmodified changed">Changed in version 3.5: </span>Synchronized objects support the <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> protocol.</p>
  1725. </div>
  1726. </dd></dl>
  1727. <p>The table below compares the syntax for creating shared ctypes objects from
  1728. shared memory with the normal ctypes syntax. (In the table <code class="docutils literal notranslate"><span class="pre">MyStruct</span></code> is some
  1729. subclass of <a class="reference internal" href="ctypes.html#ctypes.Structure" title="ctypes.Structure"><code class="xref py py-class docutils literal notranslate"><span class="pre">ctypes.Structure</span></code></a>.)</p>
  1730. <table class="docutils align-default">
  1731. <thead>
  1732. <tr class="row-odd"><th class="head"><p>ctypes</p></th>
  1733. <th class="head"><p>sharedctypes using type</p></th>
  1734. <th class="head"><p>sharedctypes using typecode</p></th>
  1735. </tr>
  1736. </thead>
  1737. <tbody>
  1738. <tr class="row-even"><td><p>c_double(2.4)</p></td>
  1739. <td><p>RawValue(c_double, 2.4)</p></td>
  1740. <td><p>RawValue(‘d’, 2.4)</p></td>
  1741. </tr>
  1742. <tr class="row-odd"><td><p>MyStruct(4, 6)</p></td>
  1743. <td><p>RawValue(MyStruct, 4, 6)</p></td>
  1744. <td></td>
  1745. </tr>
  1746. <tr class="row-even"><td><p>(c_short * 7)()</p></td>
  1747. <td><p>RawArray(c_short, 7)</p></td>
  1748. <td><p>RawArray(‘h’, 7)</p></td>
  1749. </tr>
  1750. <tr class="row-odd"><td><p>(c_int * 3)(9, 2, 8)</p></td>
  1751. <td><p>RawArray(c_int, (9, 2, 8))</p></td>
  1752. <td><p>RawArray(‘i’, (9, 2, 8))</p></td>
  1753. </tr>
  1754. </tbody>
  1755. </table>
  1756. <p>Below is an example where a number of ctypes objects are modified by a child
  1757. process:</p>
  1758. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">Lock</span>
  1759. <span class="kn">from</span> <span class="nn">multiprocessing.sharedctypes</span> <span class="kn">import</span> <span class="n">Value</span><span class="p">,</span> <span class="n">Array</span>
  1760. <span class="kn">from</span> <span class="nn">ctypes</span> <span class="kn">import</span> <span class="n">Structure</span><span class="p">,</span> <span class="n">c_double</span>
  1761. <span class="k">class</span> <span class="nc">Point</span><span class="p">(</span><span class="n">Structure</span><span class="p">):</span>
  1762. <span class="n">_fields_</span> <span class="o">=</span> <span class="p">[(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">c_double</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">,</span> <span class="n">c_double</span><span class="p">)]</span>
  1763. <span class="k">def</span> <span class="nf">modify</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">A</span><span class="p">):</span>
  1764. <span class="n">n</span><span class="o">.</span><span class="n">value</span> <span class="o">**=</span> <span class="mi">2</span>
  1765. <span class="n">x</span><span class="o">.</span><span class="n">value</span> <span class="o">**=</span> <span class="mi">2</span>
  1766. <span class="n">s</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">value</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
  1767. <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">A</span><span class="p">:</span>
  1768. <span class="n">a</span><span class="o">.</span><span class="n">x</span> <span class="o">**=</span> <span class="mi">2</span>
  1769. <span class="n">a</span><span class="o">.</span><span class="n">y</span> <span class="o">**=</span> <span class="mi">2</span>
  1770. <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>
  1771. <span class="n">lock</span> <span class="o">=</span> <span class="n">Lock</span><span class="p">()</span>
  1772. <span class="n">n</span> <span class="o">=</span> <span class="n">Value</span><span class="p">(</span><span class="s1">&#39;i&#39;</span><span class="p">,</span> <span class="mi">7</span><span class="p">)</span>
  1773. <span class="n">x</span> <span class="o">=</span> <span class="n">Value</span><span class="p">(</span><span class="n">c_double</span><span class="p">,</span> <span class="mf">1.0</span><span class="o">/</span><span class="mf">3.0</span><span class="p">,</span> <span class="n">lock</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
  1774. <span class="n">s</span> <span class="o">=</span> <span class="n">Array</span><span class="p">(</span><span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;hello world&#39;</span><span class="p">,</span> <span class="n">lock</span><span class="o">=</span><span class="n">lock</span><span class="p">)</span>
  1775. <span class="n">A</span> <span class="o">=</span> <span class="n">Array</span><span class="p">(</span><span class="n">Point</span><span class="p">,</span> <span class="p">[(</span><span class="mf">1.875</span><span class="p">,</span><span class="o">-</span><span class="mf">6.25</span><span class="p">),</span> <span class="p">(</span><span class="o">-</span><span class="mf">5.75</span><span class="p">,</span><span class="mf">2.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">2.375</span><span class="p">,</span><span class="mf">9.5</span><span class="p">)],</span> <span class="n">lock</span><span class="o">=</span><span class="n">lock</span><span class="p">)</span>
  1776. <span class="n">p</span> <span class="o">=</span> <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">modify</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">A</span><span class="p">))</span>
  1777. <span class="n">p</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  1778. <span class="n">p</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
  1779. <span class="nb">print</span><span class="p">(</span><span class="n">n</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
  1780. <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
  1781. <span class="nb">print</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
  1782. <span class="nb">print</span><span class="p">([(</span><span class="n">a</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">y</span><span class="p">)</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">A</span><span class="p">])</span>
  1783. </pre></div>
  1784. </div>
  1785. <p>The results printed are</p>
  1786. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>49
  1787. 0.1111111111111111
  1788. HELLO WORLD
  1789. [(3.515625, 39.0625), (33.0625, 4.0), (5.640625, 90.25)]
  1790. </pre></div>
  1791. </div>
  1792. </section>
  1793. </section>
  1794. <section id="managers">
  1795. <span id="multiprocessing-managers"></span><h3>Managers<a class="headerlink" href="#managers" title="Link to this heading">¶</a></h3>
  1796. <p>Managers provide a way to create data which can be shared between different
  1797. processes, including sharing over a network between processes running on
  1798. different machines. A manager object controls a server process which manages
  1799. <em>shared objects</em>. Other processes can access the shared objects by using
  1800. proxies.</p>
  1801. <dl class="py function">
  1802. <dt class="sig sig-object py" id="multiprocessing.Manager">
  1803. <span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">Manager</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.Manager" title="Link to this definition">¶</a></dt>
  1804. <dd><p>Returns a started <a class="reference internal" href="#multiprocessing.managers.SyncManager" title="multiprocessing.managers.SyncManager"><code class="xref py py-class docutils literal notranslate"><span class="pre">SyncManager</span></code></a> object which
  1805. can be used for sharing objects between processes. The returned manager
  1806. object corresponds to a spawned child process and has methods which will
  1807. create shared objects and return corresponding proxies.</p>
  1808. </dd></dl>
  1809. <p id="module-multiprocessing.managers">Manager processes will be shutdown as soon as they are garbage collected or
  1810. their parent process exits. The manager classes are defined in the
  1811. <a class="reference internal" href="#module-multiprocessing.managers" title="multiprocessing.managers: Share data between process with shared objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing.managers</span></code></a> module:</p>
  1812. <dl class="py class">
  1813. <dt class="sig sig-object py" id="multiprocessing.managers.BaseManager">
  1814. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.managers.</span></span><span class="sig-name descname"><span class="pre">BaseManager</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">address</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">authkey</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">serializer</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'pickle'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ctx</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="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">shutdown_timeout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1.0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.BaseManager" title="Link to this definition">¶</a></dt>
  1815. <dd><p>Create a BaseManager object.</p>
  1816. <p>Once created one should call <a class="reference internal" href="#multiprocessing.managers.BaseManager.start" title="multiprocessing.managers.BaseManager.start"><code class="xref py py-meth docutils literal notranslate"><span class="pre">start()</span></code></a> or <code class="docutils literal notranslate"><span class="pre">get_server().serve_forever()</span></code> to ensure
  1817. that the manager object refers to a started manager process.</p>
  1818. <p><em>address</em> is the address on which the manager process listens for new
  1819. connections. If <em>address</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> then an arbitrary one is chosen.</p>
  1820. <p><em>authkey</em> is the authentication key which will be used to check the
  1821. validity of incoming connections to the server process. If
  1822. <em>authkey</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> then <code class="docutils literal notranslate"><span class="pre">current_process().authkey</span></code> is used.
  1823. Otherwise <em>authkey</em> is used and it must be a byte string.</p>
  1824. <p><em>serializer</em> must be <code class="docutils literal notranslate"><span class="pre">'pickle'</span></code> (use <a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> serialization) or
  1825. <code class="docutils literal notranslate"><span class="pre">'xmlrpclib'</span></code> (use <a class="reference internal" href="xmlrpc.client.html#module-xmlrpc.client" title="xmlrpc.client: XML-RPC client access."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xmlrpc.client</span></code></a> serialization).</p>
  1826. <p><em>ctx</em> is a context object, or <code class="docutils literal notranslate"><span class="pre">None</span></code> (use the current context). See the
  1827. <code class="xref py py-func docutils literal notranslate"><span class="pre">get_context()</span></code> function.</p>
  1828. <p><em>shutdown_timeout</em> is a timeout in seconds used to wait until the process
  1829. used by the manager completes in the <a class="reference internal" href="#multiprocessing.managers.BaseManager.shutdown" title="multiprocessing.managers.BaseManager.shutdown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">shutdown()</span></code></a> method. If the
  1830. shutdown times out, the process is terminated. If terminating the process
  1831. also times out, the process is killed.</p>
  1832. <div class="versionchanged">
  1833. <p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>shutdown_timeout</em> parameter.</p>
  1834. </div>
  1835. <dl class="py method">
  1836. <dt class="sig sig-object py" id="multiprocessing.managers.BaseManager.start">
  1837. <span class="sig-name descname"><span class="pre">start</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">initializer</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">initargs</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.BaseManager.start" title="Link to this definition">¶</a></dt>
  1838. <dd><p>Start a subprocess to start the manager. If <em>initializer</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>
  1839. then the subprocess will call <code class="docutils literal notranslate"><span class="pre">initializer(*initargs)</span></code> when it starts.</p>
  1840. </dd></dl>
  1841. <dl class="py method">
  1842. <dt class="sig sig-object py" id="multiprocessing.managers.BaseManager.get_server">
  1843. <span class="sig-name descname"><span class="pre">get_server</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.BaseManager.get_server" title="Link to this definition">¶</a></dt>
  1844. <dd><p>Returns a <code class="xref py py-class docutils literal notranslate"><span class="pre">Server</span></code> object which represents the actual server under
  1845. the control of the Manager. The <code class="xref py py-class docutils literal notranslate"><span class="pre">Server</span></code> object supports the
  1846. <code class="xref py py-meth docutils literal notranslate"><span class="pre">serve_forever()</span></code> method:</p>
  1847. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">multiprocessing.managers</span> <span class="kn">import</span> <span class="n">BaseManager</span>
  1848. <span class="gp">&gt;&gt;&gt; </span><span class="n">manager</span> <span class="o">=</span> <span class="n">BaseManager</span><span class="p">(</span><span class="n">address</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="mi">50000</span><span class="p">),</span> <span class="n">authkey</span><span class="o">=</span><span class="sa">b</span><span class="s1">&#39;abc&#39;</span><span class="p">)</span>
  1849. <span class="gp">&gt;&gt;&gt; </span><span class="n">server</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">get_server</span><span class="p">()</span>
  1850. <span class="gp">&gt;&gt;&gt; </span><span class="n">server</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
  1851. </pre></div>
  1852. </div>
  1853. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">Server</span></code> additionally has an <a class="reference internal" href="#multiprocessing.managers.BaseManager.address" title="multiprocessing.managers.BaseManager.address"><code class="xref py py-attr docutils literal notranslate"><span class="pre">address</span></code></a> attribute.</p>
  1854. </dd></dl>
  1855. <dl class="py method">
  1856. <dt class="sig sig-object py" id="multiprocessing.managers.BaseManager.connect">
  1857. <span class="sig-name descname"><span class="pre">connect</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.BaseManager.connect" title="Link to this definition">¶</a></dt>
  1858. <dd><p>Connect a local manager object to a remote manager process:</p>
  1859. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">multiprocessing.managers</span> <span class="kn">import</span> <span class="n">BaseManager</span>
  1860. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">BaseManager</span><span class="p">(</span><span class="n">address</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">50000</span><span class="p">),</span> <span class="n">authkey</span><span class="o">=</span><span class="sa">b</span><span class="s1">&#39;abc&#39;</span><span class="p">)</span>
  1861. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
  1862. </pre></div>
  1863. </div>
  1864. </dd></dl>
  1865. <dl class="py method">
  1866. <dt class="sig sig-object py" id="multiprocessing.managers.BaseManager.shutdown">
  1867. <span class="sig-name descname"><span class="pre">shutdown</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.BaseManager.shutdown" title="Link to this definition">¶</a></dt>
  1868. <dd><p>Stop the process used by the manager. This is only available if
  1869. <a class="reference internal" href="#multiprocessing.managers.BaseManager.start" title="multiprocessing.managers.BaseManager.start"><code class="xref py py-meth docutils literal notranslate"><span class="pre">start()</span></code></a> has been used to start the server process.</p>
  1870. <p>This can be called multiple times.</p>
  1871. </dd></dl>
  1872. <dl class="py method">
  1873. <dt class="sig sig-object py" id="multiprocessing.managers.BaseManager.register">
  1874. <span class="sig-name descname"><span class="pre">register</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">typeid</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">callable</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">proxytype</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">exposed</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">method_to_typeid</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">create_method</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.BaseManager.register" title="Link to this definition">¶</a></dt>
  1875. <dd><p>A classmethod which can be used for registering a type or callable with
  1876. the manager class.</p>
  1877. <p><em>typeid</em> is a “type identifier” which is used to identify a particular
  1878. type of shared object. This must be a string.</p>
  1879. <p><em>callable</em> is a callable used for creating objects for this type
  1880. identifier. If a manager instance will be connected to the
  1881. server using the <a class="reference internal" href="#multiprocessing.managers.BaseManager.connect" title="multiprocessing.managers.BaseManager.connect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">connect()</span></code></a> method, or if the
  1882. <em>create_method</em> argument is <code class="docutils literal notranslate"><span class="pre">False</span></code> then this can be left as
  1883. <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1884. <p><em>proxytype</em> is a subclass of <a class="reference internal" href="#multiprocessing.managers.BaseProxy" title="multiprocessing.managers.BaseProxy"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseProxy</span></code></a> which is used to create
  1885. proxies for shared objects with this <em>typeid</em>. If <code class="docutils literal notranslate"><span class="pre">None</span></code> then a proxy
  1886. class is created automatically.</p>
  1887. <p><em>exposed</em> is used to specify a sequence of method names which proxies for
  1888. this typeid should be allowed to access using
  1889. <a class="reference internal" href="#multiprocessing.managers.BaseProxy._callmethod" title="multiprocessing.managers.BaseProxy._callmethod"><code class="xref py py-meth docutils literal notranslate"><span class="pre">BaseProxy._callmethod()</span></code></a>. (If <em>exposed</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> then
  1890. <code class="xref py py-attr docutils literal notranslate"><span class="pre">proxytype._exposed_</span></code> is used instead if it exists.) In the case
  1891. where no exposed list is specified, all “public methods” of the shared
  1892. object will be accessible. (Here a “public method” means any attribute
  1893. which has a <a class="reference internal" href="../reference/datamodel.html#object.__call__" title="object.__call__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__call__()</span></code></a> method and whose name does not begin
  1894. with <code class="docutils literal notranslate"><span class="pre">'_'</span></code>.)</p>
  1895. <p><em>method_to_typeid</em> is a mapping used to specify the return type of those
  1896. exposed methods which should return a proxy. It maps method names to
  1897. typeid strings. (If <em>method_to_typeid</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> then
  1898. <code class="xref py py-attr docutils literal notranslate"><span class="pre">proxytype._method_to_typeid_</span></code> is used instead if it exists.) If a
  1899. method’s name is not a key of this mapping or if the mapping is <code class="docutils literal notranslate"><span class="pre">None</span></code>
  1900. then the object returned by the method will be copied by value.</p>
  1901. <p><em>create_method</em> determines whether a method should be created with name
  1902. <em>typeid</em> which can be used to tell the server process to create a new
  1903. shared object and return a proxy for it. By default it is <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  1904. </dd></dl>
  1905. <p><a class="reference internal" href="#multiprocessing.managers.BaseManager" title="multiprocessing.managers.BaseManager"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseManager</span></code></a> instances also have one read-only property:</p>
  1906. <dl class="py attribute">
  1907. <dt class="sig sig-object py" id="multiprocessing.managers.BaseManager.address">
  1908. <span class="sig-name descname"><span class="pre">address</span></span><a class="headerlink" href="#multiprocessing.managers.BaseManager.address" title="Link to this definition">¶</a></dt>
  1909. <dd><p>The address used by the manager.</p>
  1910. </dd></dl>
  1911. <div class="versionchanged">
  1912. <p><span class="versionmodified changed">Changed in version 3.3: </span>Manager objects support the context management protocol – see
  1913. <a class="reference internal" href="stdtypes.html#typecontextmanager"><span class="std std-ref">Context Manager Types</span></a>. <a class="reference internal" href="stdtypes.html#contextmanager.__enter__" title="contextmanager.__enter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__enter__()</span></code></a> starts the
  1914. server process (if it has not already started) and then returns the
  1915. manager object. <a class="reference internal" href="stdtypes.html#contextmanager.__exit__" title="contextmanager.__exit__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__exit__()</span></code></a> calls <a class="reference internal" href="#multiprocessing.managers.BaseManager.shutdown" title="multiprocessing.managers.BaseManager.shutdown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">shutdown()</span></code></a>.</p>
  1916. <p>In previous versions <a class="reference internal" href="stdtypes.html#contextmanager.__enter__" title="contextmanager.__enter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__enter__()</span></code></a> did not start the
  1917. manager’s server process if it was not already started.</p>
  1918. </div>
  1919. </dd></dl>
  1920. <dl class="py class">
  1921. <dt class="sig sig-object py" id="multiprocessing.managers.SyncManager">
  1922. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.managers.</span></span><span class="sig-name descname"><span class="pre">SyncManager</span></span><a class="headerlink" href="#multiprocessing.managers.SyncManager" title="Link to this definition">¶</a></dt>
  1923. <dd><p>A subclass of <a class="reference internal" href="#multiprocessing.managers.BaseManager" title="multiprocessing.managers.BaseManager"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseManager</span></code></a> which can be used for the synchronization
  1924. of processes. Objects of this type are returned by
  1925. <a class="reference internal" href="#multiprocessing.Manager" title="multiprocessing.Manager"><code class="xref py py-func docutils literal notranslate"><span class="pre">multiprocessing.Manager()</span></code></a>.</p>
  1926. <p>Its methods create and return <a class="reference internal" href="#multiprocessing-proxy-objects"><span class="std std-ref">Proxy Objects</span></a> for a
  1927. number of commonly used data types to be synchronized across processes.
  1928. This notably includes shared lists and dictionaries.</p>
  1929. <dl class="py method">
  1930. <dt class="sig sig-object py" id="multiprocessing.managers.SyncManager.Barrier">
  1931. <span class="sig-name descname"><span class="pre">Barrier</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">parties</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">action</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.SyncManager.Barrier" title="Link to this definition">¶</a></dt>
  1932. <dd><p>Create a shared <a class="reference internal" href="threading.html#threading.Barrier" title="threading.Barrier"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Barrier</span></code></a> object and return a
  1933. proxy for it.</p>
  1934. <div class="versionadded">
  1935. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1936. </div>
  1937. </dd></dl>
  1938. <dl class="py method">
  1939. <dt class="sig sig-object py" id="multiprocessing.managers.SyncManager.BoundedSemaphore">
  1940. <span class="sig-name descname"><span class="pre">BoundedSemaphore</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.SyncManager.BoundedSemaphore" title="Link to this definition">¶</a></dt>
  1941. <dd><p>Create a shared <a class="reference internal" href="threading.html#threading.BoundedSemaphore" title="threading.BoundedSemaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.BoundedSemaphore</span></code></a> object and return a
  1942. proxy for it.</p>
  1943. </dd></dl>
  1944. <dl class="py method">
  1945. <dt class="sig sig-object py" id="multiprocessing.managers.SyncManager.Condition">
  1946. <span class="sig-name descname"><span class="pre">Condition</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">lock</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.SyncManager.Condition" title="Link to this definition">¶</a></dt>
  1947. <dd><p>Create a shared <a class="reference internal" href="threading.html#threading.Condition" title="threading.Condition"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Condition</span></code></a> object and return a proxy for
  1948. it.</p>
  1949. <p>If <em>lock</em> is supplied then it should be a proxy for a
  1950. <a class="reference internal" href="threading.html#threading.Lock" title="threading.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Lock</span></code></a> or <a class="reference internal" href="threading.html#threading.RLock" title="threading.RLock"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.RLock</span></code></a> object.</p>
  1951. <div class="versionchanged">
  1952. <p><span class="versionmodified changed">Changed in version 3.3: </span>The <a class="reference internal" href="threading.html#threading.Condition.wait_for" title="threading.Condition.wait_for"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait_for()</span></code></a> method was added.</p>
  1953. </div>
  1954. </dd></dl>
  1955. <dl class="py method">
  1956. <dt class="sig sig-object py" id="multiprocessing.managers.SyncManager.Event">
  1957. <span class="sig-name descname"><span class="pre">Event</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.SyncManager.Event" title="Link to this definition">¶</a></dt>
  1958. <dd><p>Create a shared <a class="reference internal" href="threading.html#threading.Event" title="threading.Event"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Event</span></code></a> object and return a proxy for it.</p>
  1959. </dd></dl>
  1960. <dl class="py method">
  1961. <dt class="sig sig-object py" id="multiprocessing.managers.SyncManager.Lock">
  1962. <span class="sig-name descname"><span class="pre">Lock</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.SyncManager.Lock" title="Link to this definition">¶</a></dt>
  1963. <dd><p>Create a shared <a class="reference internal" href="threading.html#threading.Lock" title="threading.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Lock</span></code></a> object and return a proxy for it.</p>
  1964. </dd></dl>
  1965. <dl class="py method">
  1966. <dt class="sig sig-object py" id="multiprocessing.managers.SyncManager.Namespace">
  1967. <span class="sig-name descname"><span class="pre">Namespace</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.SyncManager.Namespace" title="Link to this definition">¶</a></dt>
  1968. <dd><p>Create a shared <a class="reference internal" href="#multiprocessing.managers.Namespace" title="multiprocessing.managers.Namespace"><code class="xref py py-class docutils literal notranslate"><span class="pre">Namespace</span></code></a> object and return a proxy for it.</p>
  1969. </dd></dl>
  1970. <dl class="py method">
  1971. <dt class="sig sig-object py" id="multiprocessing.managers.SyncManager.Queue">
  1972. <span class="sig-name descname"><span class="pre">Queue</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">maxsize</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.SyncManager.Queue" title="Link to this definition">¶</a></dt>
  1973. <dd><p>Create a shared <a class="reference internal" href="queue.html#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">queue.Queue</span></code></a> object and return a proxy for it.</p>
  1974. </dd></dl>
  1975. <dl class="py method">
  1976. <dt class="sig sig-object py" id="multiprocessing.managers.SyncManager.RLock">
  1977. <span class="sig-name descname"><span class="pre">RLock</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.SyncManager.RLock" title="Link to this definition">¶</a></dt>
  1978. <dd><p>Create a shared <a class="reference internal" href="threading.html#threading.RLock" title="threading.RLock"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.RLock</span></code></a> object and return a proxy for it.</p>
  1979. </dd></dl>
  1980. <dl class="py method">
  1981. <dt class="sig sig-object py" id="multiprocessing.managers.SyncManager.Semaphore">
  1982. <span class="sig-name descname"><span class="pre">Semaphore</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.SyncManager.Semaphore" title="Link to this definition">¶</a></dt>
  1983. <dd><p>Create a shared <a class="reference internal" href="threading.html#threading.Semaphore" title="threading.Semaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Semaphore</span></code></a> object and return a proxy for
  1984. it.</p>
  1985. </dd></dl>
  1986. <dl class="py method">
  1987. <dt class="sig sig-object py" id="multiprocessing.managers.SyncManager.Array">
  1988. <span class="sig-name descname"><span class="pre">Array</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">typecode</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sequence</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.SyncManager.Array" title="Link to this definition">¶</a></dt>
  1989. <dd><p>Create an array and return a proxy for it.</p>
  1990. </dd></dl>
  1991. <dl class="py method">
  1992. <dt class="sig sig-object py" id="multiprocessing.managers.SyncManager.Value">
  1993. <span class="sig-name descname"><span class="pre">Value</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">typecode</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.SyncManager.Value" title="Link to this definition">¶</a></dt>
  1994. <dd><p>Create an object with a writable <code class="docutils literal notranslate"><span class="pre">value</span></code> attribute and return a proxy
  1995. for it.</p>
  1996. </dd></dl>
  1997. <dl class="py method">
  1998. <dt class="sig sig-object py" id="multiprocessing.managers.SyncManager.dict">
  1999. <span class="sig-name descname"><span class="pre">dict</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.SyncManager.dict" title="Link to this definition">¶</a></dt>
  2000. <dt class="sig sig-object py">
  2001. <span class="sig-name descname"><span class="pre">dict</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mapping</span></span></em><span class="sig-paren">)</span></dt>
  2002. <dt class="sig sig-object py">
  2003. <span class="sig-name descname"><span class="pre">dict</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sequence</span></span></em><span class="sig-paren">)</span></dt>
  2004. <dd><p>Create a shared <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> object and return a proxy for it.</p>
  2005. </dd></dl>
  2006. <dl class="py method">
  2007. <dt class="sig sig-object py" id="multiprocessing.managers.SyncManager.list">
  2008. <span class="sig-name descname"><span class="pre">list</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.SyncManager.list" title="Link to this definition">¶</a></dt>
  2009. <dt class="sig sig-object py">
  2010. <span class="sig-name descname"><span class="pre">list</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sequence</span></span></em><span class="sig-paren">)</span></dt>
  2011. <dd><p>Create a shared <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> object and return a proxy for it.</p>
  2012. </dd></dl>
  2013. <div class="versionchanged">
  2014. <p><span class="versionmodified changed">Changed in version 3.6: </span>Shared objects are capable of being nested. For example, a shared
  2015. container object such as a shared list can contain other shared objects
  2016. which will all be managed and synchronized by the <a class="reference internal" href="#multiprocessing.managers.SyncManager" title="multiprocessing.managers.SyncManager"><code class="xref py py-class docutils literal notranslate"><span class="pre">SyncManager</span></code></a>.</p>
  2017. </div>
  2018. </dd></dl>
  2019. <dl class="py class">
  2020. <dt class="sig sig-object py" id="multiprocessing.managers.Namespace">
  2021. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.managers.</span></span><span class="sig-name descname"><span class="pre">Namespace</span></span><a class="headerlink" href="#multiprocessing.managers.Namespace" title="Link to this definition">¶</a></dt>
  2022. <dd><p>A type that can register with <a class="reference internal" href="#multiprocessing.managers.SyncManager" title="multiprocessing.managers.SyncManager"><code class="xref py py-class docutils literal notranslate"><span class="pre">SyncManager</span></code></a>.</p>
  2023. <p>A namespace object has no public methods, but does have writable attributes.
  2024. Its representation shows the values of its attributes.</p>
  2025. <p>However, when using a proxy for a namespace object, an attribute beginning
  2026. with <code class="docutils literal notranslate"><span class="pre">'_'</span></code> will be an attribute of the proxy and not an attribute of the
  2027. referent:</p>
  2028. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">mp_context</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="s1">&#39;spawn&#39;</span><span class="p">)</span>
  2029. <span class="gp">&gt;&gt;&gt; </span><span class="n">manager</span> <span class="o">=</span> <span class="n">mp_context</span><span class="o">.</span><span class="n">Manager</span><span class="p">()</span>
  2030. <span class="gp">&gt;&gt;&gt; </span><span class="n">Global</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">Namespace</span><span class="p">()</span>
  2031. <span class="gp">&gt;&gt;&gt; </span><span class="n">Global</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="mi">10</span>
  2032. <span class="gp">&gt;&gt;&gt; </span><span class="n">Global</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="s1">&#39;hello&#39;</span>
  2033. <span class="gp">&gt;&gt;&gt; </span><span class="n">Global</span><span class="o">.</span><span class="n">_z</span> <span class="o">=</span> <span class="mf">12.3</span> <span class="c1"># this is an attribute of the proxy</span>
  2034. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">Global</span><span class="p">)</span>
  2035. <span class="go">Namespace(x=10, y=&#39;hello&#39;)</span>
  2036. </pre></div>
  2037. </div>
  2038. </dd></dl>
  2039. <section id="customized-managers">
  2040. <h4>Customized managers<a class="headerlink" href="#customized-managers" title="Link to this heading">¶</a></h4>
  2041. <p>To create one’s own manager, one creates a subclass of <a class="reference internal" href="#multiprocessing.managers.BaseManager" title="multiprocessing.managers.BaseManager"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseManager</span></code></a> and
  2042. uses the <a class="reference internal" href="#multiprocessing.managers.BaseManager.register" title="multiprocessing.managers.BaseManager.register"><code class="xref py py-meth docutils literal notranslate"><span class="pre">register()</span></code></a> classmethod to register new types or
  2043. callables with the manager class. For example:</p>
  2044. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing.managers</span> <span class="kn">import</span> <span class="n">BaseManager</span>
  2045. <span class="k">class</span> <span class="nc">MathsClass</span><span class="p">:</span>
  2046. <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
  2047. <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span>
  2048. <span class="k">def</span> <span class="nf">mul</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
  2049. <span class="k">return</span> <span class="n">x</span> <span class="o">*</span> <span class="n">y</span>
  2050. <span class="k">class</span> <span class="nc">MyManager</span><span class="p">(</span><span class="n">BaseManager</span><span class="p">):</span>
  2051. <span class="k">pass</span>
  2052. <span class="n">MyManager</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;Maths&#39;</span><span class="p">,</span> <span class="n">MathsClass</span><span class="p">)</span>
  2053. <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>
  2054. <span class="k">with</span> <span class="n">MyManager</span><span class="p">()</span> <span class="k">as</span> <span class="n">manager</span><span class="p">:</span>
  2055. <span class="n">maths</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">Maths</span><span class="p">()</span>
  2056. <span class="nb">print</span><span class="p">(</span><span class="n">maths</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span> <span class="c1"># prints 7</span>
  2057. <span class="nb">print</span><span class="p">(</span><span class="n">maths</span><span class="o">.</span><span class="n">mul</span><span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">))</span> <span class="c1"># prints 56</span>
  2058. </pre></div>
  2059. </div>
  2060. </section>
  2061. <section id="using-a-remote-manager">
  2062. <h4>Using a remote manager<a class="headerlink" href="#using-a-remote-manager" title="Link to this heading">¶</a></h4>
  2063. <p>It is possible to run a manager server on one machine and have clients use it
  2064. from other machines (assuming that the firewalls involved allow it).</p>
  2065. <p>Running the following commands creates a server for a single shared queue which
  2066. remote clients can access:</p>
  2067. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">multiprocessing.managers</span> <span class="kn">import</span> <span class="n">BaseManager</span>
  2068. <span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">queue</span> <span class="kn">import</span> <span class="n">Queue</span>
  2069. <span class="gp">&gt;&gt;&gt; </span><span class="n">queue</span> <span class="o">=</span> <span class="n">Queue</span><span class="p">()</span>
  2070. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">QueueManager</span><span class="p">(</span><span class="n">BaseManager</span><span class="p">):</span> <span class="k">pass</span>
  2071. <span class="gp">&gt;&gt;&gt; </span><span class="n">QueueManager</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;get_queue&#39;</span><span class="p">,</span> <span class="nb">callable</span><span class="o">=</span><span class="k">lambda</span><span class="p">:</span><span class="n">queue</span><span class="p">)</span>
  2072. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">QueueManager</span><span class="p">(</span><span class="n">address</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="mi">50000</span><span class="p">),</span> <span class="n">authkey</span><span class="o">=</span><span class="sa">b</span><span class="s1">&#39;abracadabra&#39;</span><span class="p">)</span>
  2073. <span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">get_server</span><span class="p">()</span>
  2074. <span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
  2075. </pre></div>
  2076. </div>
  2077. <p>One client can access the server as follows:</p>
  2078. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">multiprocessing.managers</span> <span class="kn">import</span> <span class="n">BaseManager</span>
  2079. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">QueueManager</span><span class="p">(</span><span class="n">BaseManager</span><span class="p">):</span> <span class="k">pass</span>
  2080. <span class="gp">&gt;&gt;&gt; </span><span class="n">QueueManager</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;get_queue&#39;</span><span class="p">)</span>
  2081. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">QueueManager</span><span class="p">(</span><span class="n">address</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;foo.bar.org&#39;</span><span class="p">,</span> <span class="mi">50000</span><span class="p">),</span> <span class="n">authkey</span><span class="o">=</span><span class="sa">b</span><span class="s1">&#39;abracadabra&#39;</span><span class="p">)</span>
  2082. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
  2083. <span class="gp">&gt;&gt;&gt; </span><span class="n">queue</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">get_queue</span><span class="p">()</span>
  2084. <span class="gp">&gt;&gt;&gt; </span><span class="n">queue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="s1">&#39;hello&#39;</span><span class="p">)</span>
  2085. </pre></div>
  2086. </div>
  2087. <p>Another client can also use it:</p>
  2088. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">multiprocessing.managers</span> <span class="kn">import</span> <span class="n">BaseManager</span>
  2089. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">QueueManager</span><span class="p">(</span><span class="n">BaseManager</span><span class="p">):</span> <span class="k">pass</span>
  2090. <span class="gp">&gt;&gt;&gt; </span><span class="n">QueueManager</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;get_queue&#39;</span><span class="p">)</span>
  2091. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">QueueManager</span><span class="p">(</span><span class="n">address</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;foo.bar.org&#39;</span><span class="p">,</span> <span class="mi">50000</span><span class="p">),</span> <span class="n">authkey</span><span class="o">=</span><span class="sa">b</span><span class="s1">&#39;abracadabra&#39;</span><span class="p">)</span>
  2092. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
  2093. <span class="gp">&gt;&gt;&gt; </span><span class="n">queue</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">get_queue</span><span class="p">()</span>
  2094. <span class="gp">&gt;&gt;&gt; </span><span class="n">queue</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
  2095. <span class="go">&#39;hello&#39;</span>
  2096. </pre></div>
  2097. </div>
  2098. <p>Local processes can also access that queue, using the code from above on the
  2099. client to access it remotely:</p>
  2100. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">Queue</span>
  2101. <span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">multiprocessing.managers</span> <span class="kn">import</span> <span class="n">BaseManager</span>
  2102. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Worker</span><span class="p">(</span><span class="n">Process</span><span class="p">):</span>
  2103. <span class="gp">... </span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">q</span><span class="p">):</span>
  2104. <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">=</span> <span class="n">q</span>
  2105. <span class="gp">... </span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
  2106. <span class="gp">... </span> <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  2107. <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="s1">&#39;local hello&#39;</span><span class="p">)</span>
  2108. <span class="gp">...</span>
  2109. <span class="gp">&gt;&gt;&gt; </span><span class="n">queue</span> <span class="o">=</span> <span class="n">Queue</span><span class="p">()</span>
  2110. <span class="gp">&gt;&gt;&gt; </span><span class="n">w</span> <span class="o">=</span> <span class="n">Worker</span><span class="p">(</span><span class="n">queue</span><span class="p">)</span>
  2111. <span class="gp">&gt;&gt;&gt; </span><span class="n">w</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  2112. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">QueueManager</span><span class="p">(</span><span class="n">BaseManager</span><span class="p">):</span> <span class="k">pass</span>
  2113. <span class="gp">...</span>
  2114. <span class="gp">&gt;&gt;&gt; </span><span class="n">QueueManager</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;get_queue&#39;</span><span class="p">,</span> <span class="nb">callable</span><span class="o">=</span><span class="k">lambda</span><span class="p">:</span> <span class="n">queue</span><span class="p">)</span>
  2115. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">QueueManager</span><span class="p">(</span><span class="n">address</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="mi">50000</span><span class="p">),</span> <span class="n">authkey</span><span class="o">=</span><span class="sa">b</span><span class="s1">&#39;abracadabra&#39;</span><span class="p">)</span>
  2116. <span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">get_server</span><span class="p">()</span>
  2117. <span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
  2118. </pre></div>
  2119. </div>
  2120. </section>
  2121. </section>
  2122. <section id="proxy-objects">
  2123. <span id="multiprocessing-proxy-objects"></span><h3>Proxy Objects<a class="headerlink" href="#proxy-objects" title="Link to this heading">¶</a></h3>
  2124. <p>A proxy is an object which <em>refers</em> to a shared object which lives (presumably)
  2125. in a different process. The shared object is said to be the <em>referent</em> of the
  2126. proxy. Multiple proxy objects may have the same referent.</p>
  2127. <p>A proxy object has methods which invoke corresponding methods of its referent
  2128. (although not every method of the referent will necessarily be available through
  2129. the proxy). In this way, a proxy can be used just like its referent can:</p>
  2130. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">mp_context</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="s1">&#39;spawn&#39;</span><span class="p">)</span>
  2131. <span class="gp">&gt;&gt;&gt; </span><span class="n">manager</span> <span class="o">=</span> <span class="n">mp_context</span><span class="o">.</span><span class="n">Manager</span><span class="p">()</span>
  2132. <span class="gp">&gt;&gt;&gt; </span><span class="n">l</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">list</span><span class="p">([</span><span class="n">i</span><span class="o">*</span><span class="n">i</span> <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">10</span><span class="p">)])</span>
  2133. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
  2134. <span class="go">[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</span>
  2135. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">l</span><span class="p">))</span>
  2136. <span class="go">&lt;ListProxy object, typeid &#39;list&#39; at 0x...&gt;</span>
  2137. <span class="gp">&gt;&gt;&gt; </span><span class="n">l</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span>
  2138. <span class="go">16</span>
  2139. <span class="gp">&gt;&gt;&gt; </span><span class="n">l</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">5</span><span class="p">]</span>
  2140. <span class="go">[4, 9, 16]</span>
  2141. </pre></div>
  2142. </div>
  2143. <p>Notice that applying <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> to a proxy will return the representation of
  2144. the referent, whereas applying <a class="reference internal" href="functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> will return the representation of
  2145. the proxy.</p>
  2146. <p>An important feature of proxy objects is that they are picklable so they can be
  2147. passed between processes. As such, a referent can contain
  2148. <a class="reference internal" href="#multiprocessing-proxy-objects"><span class="std std-ref">Proxy Objects</span></a>. This permits nesting of these managed
  2149. lists, dicts, and other <a class="reference internal" href="#multiprocessing-proxy-objects"><span class="std std-ref">Proxy Objects</span></a>:</p>
  2150. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">list</span><span class="p">()</span>
  2151. <span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">list</span><span class="p">()</span>
  2152. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="c1"># referent of a now contains referent of b</span>
  2153. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
  2154. <span class="go">[&lt;ListProxy object, typeid &#39;list&#39; at ...&gt;] []</span>
  2155. <span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;hello&#39;</span><span class="p">)</span>
  2156. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">b</span><span class="p">)</span>
  2157. <span class="go">[&#39;hello&#39;] [&#39;hello&#39;]</span>
  2158. </pre></div>
  2159. </div>
  2160. <p>Similarly, dict and list proxies may be nested inside one another:</p>
  2161. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">l_outer</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">list</span><span class="p">([</span> <span class="n">manager</span><span class="o">.</span><span class="n">dict</span><span class="p">()</span> <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">2</span><span class="p">)</span> <span class="p">])</span>
  2162. <span class="gp">&gt;&gt;&gt; </span><span class="n">d_first_inner</span> <span class="o">=</span> <span class="n">l_outer</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
  2163. <span class="gp">&gt;&gt;&gt; </span><span class="n">d_first_inner</span><span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
  2164. <span class="gp">&gt;&gt;&gt; </span><span class="n">d_first_inner</span><span class="p">[</span><span class="s1">&#39;b&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span>
  2165. <span class="gp">&gt;&gt;&gt; </span><span class="n">l_outer</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="s1">&#39;c&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">3</span>
  2166. <span class="gp">&gt;&gt;&gt; </span><span class="n">l_outer</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="s1">&#39;z&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">26</span>
  2167. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">l_outer</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
  2168. <span class="go">{&#39;a&#39;: 1, &#39;b&#39;: 2}</span>
  2169. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">l_outer</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
  2170. <span class="go">{&#39;c&#39;: 3, &#39;z&#39;: 26}</span>
  2171. </pre></div>
  2172. </div>
  2173. <p>If standard (non-proxy) <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> or <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> objects are contained
  2174. in a referent, modifications to those mutable values will not be propagated
  2175. through the manager because the proxy has no way of knowing when the values
  2176. contained within are modified. However, storing a value in a container proxy
  2177. (which triggers a <code class="docutils literal notranslate"><span class="pre">__setitem__</span></code> on the proxy object) does propagate through
  2178. the manager and so to effectively modify such an item, one could re-assign the
  2179. modified value to the container proxy:</p>
  2180. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># create a list proxy and append a mutable object (a dictionary)</span>
  2181. <span class="n">lproxy</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">list</span><span class="p">()</span>
  2182. <span class="n">lproxy</span><span class="o">.</span><span class="n">append</span><span class="p">({})</span>
  2183. <span class="c1"># now mutate the dictionary</span>
  2184. <span class="n">d</span> <span class="o">=</span> <span class="n">lproxy</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
  2185. <span class="n">d</span><span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
  2186. <span class="n">d</span><span class="p">[</span><span class="s1">&#39;b&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span>
  2187. <span class="c1"># at this point, the changes to d are not yet synced, but by</span>
  2188. <span class="c1"># updating the dictionary, the proxy is notified of the change</span>
  2189. <span class="n">lproxy</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">d</span>
  2190. </pre></div>
  2191. </div>
  2192. <p>This approach is perhaps less convenient than employing nested
  2193. <a class="reference internal" href="#multiprocessing-proxy-objects"><span class="std std-ref">Proxy Objects</span></a> for most use cases but also
  2194. demonstrates a level of control over the synchronization.</p>
  2195. <div class="admonition note">
  2196. <p class="admonition-title">Note</p>
  2197. <p>The proxy types in <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> do nothing to support comparisons
  2198. by value. So, for instance, we have:</p>
  2199. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">manager</span><span class="o">.</span><span class="n">list</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span> <span class="o">==</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]</span>
  2200. <span class="go">False</span>
  2201. </pre></div>
  2202. </div>
  2203. <p>One should just use a copy of the referent instead when making comparisons.</p>
  2204. </div>
  2205. <dl class="py class">
  2206. <dt class="sig sig-object py" id="multiprocessing.managers.BaseProxy">
  2207. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.managers.</span></span><span class="sig-name descname"><span class="pre">BaseProxy</span></span><a class="headerlink" href="#multiprocessing.managers.BaseProxy" title="Link to this definition">¶</a></dt>
  2208. <dd><p>Proxy objects are instances of subclasses of <a class="reference internal" href="#multiprocessing.managers.BaseProxy" title="multiprocessing.managers.BaseProxy"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseProxy</span></code></a>.</p>
  2209. <dl class="py method">
  2210. <dt class="sig sig-object py" id="multiprocessing.managers.BaseProxy._callmethod">
  2211. <span class="sig-name descname"><span class="pre">_callmethod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">methodname</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">args</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">kwds</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.BaseProxy._callmethod" title="Link to this definition">¶</a></dt>
  2212. <dd><p>Call and return the result of a method of the proxy’s referent.</p>
  2213. <p>If <code class="docutils literal notranslate"><span class="pre">proxy</span></code> is a proxy whose referent is <code class="docutils literal notranslate"><span class="pre">obj</span></code> then the expression</p>
  2214. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">proxy</span><span class="o">.</span><span class="n">_callmethod</span><span class="p">(</span><span class="n">methodname</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwds</span><span class="p">)</span>
  2215. </pre></div>
  2216. </div>
  2217. <p>will evaluate the expression</p>
  2218. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">methodname</span><span class="p">)(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>
  2219. </pre></div>
  2220. </div>
  2221. <p>in the manager’s process.</p>
  2222. <p>The returned value will be a copy of the result of the call or a proxy to
  2223. a new shared object – see documentation for the <em>method_to_typeid</em>
  2224. argument of <a class="reference internal" href="#multiprocessing.managers.BaseManager.register" title="multiprocessing.managers.BaseManager.register"><code class="xref py py-meth docutils literal notranslate"><span class="pre">BaseManager.register()</span></code></a>.</p>
  2225. <p>If an exception is raised by the call, then is re-raised by
  2226. <a class="reference internal" href="#multiprocessing.managers.BaseProxy._callmethod" title="multiprocessing.managers.BaseProxy._callmethod"><code class="xref py py-meth docutils literal notranslate"><span class="pre">_callmethod()</span></code></a>. If some other exception is raised in the manager’s
  2227. process then this is converted into a <code class="xref py py-exc docutils literal notranslate"><span class="pre">RemoteError</span></code> exception and is
  2228. raised by <a class="reference internal" href="#multiprocessing.managers.BaseProxy._callmethod" title="multiprocessing.managers.BaseProxy._callmethod"><code class="xref py py-meth docutils literal notranslate"><span class="pre">_callmethod()</span></code></a>.</p>
  2229. <p>Note in particular that an exception will be raised if <em>methodname</em> has
  2230. not been <em>exposed</em>.</p>
  2231. <p>An example of the usage of <a class="reference internal" href="#multiprocessing.managers.BaseProxy._callmethod" title="multiprocessing.managers.BaseProxy._callmethod"><code class="xref py py-meth docutils literal notranslate"><span class="pre">_callmethod()</span></code></a>:</p>
  2232. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">l</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>
  2233. <span class="gp">&gt;&gt;&gt; </span><span class="n">l</span><span class="o">.</span><span class="n">_callmethod</span><span class="p">(</span><span class="s1">&#39;__len__&#39;</span><span class="p">)</span>
  2234. <span class="go">10</span>
  2235. <span class="gp">&gt;&gt;&gt; </span><span class="n">l</span><span class="o">.</span><span class="n">_callmethod</span><span class="p">(</span><span class="s1">&#39;__getitem__&#39;</span><span class="p">,</span> <span class="p">(</span><span class="nb">slice</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">7</span><span class="p">),))</span> <span class="c1"># equivalent to l[2:7]</span>
  2236. <span class="go">[2, 3, 4, 5, 6]</span>
  2237. <span class="gp">&gt;&gt;&gt; </span><span class="n">l</span><span class="o">.</span><span class="n">_callmethod</span><span class="p">(</span><span class="s1">&#39;__getitem__&#39;</span><span class="p">,</span> <span class="p">(</span><span class="mi">20</span><span class="p">,))</span> <span class="c1"># equivalent to l[20]</span>
  2238. <span class="gt">Traceback (most recent call last):</span>
  2239. <span class="c">...</span>
  2240. <span class="gr">IndexError</span>: <span class="n">list index out of range</span>
  2241. </pre></div>
  2242. </div>
  2243. </dd></dl>
  2244. <dl class="py method">
  2245. <dt class="sig sig-object py" id="multiprocessing.managers.BaseProxy._getvalue">
  2246. <span class="sig-name descname"><span class="pre">_getvalue</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.BaseProxy._getvalue" title="Link to this definition">¶</a></dt>
  2247. <dd><p>Return a copy of the referent.</p>
  2248. <p>If the referent is unpicklable then this will raise an exception.</p>
  2249. </dd></dl>
  2250. <dl class="py method">
  2251. <dt class="sig sig-object py" id="multiprocessing.managers.BaseProxy.__repr__">
  2252. <span class="sig-name descname"><span class="pre">__repr__</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.BaseProxy.__repr__" title="Link to this definition">¶</a></dt>
  2253. <dd><p>Return a representation of the proxy object.</p>
  2254. </dd></dl>
  2255. <dl class="py method">
  2256. <dt class="sig sig-object py" id="multiprocessing.managers.BaseProxy.__str__">
  2257. <span class="sig-name descname"><span class="pre">__str__</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.managers.BaseProxy.__str__" title="Link to this definition">¶</a></dt>
  2258. <dd><p>Return the representation of the referent.</p>
  2259. </dd></dl>
  2260. </dd></dl>
  2261. <section id="cleanup">
  2262. <h4>Cleanup<a class="headerlink" href="#cleanup" title="Link to this heading">¶</a></h4>
  2263. <p>A proxy object uses a weakref callback so that when it gets garbage collected it
  2264. deregisters itself from the manager which owns its referent.</p>
  2265. <p>A shared object gets deleted from the manager process when there are no longer
  2266. any proxies referring to it.</p>
  2267. </section>
  2268. </section>
  2269. <section id="module-multiprocessing.pool">
  2270. <span id="process-pools"></span><h3>Process Pools<a class="headerlink" href="#module-multiprocessing.pool" title="Link to this heading">¶</a></h3>
  2271. <p>One can create a pool of processes which will carry out tasks submitted to it
  2272. with the <a class="reference internal" href="#multiprocessing.pool.Pool" title="multiprocessing.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a> class.</p>
  2273. <dl class="py class">
  2274. <dt class="sig sig-object py" id="multiprocessing.pool.Pool">
  2275. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.pool.</span></span><span class="sig-name descname"><span class="pre">Pool</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">processes</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">initializer</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">initargs</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">maxtasksperchild</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">context</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.pool.Pool" title="Link to this definition">¶</a></dt>
  2276. <dd><p>A process pool object which controls a pool of worker processes to which jobs
  2277. can be submitted. It supports asynchronous results with timeouts and
  2278. callbacks and has a parallel map implementation.</p>
  2279. <p><em>processes</em> is the number of worker processes to use. If <em>processes</em> is
  2280. <code class="docutils literal notranslate"><span class="pre">None</span></code> then the number returned by <a class="reference internal" href="os.html#os.cpu_count" title="os.cpu_count"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.cpu_count()</span></code></a> is used.</p>
  2281. <p>If <em>initializer</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code> then each worker process will call
  2282. <code class="docutils literal notranslate"><span class="pre">initializer(*initargs)</span></code> when it starts.</p>
  2283. <p><em>maxtasksperchild</em> is the number of tasks a worker process can complete
  2284. before it will exit and be replaced with a fresh worker process, to enable
  2285. unused resources to be freed. The default <em>maxtasksperchild</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, which
  2286. means worker processes will live as long as the pool.</p>
  2287. <p><em>context</em> can be used to specify the context used for starting
  2288. the worker processes. Usually a pool is created using the
  2289. function <code class="xref py py-func docutils literal notranslate"><span class="pre">multiprocessing.Pool()</span></code> or the <a class="reference internal" href="#multiprocessing.pool.Pool" title="multiprocessing.pool.Pool"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Pool()</span></code></a> method
  2290. of a context object. In both cases <em>context</em> is set
  2291. appropriately.</p>
  2292. <p>Note that the methods of the pool object should only be called by
  2293. the process which created the pool.</p>
  2294. <div class="admonition warning">
  2295. <p class="admonition-title">Warning</p>
  2296. <p><a class="reference internal" href="#module-multiprocessing.pool" title="multiprocessing.pool: Create pools of processes."><code class="xref py py-class docutils literal notranslate"><span class="pre">multiprocessing.pool</span></code></a> objects have internal resources that need to be
  2297. properly managed (like any other resource) by using the pool as a context manager
  2298. or by calling <a class="reference internal" href="#multiprocessing.pool.Pool.close" title="multiprocessing.pool.Pool.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> and <a class="reference internal" href="#multiprocessing.pool.Pool.terminate" title="multiprocessing.pool.Pool.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">terminate()</span></code></a> manually. Failure to do this
  2299. can lead to the process hanging on finalization.</p>
  2300. <p>Note that it is <strong>not correct</strong> to rely on the garbage collector to destroy the pool
  2301. as CPython does not assure that the finalizer of the pool will be called
  2302. (see <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">object.__del__()</span></code></a> for more information).</p>
  2303. </div>
  2304. <div class="versionchanged">
  2305. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added the <em>maxtasksperchild</em> parameter.</p>
  2306. </div>
  2307. <div class="versionchanged">
  2308. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added the <em>context</em> parameter.</p>
  2309. </div>
  2310. <div class="admonition note">
  2311. <p class="admonition-title">Note</p>
  2312. <p>Worker processes within a <a class="reference internal" href="#multiprocessing.pool.Pool" title="multiprocessing.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a> typically live for the complete
  2313. duration of the Pool’s work queue. A frequent pattern found in other
  2314. systems (such as Apache, mod_wsgi, etc) to free resources held by
  2315. workers is to allow a worker within a pool to complete only a set
  2316. amount of work before being exiting, being cleaned up and a new
  2317. process spawned to replace the old one. The <em>maxtasksperchild</em>
  2318. argument to the <a class="reference internal" href="#multiprocessing.pool.Pool" title="multiprocessing.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a> exposes this ability to the end user.</p>
  2319. </div>
  2320. <dl class="py method">
  2321. <dt class="sig sig-object py" id="multiprocessing.pool.Pool.apply">
  2322. <span class="sig-name descname"><span class="pre">apply</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">args</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">kwds</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.pool.Pool.apply" title="Link to this definition">¶</a></dt>
  2323. <dd><p>Call <em>func</em> with arguments <em>args</em> and keyword arguments <em>kwds</em>. It blocks
  2324. until the result is ready. Given this blocks, <a class="reference internal" href="#multiprocessing.pool.Pool.apply_async" title="multiprocessing.pool.Pool.apply_async"><code class="xref py py-meth docutils literal notranslate"><span class="pre">apply_async()</span></code></a> is
  2325. better suited for performing work in parallel. Additionally, <em>func</em>
  2326. is only executed in one of the workers of the pool.</p>
  2327. </dd></dl>
  2328. <dl class="py method">
  2329. <dt class="sig sig-object py" id="multiprocessing.pool.Pool.apply_async">
  2330. <span class="sig-name descname"><span class="pre">apply_async</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">args</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">kwds</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">callback</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">error_callback</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.pool.Pool.apply_async" title="Link to this definition">¶</a></dt>
  2331. <dd><p>A variant of the <a class="reference internal" href="#multiprocessing.pool.Pool.apply" title="multiprocessing.pool.Pool.apply"><code class="xref py py-meth docutils literal notranslate"><span class="pre">apply()</span></code></a> method which returns a
  2332. <a class="reference internal" href="#multiprocessing.pool.AsyncResult" title="multiprocessing.pool.AsyncResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncResult</span></code></a> object.</p>
  2333. <p>If <em>callback</em> is specified then it should be a callable which accepts a
  2334. single argument. When the result becomes ready <em>callback</em> is applied to
  2335. it, that is unless the call failed, in which case the <em>error_callback</em>
  2336. is applied instead.</p>
  2337. <p>If <em>error_callback</em> is specified then it should be a callable which
  2338. accepts a single argument. If the target function fails, then
  2339. the <em>error_callback</em> is called with the exception instance.</p>
  2340. <p>Callbacks should complete immediately since otherwise the thread which
  2341. handles the results will get blocked.</p>
  2342. </dd></dl>
  2343. <dl class="py method">
  2344. <dt class="sig sig-object py" id="multiprocessing.pool.Pool.map">
  2345. <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">func</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">chunksize</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.pool.Pool.map" title="Link to this definition">¶</a></dt>
  2346. <dd><p>A parallel equivalent of the <a class="reference internal" href="functions.html#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a> built-in function (it supports only
  2347. one <em>iterable</em> argument though, for multiple iterables see <a class="reference internal" href="#multiprocessing.pool.Pool.starmap" title="multiprocessing.pool.Pool.starmap"><code class="xref py py-meth docutils literal notranslate"><span class="pre">starmap()</span></code></a>).
  2348. It blocks until the result is ready.</p>
  2349. <p>This method chops the iterable into a number of chunks which it submits to
  2350. the process pool as separate tasks. The (approximate) size of these
  2351. chunks can be specified by setting <em>chunksize</em> to a positive integer.</p>
  2352. <p>Note that it may cause high memory usage for very long iterables. Consider
  2353. using <a class="reference internal" href="#multiprocessing.pool.Pool.imap" title="multiprocessing.pool.Pool.imap"><code class="xref py py-meth docutils literal notranslate"><span class="pre">imap()</span></code></a> or <a class="reference internal" href="#multiprocessing.pool.Pool.imap_unordered" title="multiprocessing.pool.Pool.imap_unordered"><code class="xref py py-meth docutils literal notranslate"><span class="pre">imap_unordered()</span></code></a> with explicit <em>chunksize</em>
  2354. option for better efficiency.</p>
  2355. </dd></dl>
  2356. <dl class="py method">
  2357. <dt class="sig sig-object py" id="multiprocessing.pool.Pool.map_async">
  2358. <span class="sig-name descname"><span class="pre">map_async</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">chunksize</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">callback</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">error_callback</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.pool.Pool.map_async" title="Link to this definition">¶</a></dt>
  2359. <dd><p>A variant of the <a class="reference internal" href="#multiprocessing.pool.Pool.map" title="multiprocessing.pool.Pool.map"><code class="xref py py-meth docutils literal notranslate"><span class="pre">map()</span></code></a> method which returns a
  2360. <a class="reference internal" href="#multiprocessing.pool.AsyncResult" title="multiprocessing.pool.AsyncResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncResult</span></code></a> object.</p>
  2361. <p>If <em>callback</em> is specified then it should be a callable which accepts a
  2362. single argument. When the result becomes ready <em>callback</em> is applied to
  2363. it, that is unless the call failed, in which case the <em>error_callback</em>
  2364. is applied instead.</p>
  2365. <p>If <em>error_callback</em> is specified then it should be a callable which
  2366. accepts a single argument. If the target function fails, then
  2367. the <em>error_callback</em> is called with the exception instance.</p>
  2368. <p>Callbacks should complete immediately since otherwise the thread which
  2369. handles the results will get blocked.</p>
  2370. </dd></dl>
  2371. <dl class="py method">
  2372. <dt class="sig sig-object py" id="multiprocessing.pool.Pool.imap">
  2373. <span class="sig-name descname"><span class="pre">imap</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">chunksize</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.pool.Pool.imap" title="Link to this definition">¶</a></dt>
  2374. <dd><p>A lazier version of <a class="reference internal" href="#multiprocessing.pool.Pool.map" title="multiprocessing.pool.Pool.map"><code class="xref py py-meth docutils literal notranslate"><span class="pre">map()</span></code></a>.</p>
  2375. <p>The <em>chunksize</em> argument is the same as the one used by the <a class="reference internal" href="#multiprocessing.pool.Pool.map" title="multiprocessing.pool.Pool.map"><code class="xref py py-meth docutils literal notranslate"><span class="pre">map()</span></code></a>
  2376. method. For very long iterables using a large value for <em>chunksize</em> can
  2377. make the job complete <strong>much</strong> faster than using the default value of
  2378. <code class="docutils literal notranslate"><span class="pre">1</span></code>.</p>
  2379. <p>Also if <em>chunksize</em> is <code class="docutils literal notranslate"><span class="pre">1</span></code> then the <code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code> method of the iterator
  2380. returned by the <a class="reference internal" href="#multiprocessing.pool.Pool.imap" title="multiprocessing.pool.Pool.imap"><code class="xref py py-meth docutils literal notranslate"><span class="pre">imap()</span></code></a> method has an optional <em>timeout</em> parameter:
  2381. <code class="docutils literal notranslate"><span class="pre">next(timeout)</span></code> will raise <a class="reference internal" href="#multiprocessing.TimeoutError" title="multiprocessing.TimeoutError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">multiprocessing.TimeoutError</span></code></a> if the
  2382. result cannot be returned within <em>timeout</em> seconds.</p>
  2383. </dd></dl>
  2384. <dl class="py method">
  2385. <dt class="sig sig-object py" id="multiprocessing.pool.Pool.imap_unordered">
  2386. <span class="sig-name descname"><span class="pre">imap_unordered</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">chunksize</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.pool.Pool.imap_unordered" title="Link to this definition">¶</a></dt>
  2387. <dd><p>The same as <a class="reference internal" href="#multiprocessing.pool.Pool.imap" title="multiprocessing.pool.Pool.imap"><code class="xref py py-meth docutils literal notranslate"><span class="pre">imap()</span></code></a> except that the ordering of the results from the
  2388. returned iterator should be considered arbitrary. (Only when there is
  2389. only one worker process is the order guaranteed to be “correct”.)</p>
  2390. </dd></dl>
  2391. <dl class="py method">
  2392. <dt class="sig sig-object py" id="multiprocessing.pool.Pool.starmap">
  2393. <span class="sig-name descname"><span class="pre">starmap</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">chunksize</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.pool.Pool.starmap" title="Link to this definition">¶</a></dt>
  2394. <dd><p>Like <a class="reference internal" href="#multiprocessing.pool.Pool.map" title="multiprocessing.pool.Pool.map"><code class="xref py py-meth docutils literal notranslate"><span class="pre">map()</span></code></a> except that the
  2395. elements of the <em>iterable</em> are expected to be iterables that are
  2396. unpacked as arguments.</p>
  2397. <p>Hence an <em>iterable</em> of <code class="docutils literal notranslate"><span class="pre">[(1,2),</span> <span class="pre">(3,</span> <span class="pre">4)]</span></code> results in <code class="docutils literal notranslate"><span class="pre">[func(1,2),</span>
  2398. <span class="pre">func(3,4)]</span></code>.</p>
  2399. <div class="versionadded">
  2400. <p><span class="versionmodified added">New in version 3.3.</span></p>
  2401. </div>
  2402. </dd></dl>
  2403. <dl class="py method">
  2404. <dt class="sig sig-object py" id="multiprocessing.pool.Pool.starmap_async">
  2405. <span class="sig-name descname"><span class="pre">starmap_async</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">chunksize</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">callback</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">error_callback</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.pool.Pool.starmap_async" title="Link to this definition">¶</a></dt>
  2406. <dd><p>A combination of <a class="reference internal" href="#multiprocessing.pool.Pool.starmap" title="multiprocessing.pool.Pool.starmap"><code class="xref py py-meth docutils literal notranslate"><span class="pre">starmap()</span></code></a> and <a class="reference internal" href="#multiprocessing.pool.Pool.map_async" title="multiprocessing.pool.Pool.map_async"><code class="xref py py-meth docutils literal notranslate"><span class="pre">map_async()</span></code></a> that iterates over
  2407. <em>iterable</em> of iterables and calls <em>func</em> with the iterables unpacked.
  2408. Returns a result object.</p>
  2409. <div class="versionadded">
  2410. <p><span class="versionmodified added">New in version 3.3.</span></p>
  2411. </div>
  2412. </dd></dl>
  2413. <dl class="py method">
  2414. <dt class="sig sig-object py" id="multiprocessing.pool.Pool.close">
  2415. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.pool.Pool.close" title="Link to this definition">¶</a></dt>
  2416. <dd><p>Prevents any more tasks from being submitted to the pool. Once all the
  2417. tasks have been completed the worker processes will exit.</p>
  2418. </dd></dl>
  2419. <dl class="py method">
  2420. <dt class="sig sig-object py" id="multiprocessing.pool.Pool.terminate">
  2421. <span class="sig-name descname"><span class="pre">terminate</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.pool.Pool.terminate" title="Link to this definition">¶</a></dt>
  2422. <dd><p>Stops the worker processes immediately without completing outstanding
  2423. work. When the pool object is garbage collected <a class="reference internal" href="#multiprocessing.pool.Pool.terminate" title="multiprocessing.pool.Pool.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">terminate()</span></code></a> will be
  2424. called immediately.</p>
  2425. </dd></dl>
  2426. <dl class="py method">
  2427. <dt class="sig sig-object py" id="multiprocessing.pool.Pool.join">
  2428. <span class="sig-name descname"><span class="pre">join</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.pool.Pool.join" title="Link to this definition">¶</a></dt>
  2429. <dd><p>Wait for the worker processes to exit. One must call <a class="reference internal" href="#multiprocessing.pool.Pool.close" title="multiprocessing.pool.Pool.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> or
  2430. <a class="reference internal" href="#multiprocessing.pool.Pool.terminate" title="multiprocessing.pool.Pool.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">terminate()</span></code></a> before using <a class="reference internal" href="#multiprocessing.pool.Pool.join" title="multiprocessing.pool.Pool.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a>.</p>
  2431. </dd></dl>
  2432. <div class="versionchanged">
  2433. <p><span class="versionmodified changed">Changed in version 3.3: </span>Pool objects now support the context management protocol – see
  2434. <a class="reference internal" href="stdtypes.html#typecontextmanager"><span class="std std-ref">Context Manager Types</span></a>. <a class="reference internal" href="stdtypes.html#contextmanager.__enter__" title="contextmanager.__enter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__enter__()</span></code></a> returns the
  2435. pool object, and <a class="reference internal" href="stdtypes.html#contextmanager.__exit__" title="contextmanager.__exit__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__exit__()</span></code></a> calls <a class="reference internal" href="#multiprocessing.pool.Pool.terminate" title="multiprocessing.pool.Pool.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">terminate()</span></code></a>.</p>
  2436. </div>
  2437. </dd></dl>
  2438. <dl class="py class">
  2439. <dt class="sig sig-object py" id="multiprocessing.pool.AsyncResult">
  2440. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.pool.</span></span><span class="sig-name descname"><span class="pre">AsyncResult</span></span><a class="headerlink" href="#multiprocessing.pool.AsyncResult" title="Link to this definition">¶</a></dt>
  2441. <dd><p>The class of the result returned by <a class="reference internal" href="#multiprocessing.pool.Pool.apply_async" title="multiprocessing.pool.Pool.apply_async"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Pool.apply_async()</span></code></a> and
  2442. <a class="reference internal" href="#multiprocessing.pool.Pool.map_async" title="multiprocessing.pool.Pool.map_async"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Pool.map_async()</span></code></a>.</p>
  2443. <dl class="py method">
  2444. <dt class="sig sig-object py" id="multiprocessing.pool.AsyncResult.get">
  2445. <span class="sig-name descname"><span class="pre">get</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">timeout</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.pool.AsyncResult.get" title="Link to this definition">¶</a></dt>
  2446. <dd><p>Return the result when it arrives. If <em>timeout</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code> and the
  2447. result does not arrive within <em>timeout</em> seconds then
  2448. <a class="reference internal" href="#multiprocessing.TimeoutError" title="multiprocessing.TimeoutError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">multiprocessing.TimeoutError</span></code></a> is raised. If the remote call raised
  2449. an exception then that exception will be reraised by <a class="reference internal" href="#multiprocessing.pool.AsyncResult.get" title="multiprocessing.pool.AsyncResult.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code></a>.</p>
  2450. </dd></dl>
  2451. <dl class="py method">
  2452. <dt class="sig sig-object py" id="multiprocessing.pool.AsyncResult.wait">
  2453. <span class="sig-name descname"><span class="pre">wait</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">timeout</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.pool.AsyncResult.wait" title="Link to this definition">¶</a></dt>
  2454. <dd><p>Wait until the result is available or until <em>timeout</em> seconds pass.</p>
  2455. </dd></dl>
  2456. <dl class="py method">
  2457. <dt class="sig sig-object py" id="multiprocessing.pool.AsyncResult.ready">
  2458. <span class="sig-name descname"><span class="pre">ready</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.pool.AsyncResult.ready" title="Link to this definition">¶</a></dt>
  2459. <dd><p>Return whether the call has completed.</p>
  2460. </dd></dl>
  2461. <dl class="py method">
  2462. <dt class="sig sig-object py" id="multiprocessing.pool.AsyncResult.successful">
  2463. <span class="sig-name descname"><span class="pre">successful</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.pool.AsyncResult.successful" title="Link to this definition">¶</a></dt>
  2464. <dd><p>Return whether the call completed without raising an exception. Will
  2465. raise <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> if the result is not ready.</p>
  2466. <div class="versionchanged">
  2467. <p><span class="versionmodified changed">Changed in version 3.7: </span>If the result is not ready, <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> is raised instead of
  2468. <a class="reference internal" href="exceptions.html#AssertionError" title="AssertionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AssertionError</span></code></a>.</p>
  2469. </div>
  2470. </dd></dl>
  2471. </dd></dl>
  2472. <p>The following example demonstrates the use of a pool:</p>
  2473. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Pool</span>
  2474. <span class="kn">import</span> <span class="nn">time</span>
  2475. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
  2476. <span class="k">return</span> <span class="n">x</span><span class="o">*</span><span class="n">x</span>
  2477. <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>
  2478. <span class="k">with</span> <span class="n">Pool</span><span class="p">(</span><span class="n">processes</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span> <span class="k">as</span> <span class="n">pool</span><span class="p">:</span> <span class="c1"># start 4 worker processes</span>
  2479. <span class="n">result</span> <span class="o">=</span> <span class="n">pool</span><span class="o">.</span><span class="n">apply_async</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="p">(</span><span class="mi">10</span><span class="p">,))</span> <span class="c1"># evaluate &quot;f(10)&quot; asynchronously in a single process</span>
  2480. <span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">timeout</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span> <span class="c1"># prints &quot;100&quot; unless your computer is *very* slow</span>
  2481. <span class="nb">print</span><span class="p">(</span><span class="n">pool</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)))</span> <span class="c1"># prints &quot;[0, 1, 4,..., 81]&quot;</span>
  2482. <span class="n">it</span> <span class="o">=</span> <span class="n">pool</span><span class="o">.</span><span class="n">imap</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>
  2483. <span class="nb">print</span><span class="p">(</span><span class="nb">next</span><span class="p">(</span><span class="n">it</span><span class="p">))</span> <span class="c1"># prints &quot;0&quot;</span>
  2484. <span class="nb">print</span><span class="p">(</span><span class="nb">next</span><span class="p">(</span><span class="n">it</span><span class="p">))</span> <span class="c1"># prints &quot;1&quot;</span>
  2485. <span class="nb">print</span><span class="p">(</span><span class="n">it</span><span class="o">.</span><span class="n">next</span><span class="p">(</span><span class="n">timeout</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span> <span class="c1"># prints &quot;4&quot; unless your computer is *very* slow</span>
  2486. <span class="n">result</span> <span class="o">=</span> <span class="n">pool</span><span class="o">.</span><span class="n">apply_async</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">,</span> <span class="p">(</span><span class="mi">10</span><span class="p">,))</span>
  2487. <span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">timeout</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span> <span class="c1"># raises multiprocessing.TimeoutError</span>
  2488. </pre></div>
  2489. </div>
  2490. </section>
  2491. <section id="module-multiprocessing.connection">
  2492. <span id="listeners-and-clients"></span><span id="multiprocessing-listeners-clients"></span><h3>Listeners and Clients<a class="headerlink" href="#module-multiprocessing.connection" title="Link to this heading">¶</a></h3>
  2493. <p>Usually message passing between processes is done using queues or by using
  2494. <a class="reference internal" href="#multiprocessing.connection.Connection" title="multiprocessing.connection.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> objects returned by
  2495. <a class="reference internal" href="#multiprocessing.Pipe" title="multiprocessing.Pipe"><code class="xref py py-func docutils literal notranslate"><span class="pre">Pipe()</span></code></a>.</p>
  2496. <p>However, the <a class="reference internal" href="#module-multiprocessing.connection" title="multiprocessing.connection: API for dealing with sockets."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing.connection</span></code></a> module allows some extra
  2497. flexibility. It basically gives a high level message oriented API for dealing
  2498. with sockets or Windows named pipes. It also has support for <em>digest
  2499. authentication</em> using the <a class="reference internal" href="hmac.html#module-hmac" title="hmac: Keyed-Hashing for Message Authentication (HMAC) implementation"><code class="xref py py-mod docutils literal notranslate"><span class="pre">hmac</span></code></a> module, and for polling
  2500. multiple connections at the same time.</p>
  2501. <dl class="py function">
  2502. <dt class="sig sig-object py" id="multiprocessing.connection.deliver_challenge">
  2503. <span class="sig-prename descclassname"><span class="pre">multiprocessing.connection.</span></span><span class="sig-name descname"><span class="pre">deliver_challenge</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">connection</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">authkey</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.connection.deliver_challenge" title="Link to this definition">¶</a></dt>
  2504. <dd><p>Send a randomly generated message to the other end of the connection and wait
  2505. for a reply.</p>
  2506. <p>If the reply matches the digest of the message using <em>authkey</em> as the key
  2507. then a welcome message is sent to the other end of the connection. Otherwise
  2508. <a class="reference internal" href="#multiprocessing.AuthenticationError" title="multiprocessing.AuthenticationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AuthenticationError</span></code></a> is raised.</p>
  2509. </dd></dl>
  2510. <dl class="py function">
  2511. <dt class="sig sig-object py" id="multiprocessing.connection.answer_challenge">
  2512. <span class="sig-prename descclassname"><span class="pre">multiprocessing.connection.</span></span><span class="sig-name descname"><span class="pre">answer_challenge</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">connection</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">authkey</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.connection.answer_challenge" title="Link to this definition">¶</a></dt>
  2513. <dd><p>Receive a message, calculate the digest of the message using <em>authkey</em> as the
  2514. key, and then send the digest back.</p>
  2515. <p>If a welcome message is not received, then
  2516. <a class="reference internal" href="#multiprocessing.AuthenticationError" title="multiprocessing.AuthenticationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AuthenticationError</span></code></a> is raised.</p>
  2517. </dd></dl>
  2518. <dl class="py function">
  2519. <dt class="sig sig-object py" id="multiprocessing.connection.Client">
  2520. <span class="sig-prename descclassname"><span class="pre">multiprocessing.connection.</span></span><span class="sig-name descname"><span class="pre">Client</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">address</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">family</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">authkey</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.connection.Client" title="Link to this definition">¶</a></dt>
  2521. <dd><p>Attempt to set up a connection to the listener which is using address
  2522. <em>address</em>, returning a <a class="reference internal" href="#multiprocessing.connection.Connection" title="multiprocessing.connection.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a>.</p>
  2523. <p>The type of the connection is determined by <em>family</em> argument, but this can
  2524. generally be omitted since it can usually be inferred from the format of
  2525. <em>address</em>. (See <a class="reference internal" href="#multiprocessing-address-formats"><span class="std std-ref">Address Formats</span></a>)</p>
  2526. <p>If <em>authkey</em> is given and not None, it should be a byte string and will be
  2527. used as the secret key for an HMAC-based authentication challenge. No
  2528. authentication is done if <em>authkey</em> is None.
  2529. <a class="reference internal" href="#multiprocessing.AuthenticationError" title="multiprocessing.AuthenticationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AuthenticationError</span></code></a> is raised if authentication fails.
  2530. See <a class="reference internal" href="#multiprocessing-auth-keys"><span class="std std-ref">Authentication keys</span></a>.</p>
  2531. </dd></dl>
  2532. <dl class="py class">
  2533. <dt class="sig sig-object py" id="multiprocessing.connection.Listener">
  2534. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.connection.</span></span><span class="sig-name descname"><span class="pre">Listener</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">address</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">family</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">backlog</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">authkey</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.connection.Listener" title="Link to this definition">¶</a></dt>
  2535. <dd><p>A wrapper for a bound socket or Windows named pipe which is ‘listening’ for
  2536. connections.</p>
  2537. <p><em>address</em> is the address to be used by the bound socket or named pipe of the
  2538. listener object.</p>
  2539. <div class="admonition note">
  2540. <p class="admonition-title">Note</p>
  2541. <p>If an address of ‘0.0.0.0’ is used, the address will not be a connectable
  2542. end point on Windows. If you require a connectable end-point,
  2543. you should use ‘127.0.0.1’.</p>
  2544. </div>
  2545. <p><em>family</em> is the type of socket (or named pipe) to use. This can be one of
  2546. the strings <code class="docutils literal notranslate"><span class="pre">'AF_INET'</span></code> (for a TCP socket), <code class="docutils literal notranslate"><span class="pre">'AF_UNIX'</span></code> (for a Unix
  2547. domain socket) or <code class="docutils literal notranslate"><span class="pre">'AF_PIPE'</span></code> (for a Windows named pipe). Of these only
  2548. the first is guaranteed to be available. If <em>family</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> then the
  2549. family is inferred from the format of <em>address</em>. If <em>address</em> is also
  2550. <code class="docutils literal notranslate"><span class="pre">None</span></code> then a default is chosen. This default is the family which is
  2551. assumed to be the fastest available. See
  2552. <a class="reference internal" href="#multiprocessing-address-formats"><span class="std std-ref">Address Formats</span></a>. Note that if <em>family</em> is
  2553. <code class="docutils literal notranslate"><span class="pre">'AF_UNIX'</span></code> and address is <code class="docutils literal notranslate"><span class="pre">None</span></code> then the socket will be created in a
  2554. private temporary directory created using <a class="reference internal" href="tempfile.html#tempfile.mkstemp" title="tempfile.mkstemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">tempfile.mkstemp()</span></code></a>.</p>
  2555. <p>If the listener object uses a socket then <em>backlog</em> (1 by default) is passed
  2556. to the <a class="reference internal" href="socket.html#socket.socket.listen" title="socket.socket.listen"><code class="xref py py-meth docutils literal notranslate"><span class="pre">listen()</span></code></a> method of the socket once it has been
  2557. bound.</p>
  2558. <p>If <em>authkey</em> is given and not None, it should be a byte string and will be
  2559. used as the secret key for an HMAC-based authentication challenge. No
  2560. authentication is done if <em>authkey</em> is None.
  2561. <a class="reference internal" href="#multiprocessing.AuthenticationError" title="multiprocessing.AuthenticationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AuthenticationError</span></code></a> is raised if authentication fails.
  2562. See <a class="reference internal" href="#multiprocessing-auth-keys"><span class="std std-ref">Authentication keys</span></a>.</p>
  2563. <dl class="py method">
  2564. <dt class="sig sig-object py" id="multiprocessing.connection.Listener.accept">
  2565. <span class="sig-name descname"><span class="pre">accept</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.connection.Listener.accept" title="Link to this definition">¶</a></dt>
  2566. <dd><p>Accept a connection on the bound socket or named pipe of the listener
  2567. object and return a <a class="reference internal" href="#multiprocessing.connection.Connection" title="multiprocessing.connection.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> object.
  2568. If authentication is attempted and fails, then
  2569. <a class="reference internal" href="#multiprocessing.AuthenticationError" title="multiprocessing.AuthenticationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AuthenticationError</span></code></a> is raised.</p>
  2570. </dd></dl>
  2571. <dl class="py method">
  2572. <dt class="sig sig-object py" id="multiprocessing.connection.Listener.close">
  2573. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.connection.Listener.close" title="Link to this definition">¶</a></dt>
  2574. <dd><p>Close the bound socket or named pipe of the listener object. This is
  2575. called automatically when the listener is garbage collected. However it
  2576. is advisable to call it explicitly.</p>
  2577. </dd></dl>
  2578. <p>Listener objects have the following read-only properties:</p>
  2579. <dl class="py attribute">
  2580. <dt class="sig sig-object py" id="multiprocessing.connection.Listener.address">
  2581. <span class="sig-name descname"><span class="pre">address</span></span><a class="headerlink" href="#multiprocessing.connection.Listener.address" title="Link to this definition">¶</a></dt>
  2582. <dd><p>The address which is being used by the Listener object.</p>
  2583. </dd></dl>
  2584. <dl class="py attribute">
  2585. <dt class="sig sig-object py" id="multiprocessing.connection.Listener.last_accepted">
  2586. <span class="sig-name descname"><span class="pre">last_accepted</span></span><a class="headerlink" href="#multiprocessing.connection.Listener.last_accepted" title="Link to this definition">¶</a></dt>
  2587. <dd><p>The address from which the last accepted connection came. If this is
  2588. unavailable then it is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  2589. </dd></dl>
  2590. <div class="versionchanged">
  2591. <p><span class="versionmodified changed">Changed in version 3.3: </span>Listener objects now support the context management protocol – see
  2592. <a class="reference internal" href="stdtypes.html#typecontextmanager"><span class="std std-ref">Context Manager Types</span></a>. <a class="reference internal" href="stdtypes.html#contextmanager.__enter__" title="contextmanager.__enter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__enter__()</span></code></a> returns the
  2593. listener object, and <a class="reference internal" href="stdtypes.html#contextmanager.__exit__" title="contextmanager.__exit__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__exit__()</span></code></a> calls <a class="reference internal" href="#multiprocessing.connection.Listener.close" title="multiprocessing.connection.Listener.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a>.</p>
  2594. </div>
  2595. </dd></dl>
  2596. <dl class="py function">
  2597. <dt class="sig sig-object py" id="multiprocessing.connection.wait">
  2598. <span class="sig-prename descclassname"><span class="pre">multiprocessing.connection.</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">object_list</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="#multiprocessing.connection.wait" title="Link to this definition">¶</a></dt>
  2599. <dd><p>Wait till an object in <em>object_list</em> is ready. Returns the list of
  2600. those objects in <em>object_list</em> which are ready. If <em>timeout</em> is a
  2601. float then the call blocks for at most that many seconds. If
  2602. <em>timeout</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> then it will block for an unlimited period.
  2603. A negative timeout is equivalent to a zero timeout.</p>
  2604. <p>For both POSIX and Windows, an object can appear in <em>object_list</em> if
  2605. it is</p>
  2606. <ul class="simple">
  2607. <li><p>a readable <a class="reference internal" href="#multiprocessing.connection.Connection" title="multiprocessing.connection.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> object;</p></li>
  2608. <li><p>a connected and readable <a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal notranslate"><span class="pre">socket.socket</span></code></a> object; or</p></li>
  2609. <li><p>the <a class="reference internal" href="#multiprocessing.Process.sentinel" title="multiprocessing.Process.sentinel"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sentinel</span></code></a> attribute of a
  2610. <a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> object.</p></li>
  2611. </ul>
  2612. <p>A connection or socket object is ready when there is data available
  2613. to be read from it, or the other end has been closed.</p>
  2614. <p><strong>POSIX</strong>: <code class="docutils literal notranslate"><span class="pre">wait(object_list,</span> <span class="pre">timeout)</span></code> almost equivalent
  2615. <code class="docutils literal notranslate"><span class="pre">select.select(object_list,</span> <span class="pre">[],</span> <span class="pre">[],</span> <span class="pre">timeout)</span></code>. The difference is
  2616. that, if <a class="reference internal" href="select.html#select.select" title="select.select"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.select()</span></code></a> is interrupted by a signal, it can
  2617. raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> with an error number of <code class="docutils literal notranslate"><span class="pre">EINTR</span></code>, whereas
  2618. <a class="reference internal" href="#multiprocessing.connection.wait" title="multiprocessing.connection.wait"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait()</span></code></a> will not.</p>
  2619. <p><strong>Windows</strong>: An item in <em>object_list</em> must either be an integer
  2620. handle which is waitable (according to the definition used by the
  2621. documentation of the Win32 function <code class="docutils literal notranslate"><span class="pre">WaitForMultipleObjects()</span></code>)
  2622. or it can be an object with a <a class="reference internal" href="io.html#io.IOBase.fileno" title="io.IOBase.fileno"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fileno()</span></code></a> method which returns a
  2623. socket handle or pipe handle. (Note that pipe handles and socket
  2624. handles are <strong>not</strong> waitable handles.)</p>
  2625. <div class="versionadded">
  2626. <p><span class="versionmodified added">New in version 3.3.</span></p>
  2627. </div>
  2628. </dd></dl>
  2629. <p><strong>Examples</strong></p>
  2630. <p>The following server code creates a listener which uses <code class="docutils literal notranslate"><span class="pre">'secret</span> <span class="pre">password'</span></code> as
  2631. an authentication key. It then waits for a connection and sends some data to
  2632. the client:</p>
  2633. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing.connection</span> <span class="kn">import</span> <span class="n">Listener</span>
  2634. <span class="kn">from</span> <span class="nn">array</span> <span class="kn">import</span> <span class="n">array</span>
  2635. <span class="n">address</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;localhost&#39;</span><span class="p">,</span> <span class="mi">6000</span><span class="p">)</span> <span class="c1"># family is deduced to be &#39;AF_INET&#39;</span>
  2636. <span class="k">with</span> <span class="n">Listener</span><span class="p">(</span><span class="n">address</span><span class="p">,</span> <span class="n">authkey</span><span class="o">=</span><span class="sa">b</span><span class="s1">&#39;secret password&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">listener</span><span class="p">:</span>
  2637. <span class="k">with</span> <span class="n">listener</span><span class="o">.</span><span class="n">accept</span><span class="p">()</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
  2638. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;connection accepted from&#39;</span><span class="p">,</span> <span class="n">listener</span><span class="o">.</span><span class="n">last_accepted</span><span class="p">)</span>
  2639. <span class="n">conn</span><span class="o">.</span><span class="n">send</span><span class="p">([</span><span class="mf">2.25</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="s1">&#39;junk&#39;</span><span class="p">,</span> <span class="nb">float</span><span class="p">])</span>
  2640. <span class="n">conn</span><span class="o">.</span><span class="n">send_bytes</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;hello&#39;</span><span class="p">)</span>
  2641. <span class="n">conn</span><span class="o">.</span><span class="n">send_bytes</span><span class="p">(</span><span class="n">array</span><span class="p">(</span><span class="s1">&#39;i&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">42</span><span class="p">,</span> <span class="mi">1729</span><span class="p">]))</span>
  2642. </pre></div>
  2643. </div>
  2644. <p>The following code connects to the server and receives some data from the
  2645. server:</p>
  2646. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing.connection</span> <span class="kn">import</span> <span class="n">Client</span>
  2647. <span class="kn">from</span> <span class="nn">array</span> <span class="kn">import</span> <span class="n">array</span>
  2648. <span class="n">address</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;localhost&#39;</span><span class="p">,</span> <span class="mi">6000</span><span class="p">)</span>
  2649. <span class="k">with</span> <span class="n">Client</span><span class="p">(</span><span class="n">address</span><span class="p">,</span> <span class="n">authkey</span><span class="o">=</span><span class="sa">b</span><span class="s1">&#39;secret password&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
  2650. <span class="nb">print</span><span class="p">(</span><span class="n">conn</span><span class="o">.</span><span class="n">recv</span><span class="p">())</span> <span class="c1"># =&gt; [2.25, None, &#39;junk&#39;, float]</span>
  2651. <span class="nb">print</span><span class="p">(</span><span class="n">conn</span><span class="o">.</span><span class="n">recv_bytes</span><span class="p">())</span> <span class="c1"># =&gt; &#39;hello&#39;</span>
  2652. <span class="n">arr</span> <span class="o">=</span> <span class="n">array</span><span class="p">(</span><span class="s1">&#39;i&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">])</span>
  2653. <span class="nb">print</span><span class="p">(</span><span class="n">conn</span><span class="o">.</span><span class="n">recv_bytes_into</span><span class="p">(</span><span class="n">arr</span><span class="p">))</span> <span class="c1"># =&gt; 8</span>
  2654. <span class="nb">print</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span> <span class="c1"># =&gt; array(&#39;i&#39;, [42, 1729, 0, 0, 0])</span>
  2655. </pre></div>
  2656. </div>
  2657. <p>The following code uses <a class="reference internal" href="#multiprocessing.connection.wait" title="multiprocessing.connection.wait"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait()</span></code></a> to
  2658. wait for messages from multiple processes at once:</p>
  2659. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">time</span><span class="o">,</span> <span class="nn">random</span>
  2660. <span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">Pipe</span><span class="p">,</span> <span class="n">current_process</span>
  2661. <span class="kn">from</span> <span class="nn">multiprocessing.connection</span> <span class="kn">import</span> <span class="n">wait</span>
  2662. <span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">w</span><span class="p">):</span>
  2663. <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">10</span><span class="p">):</span>
  2664. <span class="n">w</span><span class="o">.</span><span class="n">send</span><span class="p">((</span><span class="n">i</span><span class="p">,</span> <span class="n">current_process</span><span class="p">()</span><span class="o">.</span><span class="n">name</span><span class="p">))</span>
  2665. <span class="n">w</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2666. <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>
  2667. <span class="n">readers</span> <span class="o">=</span> <span class="p">[]</span>
  2668. <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">4</span><span class="p">):</span>
  2669. <span class="n">r</span><span class="p">,</span> <span class="n">w</span> <span class="o">=</span> <span class="n">Pipe</span><span class="p">(</span><span class="n">duplex</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
  2670. <span class="n">readers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
  2671. <span class="n">p</span> <span class="o">=</span> <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">foo</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">w</span><span class="p">,))</span>
  2672. <span class="n">p</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  2673. <span class="c1"># We close the writable end of the pipe now to be sure that</span>
  2674. <span class="c1"># p is the only process which owns a handle for it. This</span>
  2675. <span class="c1"># ensures that when p closes its handle for the writable end,</span>
  2676. <span class="c1"># wait() will promptly report the readable end as being ready.</span>
  2677. <span class="n">w</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2678. <span class="k">while</span> <span class="n">readers</span><span class="p">:</span>
  2679. <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">wait</span><span class="p">(</span><span class="n">readers</span><span class="p">):</span>
  2680. <span class="k">try</span><span class="p">:</span>
  2681. <span class="n">msg</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">recv</span><span class="p">()</span>
  2682. <span class="k">except</span> <span class="ne">EOFError</span><span class="p">:</span>
  2683. <span class="n">readers</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
  2684. <span class="k">else</span><span class="p">:</span>
  2685. <span class="nb">print</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
  2686. </pre></div>
  2687. </div>
  2688. <section id="address-formats">
  2689. <span id="multiprocessing-address-formats"></span><h4>Address Formats<a class="headerlink" href="#address-formats" title="Link to this heading">¶</a></h4>
  2690. <ul class="simple">
  2691. <li><p>An <code class="docutils literal notranslate"><span class="pre">'AF_INET'</span></code> address is a tuple of the form <code class="docutils literal notranslate"><span class="pre">(hostname,</span> <span class="pre">port)</span></code> where
  2692. <em>hostname</em> is a string and <em>port</em> is an integer.</p></li>
  2693. <li><p>An <code class="docutils literal notranslate"><span class="pre">'AF_UNIX'</span></code> address is a string representing a filename on the
  2694. filesystem.</p></li>
  2695. <li><p>An <code class="docutils literal notranslate"><span class="pre">'AF_PIPE'</span></code> address is a string of the form
  2696. <code class="samp docutils literal notranslate"><span class="pre">r'\\.\pipe\</span><em><span class="pre">PipeName</span></em><span class="pre">'</span></code>. To use <a class="reference internal" href="#multiprocessing.connection.Client" title="multiprocessing.connection.Client"><code class="xref py py-func docutils literal notranslate"><span class="pre">Client()</span></code></a> to connect to a named
  2697. pipe on a remote computer called <em>ServerName</em> one should use an address of the
  2698. form <code class="samp docutils literal notranslate"><span class="pre">r'\\</span><em><span class="pre">ServerName</span></em><span class="pre">\pipe\</span><em><span class="pre">PipeName</span></em><span class="pre">'</span></code> instead.</p></li>
  2699. </ul>
  2700. <p>Note that any string beginning with two backslashes is assumed by default to be
  2701. an <code class="docutils literal notranslate"><span class="pre">'AF_PIPE'</span></code> address rather than an <code class="docutils literal notranslate"><span class="pre">'AF_UNIX'</span></code> address.</p>
  2702. </section>
  2703. </section>
  2704. <section id="authentication-keys">
  2705. <span id="multiprocessing-auth-keys"></span><h3>Authentication keys<a class="headerlink" href="#authentication-keys" title="Link to this heading">¶</a></h3>
  2706. <p>When one uses <a class="reference internal" href="#multiprocessing.connection.Connection.recv" title="multiprocessing.connection.Connection.recv"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Connection.recv</span></code></a>, the
  2707. data received is automatically
  2708. unpickled. Unfortunately unpickling data from an untrusted source is a security
  2709. risk. Therefore <a class="reference internal" href="#multiprocessing.connection.Listener" title="multiprocessing.connection.Listener"><code class="xref py py-class docutils literal notranslate"><span class="pre">Listener</span></code></a> and <a class="reference internal" href="#multiprocessing.connection.Client" title="multiprocessing.connection.Client"><code class="xref py py-func docutils literal notranslate"><span class="pre">Client()</span></code></a> use the <a class="reference internal" href="hmac.html#module-hmac" title="hmac: Keyed-Hashing for Message Authentication (HMAC) implementation"><code class="xref py py-mod docutils literal notranslate"><span class="pre">hmac</span></code></a> module
  2710. to provide digest authentication.</p>
  2711. <p>An authentication key is a byte string which can be thought of as a
  2712. password: once a connection is established both ends will demand proof
  2713. that the other knows the authentication key. (Demonstrating that both
  2714. ends are using the same key does <strong>not</strong> involve sending the key over
  2715. the connection.)</p>
  2716. <p>If authentication is requested but no authentication key is specified then the
  2717. return value of <code class="docutils literal notranslate"><span class="pre">current_process().authkey</span></code> is used (see
  2718. <a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a>). This value will be automatically inherited by
  2719. any <a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> object that the current process creates.
  2720. This means that (by default) all processes of a multi-process program will share
  2721. a single authentication key which can be used when setting up connections
  2722. between themselves.</p>
  2723. <p>Suitable authentication keys can also be generated by using <a class="reference internal" href="os.html#os.urandom" title="os.urandom"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.urandom()</span></code></a>.</p>
  2724. </section>
  2725. <section id="logging">
  2726. <h3>Logging<a class="headerlink" href="#logging" title="Link to this heading">¶</a></h3>
  2727. <p>Some support for logging is available. Note, however, that the <a class="reference internal" href="logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code></a>
  2728. package does not use process shared locks so it is possible (depending on the
  2729. handler type) for messages from different processes to get mixed up.</p>
  2730. <dl class="py function">
  2731. <dt class="sig sig-object py" id="multiprocessing.get_logger">
  2732. <span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">get_logger</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.get_logger" title="Link to this definition">¶</a></dt>
  2733. <dd><p>Returns the logger used by <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a>. If necessary, a new one
  2734. will be created.</p>
  2735. <p>When first created the logger has level <a class="reference internal" href="logging.html#logging.NOTSET" title="logging.NOTSET"><code class="xref py py-const docutils literal notranslate"><span class="pre">logging.NOTSET</span></code></a> and no
  2736. default handler. Messages sent to this logger will not by default propagate
  2737. to the root logger.</p>
  2738. <p>Note that on Windows child processes will only inherit the level of the
  2739. parent process’s logger – any other customization of the logger will not be
  2740. inherited.</p>
  2741. </dd></dl>
  2742. <dl class="py function">
  2743. <dt class="sig sig-object py" id="multiprocessing.log_to_stderr">
  2744. <span class="sig-prename descclassname"><span class="pre">multiprocessing.</span></span><span class="sig-name descname"><span class="pre">log_to_stderr</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">level</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="#multiprocessing.log_to_stderr" title="Link to this definition">¶</a></dt>
  2745. <dd><p>This function performs a call to <a class="reference internal" href="#multiprocessing.get_logger" title="multiprocessing.get_logger"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_logger()</span></code></a> but in addition to
  2746. returning the logger created by get_logger, it adds a handler which sends
  2747. output to <a class="reference internal" href="sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a> using format
  2748. <code class="docutils literal notranslate"><span class="pre">'[%(levelname)s/%(processName)s]</span> <span class="pre">%(message)s'</span></code>.
  2749. You can modify <code class="docutils literal notranslate"><span class="pre">levelname</span></code> of the logger by passing a <code class="docutils literal notranslate"><span class="pre">level</span></code> argument.</p>
  2750. </dd></dl>
  2751. <p>Below is an example session with logging turned on:</p>
  2752. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">multiprocessing</span><span class="o">,</span> <span class="nn">logging</span>
  2753. <span class="gp">&gt;&gt;&gt; </span><span class="n">logger</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">log_to_stderr</span><span class="p">()</span>
  2754. <span class="gp">&gt;&gt;&gt; </span><span class="n">logger</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">INFO</span><span class="p">)</span>
  2755. <span class="gp">&gt;&gt;&gt; </span><span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">&#39;doomed&#39;</span><span class="p">)</span>
  2756. <span class="go">[WARNING/MainProcess] doomed</span>
  2757. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">Manager</span><span class="p">()</span>
  2758. <span class="go">[INFO/SyncManager-...] child process calling self.run()</span>
  2759. <span class="go">[INFO/SyncManager-...] created temp directory /.../pymp-...</span>
  2760. <span class="go">[INFO/SyncManager-...] manager serving at &#39;/.../listener-...&#39;</span>
  2761. <span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">m</span>
  2762. <span class="go">[INFO/MainProcess] sending shutdown message to manager</span>
  2763. <span class="go">[INFO/SyncManager-...] manager exiting with exitcode 0</span>
  2764. </pre></div>
  2765. </div>
  2766. <p>For a full table of logging levels, see the <a class="reference internal" href="logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code></a> module.</p>
  2767. </section>
  2768. <section id="module-multiprocessing.dummy">
  2769. <span id="the-multiprocessing-dummy-module"></span><h3>The <a class="reference internal" href="#module-multiprocessing.dummy" title="multiprocessing.dummy: Dumb wrapper around threading."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing.dummy</span></code></a> module<a class="headerlink" href="#module-multiprocessing.dummy" title="Link to this heading">¶</a></h3>
  2770. <p><a class="reference internal" href="#module-multiprocessing.dummy" title="multiprocessing.dummy: Dumb wrapper around threading."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing.dummy</span></code></a> replicates the API of <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> but is
  2771. no more than a wrapper around the <a class="reference internal" href="threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module.</p>
  2772. <p>In particular, the <code class="docutils literal notranslate"><span class="pre">Pool</span></code> function provided by <a class="reference internal" href="#module-multiprocessing.dummy" title="multiprocessing.dummy: Dumb wrapper around threading."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing.dummy</span></code></a>
  2773. returns an instance of <a class="reference internal" href="#multiprocessing.pool.ThreadPool" title="multiprocessing.pool.ThreadPool"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPool</span></code></a>, which is a subclass of
  2774. <a class="reference internal" href="#multiprocessing.pool.Pool" title="multiprocessing.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a> that supports all the same method calls but uses a pool of
  2775. worker threads rather than worker processes.</p>
  2776. <dl class="py class">
  2777. <dt class="sig sig-object py" id="multiprocessing.pool.ThreadPool">
  2778. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">multiprocessing.pool.</span></span><span class="sig-name descname"><span class="pre">ThreadPool</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">processes</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">initializer</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">initargs</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#multiprocessing.pool.ThreadPool" title="Link to this definition">¶</a></dt>
  2779. <dd><p>A thread pool object which controls a pool of worker threads to which jobs
  2780. can be submitted. <a class="reference internal" href="#multiprocessing.pool.ThreadPool" title="multiprocessing.pool.ThreadPool"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPool</span></code></a> instances are fully interface
  2781. compatible with <a class="reference internal" href="#multiprocessing.pool.Pool" title="multiprocessing.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a> instances, and their resources must also be
  2782. properly managed, either by using the pool as a context manager or by
  2783. calling <a class="reference internal" href="#multiprocessing.pool.Pool.close" title="multiprocessing.pool.Pool.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> and
  2784. <a class="reference internal" href="#multiprocessing.pool.Pool.terminate" title="multiprocessing.pool.Pool.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">terminate()</span></code></a> manually.</p>
  2785. <p><em>processes</em> is the number of worker threads to use. If <em>processes</em> is
  2786. <code class="docutils literal notranslate"><span class="pre">None</span></code> then the number returned by <a class="reference internal" href="os.html#os.cpu_count" title="os.cpu_count"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.cpu_count()</span></code></a> is used.</p>
  2787. <p>If <em>initializer</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code> then each worker process will call
  2788. <code class="docutils literal notranslate"><span class="pre">initializer(*initargs)</span></code> when it starts.</p>
  2789. <p>Unlike <a class="reference internal" href="#multiprocessing.pool.Pool" title="multiprocessing.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a>, <em>maxtasksperchild</em> and <em>context</em> cannot be provided.</p>
  2790. <div class="admonition note">
  2791. <p class="admonition-title">Note</p>
  2792. <p>A <a class="reference internal" href="#multiprocessing.pool.ThreadPool" title="multiprocessing.pool.ThreadPool"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPool</span></code></a> shares the same interface as <a class="reference internal" href="#multiprocessing.pool.Pool" title="multiprocessing.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a>, which
  2793. is designed around a pool of processes and predates the introduction of
  2794. the <a class="reference internal" href="concurrent.futures.html#module-concurrent.futures" title="concurrent.futures: Execute computations concurrently using threads or processes."><code class="xref py py-class docutils literal notranslate"><span class="pre">concurrent.futures</span></code></a> module. As such, it inherits some
  2795. operations that don’t make sense for a pool backed by threads, and it
  2796. has its own type for representing the status of asynchronous jobs,
  2797. <a class="reference internal" href="#multiprocessing.pool.AsyncResult" title="multiprocessing.pool.AsyncResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncResult</span></code></a>, that is not understood by any other libraries.</p>
  2798. <p>Users should generally prefer to use
  2799. <a class="reference internal" href="concurrent.futures.html#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">concurrent.futures.ThreadPoolExecutor</span></code></a>, which has a simpler
  2800. interface that was designed around threads from the start, and which
  2801. returns <a class="reference internal" href="concurrent.futures.html#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">concurrent.futures.Future</span></code></a> instances that are
  2802. compatible with many other libraries, including <a class="reference internal" href="asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code></a>.</p>
  2803. </div>
  2804. </dd></dl>
  2805. </section>
  2806. </section>
  2807. <section id="programming-guidelines">
  2808. <span id="multiprocessing-programming"></span><h2>Programming guidelines<a class="headerlink" href="#programming-guidelines" title="Link to this heading">¶</a></h2>
  2809. <p>There are certain guidelines and idioms which should be adhered to when using
  2810. <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a>.</p>
  2811. <section id="all-start-methods">
  2812. <h3>All start methods<a class="headerlink" href="#all-start-methods" title="Link to this heading">¶</a></h3>
  2813. <p>The following applies to all start methods.</p>
  2814. <p>Avoid shared state</p>
  2815. <blockquote>
  2816. <div><p>As far as possible one should try to avoid shifting large amounts of data
  2817. between processes.</p>
  2818. <p>It is probably best to stick to using queues or pipes for communication
  2819. between processes rather than using the lower level synchronization
  2820. primitives.</p>
  2821. </div></blockquote>
  2822. <p>Picklability</p>
  2823. <blockquote>
  2824. <div><p>Ensure that the arguments to the methods of proxies are picklable.</p>
  2825. </div></blockquote>
  2826. <p>Thread safety of proxies</p>
  2827. <blockquote>
  2828. <div><p>Do not use a proxy object from more than one thread unless you protect it
  2829. with a lock.</p>
  2830. <p>(There is never a problem with different processes using the <em>same</em> proxy.)</p>
  2831. </div></blockquote>
  2832. <p>Joining zombie processes</p>
  2833. <blockquote>
  2834. <div><p>On POSIX when a process finishes but has not been joined it becomes a zombie.
  2835. There should never be very many because each time a new process starts (or
  2836. <a class="reference internal" href="#multiprocessing.active_children" title="multiprocessing.active_children"><code class="xref py py-func docutils literal notranslate"><span class="pre">active_children()</span></code></a> is called) all completed processes
  2837. which have not yet been joined will be joined. Also calling a finished
  2838. process’s <a class="reference internal" href="#multiprocessing.Process.is_alive" title="multiprocessing.Process.is_alive"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Process.is_alive</span></code></a> will
  2839. join the process. Even so it is probably good
  2840. practice to explicitly join all the processes that you start.</p>
  2841. </div></blockquote>
  2842. <p>Better to inherit than pickle/unpickle</p>
  2843. <blockquote>
  2844. <div><p>When using the <em>spawn</em> or <em>forkserver</em> start methods many types
  2845. from <a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> need to be picklable so that child
  2846. processes can use them. However, one should generally avoid
  2847. sending shared objects to other processes using pipes or queues.
  2848. Instead you should arrange the program so that a process which
  2849. needs access to a shared resource created elsewhere can inherit it
  2850. from an ancestor process.</p>
  2851. </div></blockquote>
  2852. <p>Avoid terminating processes</p>
  2853. <blockquote>
  2854. <div><p>Using the <a class="reference internal" href="#multiprocessing.Process.terminate" title="multiprocessing.Process.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Process.terminate</span></code></a>
  2855. method to stop a process is liable to
  2856. cause any shared resources (such as locks, semaphores, pipes and queues)
  2857. currently being used by the process to become broken or unavailable to other
  2858. processes.</p>
  2859. <p>Therefore it is probably best to only consider using
  2860. <a class="reference internal" href="#multiprocessing.Process.terminate" title="multiprocessing.Process.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Process.terminate</span></code></a> on processes
  2861. which never use any shared resources.</p>
  2862. </div></blockquote>
  2863. <p>Joining processes that use queues</p>
  2864. <blockquote>
  2865. <div><p>Bear in mind that a process that has put items in a queue will wait before
  2866. terminating until all the buffered items are fed by the “feeder” thread to
  2867. the underlying pipe. (The child process can call the
  2868. <a class="reference internal" href="#multiprocessing.Queue.cancel_join_thread" title="multiprocessing.Queue.cancel_join_thread"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Queue.cancel_join_thread</span></code></a>
  2869. method of the queue to avoid this behaviour.)</p>
  2870. <p>This means that whenever you use a queue you need to make sure that all
  2871. items which have been put on the queue will eventually be removed before the
  2872. process is joined. Otherwise you cannot be sure that processes which have
  2873. put items on the queue will terminate. Remember also that non-daemonic
  2874. processes will be joined automatically.</p>
  2875. <p>An example which will deadlock is the following:</p>
  2876. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">Queue</span>
  2877. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">q</span><span class="p">):</span>
  2878. <span class="n">q</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="s1">&#39;X&#39;</span> <span class="o">*</span> <span class="mi">1000000</span><span class="p">)</span>
  2879. <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>
  2880. <span class="n">queue</span> <span class="o">=</span> <span class="n">Queue</span><span class="p">()</span>
  2881. <span class="n">p</span> <span class="o">=</span> <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">f</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">queue</span><span class="p">,))</span>
  2882. <span class="n">p</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  2883. <span class="n">p</span><span class="o">.</span><span class="n">join</span><span class="p">()</span> <span class="c1"># this deadlocks</span>
  2884. <span class="n">obj</span> <span class="o">=</span> <span class="n">queue</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
  2885. </pre></div>
  2886. </div>
  2887. <p>A fix here would be to swap the last two lines (or simply remove the
  2888. <code class="docutils literal notranslate"><span class="pre">p.join()</span></code> line).</p>
  2889. </div></blockquote>
  2890. <p>Explicitly pass resources to child processes</p>
  2891. <blockquote>
  2892. <div><p>On POSIX using the <em>fork</em> start method, a child process can make
  2893. use of a shared resource created in a parent process using a
  2894. global resource. However, it is better to pass the object as an
  2895. argument to the constructor for the child process.</p>
  2896. <p>Apart from making the code (potentially) compatible with Windows
  2897. and the other start methods this also ensures that as long as the
  2898. child process is still alive the object will not be garbage
  2899. collected in the parent process. This might be important if some
  2900. resource is freed when the object is garbage collected in the
  2901. parent process.</p>
  2902. <p>So for instance</p>
  2903. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">Lock</span>
  2904. <span class="k">def</span> <span class="nf">f</span><span class="p">():</span>
  2905. <span class="o">...</span> <span class="n">do</span> <span class="n">something</span> <span class="n">using</span> <span class="s2">&quot;lock&quot;</span> <span class="o">...</span>
  2906. <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>
  2907. <span class="n">lock</span> <span class="o">=</span> <span class="n">Lock</span><span class="p">()</span>
  2908. <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">10</span><span class="p">):</span>
  2909. <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">f</span><span class="p">)</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  2910. </pre></div>
  2911. </div>
  2912. <p>should be rewritten as</p>
  2913. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">Lock</span>
  2914. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">l</span><span class="p">):</span>
  2915. <span class="o">...</span> <span class="n">do</span> <span class="n">something</span> <span class="n">using</span> <span class="s2">&quot;l&quot;</span> <span class="o">...</span>
  2916. <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>
  2917. <span class="n">lock</span> <span class="o">=</span> <span class="n">Lock</span><span class="p">()</span>
  2918. <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">10</span><span class="p">):</span>
  2919. <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">f</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">lock</span><span class="p">,))</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  2920. </pre></div>
  2921. </div>
  2922. </div></blockquote>
  2923. <p>Beware of replacing <a class="reference internal" href="sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a> with a “file like object”</p>
  2924. <blockquote>
  2925. <div><p><a class="reference internal" href="#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> originally unconditionally called:</p>
  2926. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">os</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">fileno</span><span class="p">())</span>
  2927. </pre></div>
  2928. </div>
  2929. <p>in the <code class="xref py py-meth docutils literal notranslate"><span class="pre">multiprocessing.Process._bootstrap()</span></code> method — this resulted
  2930. in issues with processes-in-processes. This has been changed to:</p>
  2931. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2932. <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">devnull</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">O_RDONLY</span><span class="p">),</span> <span class="n">closefd</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
  2933. </pre></div>
  2934. </div>
  2935. <p>Which solves the fundamental issue of processes colliding with each other
  2936. resulting in a bad file descriptor error, but introduces a potential danger
  2937. to applications which replace <a class="reference internal" href="sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.stdin()</span></code></a> with a “file-like object”
  2938. with output buffering. This danger is that if multiple processes call
  2939. <a class="reference internal" href="io.html#io.IOBase.close" title="io.IOBase.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> on this file-like object, it could result in the same
  2940. data being flushed to the object multiple times, resulting in corruption.</p>
  2941. <p>If you write a file-like object and implement your own caching, you can
  2942. make it fork-safe by storing the pid whenever you append to the cache,
  2943. and discarding the cache when the pid changes. For example:</p>
  2944. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@property</span>
  2945. <span class="k">def</span> <span class="nf">cache</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  2946. <span class="n">pid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">()</span>
  2947. <span class="k">if</span> <span class="n">pid</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">:</span>
  2948. <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span> <span class="o">=</span> <span class="n">pid</span>
  2949. <span class="bp">self</span><span class="o">.</span><span class="n">_cache</span> <span class="o">=</span> <span class="p">[]</span>
  2950. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cache</span>
  2951. </pre></div>
  2952. </div>
  2953. <p>For more information, see <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5155">bpo-5155</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5313">bpo-5313</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5331">bpo-5331</a></p>
  2954. </div></blockquote>
  2955. </section>
  2956. <section id="the-spawn-and-forkserver-start-methods">
  2957. <h3>The <em>spawn</em> and <em>forkserver</em> start methods<a class="headerlink" href="#the-spawn-and-forkserver-start-methods" title="Link to this heading">¶</a></h3>
  2958. <p>There are a few extra restrictions which don’t apply to the <em>fork</em>
  2959. start method.</p>
  2960. <p>More picklability</p>
  2961. <blockquote>
  2962. <div><p>Ensure that all arguments to <code class="xref py py-meth docutils literal notranslate"><span class="pre">Process.__init__()</span></code> are picklable.
  2963. Also, if you subclass <a class="reference internal" href="#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> then make sure that
  2964. instances will be picklable when the <a class="reference internal" href="#multiprocessing.Process.start" title="multiprocessing.Process.start"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Process.start</span></code></a> method is called.</p>
  2965. </div></blockquote>
  2966. <p>Global variables</p>
  2967. <blockquote>
  2968. <div><p>Bear in mind that if code run in a child process tries to access a global
  2969. variable, then the value it sees (if any) may not be the same as the value
  2970. in the parent process at the time that <a class="reference internal" href="#multiprocessing.Process.start" title="multiprocessing.Process.start"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Process.start</span></code></a> was called.</p>
  2971. <p>However, global variables which are just module level constants cause no
  2972. problems.</p>
  2973. </div></blockquote>
  2974. <p id="multiprocessing-safe-main-import">Safe importing of main module</p>
  2975. <blockquote>
  2976. <div><p>Make sure that the main module can be safely imported by a new Python
  2977. interpreter without causing unintended side effects (such as starting a new
  2978. process).</p>
  2979. <p>For example, using the <em>spawn</em> or <em>forkserver</em> start method
  2980. running the following module would fail with a
  2981. <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>
  2982. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span>
  2983. <span class="k">def</span> <span class="nf">foo</span><span class="p">():</span>
  2984. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;hello&#39;</span><span class="p">)</span>
  2985. <span class="n">p</span> <span class="o">=</span> <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">foo</span><span class="p">)</span>
  2986. <span class="n">p</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  2987. </pre></div>
  2988. </div>
  2989. <p>Instead one should protect the “entry point” of the program by using <code class="docutils literal notranslate"><span class="pre">if</span>
  2990. <span class="pre">__name__</span> <span class="pre">==</span> <span class="pre">'__main__':</span></code> as follows:</p>
  2991. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">freeze_support</span><span class="p">,</span> <span class="n">set_start_method</span>
  2992. <span class="k">def</span> <span class="nf">foo</span><span class="p">():</span>
  2993. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;hello&#39;</span><span class="p">)</span>
  2994. <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>
  2995. <span class="n">freeze_support</span><span class="p">()</span>
  2996. <span class="n">set_start_method</span><span class="p">(</span><span class="s1">&#39;spawn&#39;</span><span class="p">)</span>
  2997. <span class="n">p</span> <span class="o">=</span> <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">foo</span><span class="p">)</span>
  2998. <span class="n">p</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  2999. </pre></div>
  3000. </div>
  3001. <p>(The <code class="docutils literal notranslate"><span class="pre">freeze_support()</span></code> line can be omitted if the program will be run
  3002. normally instead of frozen.)</p>
  3003. <p>This allows the newly spawned Python interpreter to safely import the module
  3004. and then run the module’s <code class="docutils literal notranslate"><span class="pre">foo()</span></code> function.</p>
  3005. <p>Similar restrictions apply if a pool or manager is created in the main
  3006. module.</p>
  3007. </div></blockquote>
  3008. </section>
  3009. </section>
  3010. <section id="examples">
  3011. <span id="multiprocessing-examples"></span><h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
  3012. <p>Demonstration of how to create and use customized managers and proxies:</p>
  3013. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">freeze_support</span>
  3014. <span class="kn">from</span> <span class="nn">multiprocessing.managers</span> <span class="kn">import</span> <span class="n">BaseManager</span><span class="p">,</span> <span class="n">BaseProxy</span>
  3015. <span class="kn">import</span> <span class="nn">operator</span>
  3016. <span class="c1">##</span>
  3017. <span class="k">class</span> <span class="nc">Foo</span><span class="p">:</span>
  3018. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  3019. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;you called Foo.f()&#39;</span><span class="p">)</span>
  3020. <span class="k">def</span> <span class="nf">g</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  3021. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;you called Foo.g()&#39;</span><span class="p">)</span>
  3022. <span class="k">def</span> <span class="nf">_h</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  3023. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;you called Foo._h()&#39;</span><span class="p">)</span>
  3024. <span class="c1"># A simple generator function</span>
  3025. <span class="k">def</span> <span class="nf">baz</span><span class="p">():</span>
  3026. <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">10</span><span class="p">):</span>
  3027. <span class="k">yield</span> <span class="n">i</span><span class="o">*</span><span class="n">i</span>
  3028. <span class="c1"># Proxy type for generator objects</span>
  3029. <span class="k">class</span> <span class="nc">GeneratorProxy</span><span class="p">(</span><span class="n">BaseProxy</span><span class="p">):</span>
  3030. <span class="n">_exposed_</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;__next__&#39;</span><span class="p">]</span>
  3031. <span class="k">def</span> <span class="fm">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  3032. <span class="k">return</span> <span class="bp">self</span>
  3033. <span class="k">def</span> <span class="fm">__next__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  3034. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_callmethod</span><span class="p">(</span><span class="s1">&#39;__next__&#39;</span><span class="p">)</span>
  3035. <span class="c1"># Function to return the operator module</span>
  3036. <span class="k">def</span> <span class="nf">get_operator_module</span><span class="p">():</span>
  3037. <span class="k">return</span> <span class="n">operator</span>
  3038. <span class="c1">##</span>
  3039. <span class="k">class</span> <span class="nc">MyManager</span><span class="p">(</span><span class="n">BaseManager</span><span class="p">):</span>
  3040. <span class="k">pass</span>
  3041. <span class="c1"># register the Foo class; make `f()` and `g()` accessible via proxy</span>
  3042. <span class="n">MyManager</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;Foo1&#39;</span><span class="p">,</span> <span class="n">Foo</span><span class="p">)</span>
  3043. <span class="c1"># register the Foo class; make `g()` and `_h()` accessible via proxy</span>
  3044. <span class="n">MyManager</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;Foo2&#39;</span><span class="p">,</span> <span class="n">Foo</span><span class="p">,</span> <span class="n">exposed</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;g&#39;</span><span class="p">,</span> <span class="s1">&#39;_h&#39;</span><span class="p">))</span>
  3045. <span class="c1"># register the generator function baz; use `GeneratorProxy` to make proxies</span>
  3046. <span class="n">MyManager</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;baz&#39;</span><span class="p">,</span> <span class="n">baz</span><span class="p">,</span> <span class="n">proxytype</span><span class="o">=</span><span class="n">GeneratorProxy</span><span class="p">)</span>
  3047. <span class="c1"># register get_operator_module(); make public functions accessible via proxy</span>
  3048. <span class="n">MyManager</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;operator&#39;</span><span class="p">,</span> <span class="n">get_operator_module</span><span class="p">)</span>
  3049. <span class="c1">##</span>
  3050. <span class="k">def</span> <span class="nf">test</span><span class="p">():</span>
  3051. <span class="n">manager</span> <span class="o">=</span> <span class="n">MyManager</span><span class="p">()</span>
  3052. <span class="n">manager</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  3053. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;-&#39;</span> <span class="o">*</span> <span class="mi">20</span><span class="p">)</span>
  3054. <span class="n">f1</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">Foo1</span><span class="p">()</span>
  3055. <span class="n">f1</span><span class="o">.</span><span class="n">f</span><span class="p">()</span>
  3056. <span class="n">f1</span><span class="o">.</span><span class="n">g</span><span class="p">()</span>
  3057. <span class="k">assert</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">f1</span><span class="p">,</span> <span class="s1">&#39;_h&#39;</span><span class="p">)</span>
  3058. <span class="k">assert</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">f1</span><span class="o">.</span><span class="n">_exposed_</span><span class="p">)</span> <span class="o">==</span> <span class="nb">sorted</span><span class="p">([</span><span class="s1">&#39;f&#39;</span><span class="p">,</span> <span class="s1">&#39;g&#39;</span><span class="p">])</span>
  3059. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;-&#39;</span> <span class="o">*</span> <span class="mi">20</span><span class="p">)</span>
  3060. <span class="n">f2</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">Foo2</span><span class="p">()</span>
  3061. <span class="n">f2</span><span class="o">.</span><span class="n">g</span><span class="p">()</span>
  3062. <span class="n">f2</span><span class="o">.</span><span class="n">_h</span><span class="p">()</span>
  3063. <span class="k">assert</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">f2</span><span class="p">,</span> <span class="s1">&#39;f&#39;</span><span class="p">)</span>
  3064. <span class="k">assert</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">f2</span><span class="o">.</span><span class="n">_exposed_</span><span class="p">)</span> <span class="o">==</span> <span class="nb">sorted</span><span class="p">([</span><span class="s1">&#39;g&#39;</span><span class="p">,</span> <span class="s1">&#39;_h&#39;</span><span class="p">])</span>
  3065. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;-&#39;</span> <span class="o">*</span> <span class="mi">20</span><span class="p">)</span>
  3066. <span class="n">it</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">baz</span><span class="p">()</span>
  3067. <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">it</span><span class="p">:</span>
  3068. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;&lt;</span><span class="si">%d</span><span class="s1">&gt;&#39;</span> <span class="o">%</span> <span class="n">i</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="p">)</span>
  3069. <span class="nb">print</span><span class="p">()</span>
  3070. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;-&#39;</span> <span class="o">*</span> <span class="mi">20</span><span class="p">)</span>
  3071. <span class="n">op</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">operator</span><span class="p">()</span>
  3072. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;op.add(23, 45) =&#39;</span><span class="p">,</span> <span class="n">op</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">23</span><span class="p">,</span> <span class="mi">45</span><span class="p">))</span>
  3073. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;op.pow(2, 94) =&#39;</span><span class="p">,</span> <span class="n">op</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">94</span><span class="p">))</span>
  3074. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;op._exposed_ =&#39;</span><span class="p">,</span> <span class="n">op</span><span class="o">.</span><span class="n">_exposed_</span><span class="p">)</span>
  3075. <span class="c1">##</span>
  3076. <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>
  3077. <span class="n">freeze_support</span><span class="p">()</span>
  3078. <span class="n">test</span><span class="p">()</span>
  3079. </pre></div>
  3080. </div>
  3081. <p>Using <a class="reference internal" href="#multiprocessing.pool.Pool" title="multiprocessing.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a>:</p>
  3082. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">multiprocessing</span>
  3083. <span class="kn">import</span> <span class="nn">time</span>
  3084. <span class="kn">import</span> <span class="nn">random</span>
  3085. <span class="kn">import</span> <span class="nn">sys</span>
  3086. <span class="c1">#</span>
  3087. <span class="c1"># Functions used by test code</span>
  3088. <span class="c1">#</span>
  3089. <span class="k">def</span> <span class="nf">calculate</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
  3090. <span class="n">result</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
  3091. <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> says that </span><span class="si">%s%s</span><span class="s1"> = </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
  3092. <span class="n">multiprocessing</span><span class="o">.</span><span class="n">current_process</span><span class="p">()</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
  3093. <span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">result</span>
  3094. <span class="p">)</span>
  3095. <span class="k">def</span> <span class="nf">calculatestar</span><span class="p">(</span><span class="n">args</span><span class="p">):</span>
  3096. <span class="k">return</span> <span class="n">calculate</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
  3097. <span class="k">def</span> <span class="nf">mul</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
  3098. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">())</span>
  3099. <span class="k">return</span> <span class="n">a</span> <span class="o">*</span> <span class="n">b</span>
  3100. <span class="k">def</span> <span class="nf">plus</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
  3101. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">())</span>
  3102. <span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
  3103. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
  3104. <span class="k">return</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mf">5.0</span><span class="p">)</span>
  3105. <span class="k">def</span> <span class="nf">pow3</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
  3106. <span class="k">return</span> <span class="n">x</span> <span class="o">**</span> <span class="mi">3</span>
  3107. <span class="k">def</span> <span class="nf">noop</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
  3108. <span class="k">pass</span>
  3109. <span class="c1">#</span>
  3110. <span class="c1"># Test code</span>
  3111. <span class="c1">#</span>
  3112. <span class="k">def</span> <span class="nf">test</span><span class="p">():</span>
  3113. <span class="n">PROCESSES</span> <span class="o">=</span> <span class="mi">4</span>
  3114. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Creating pool with </span><span class="si">%d</span><span class="s1"> processes</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">PROCESSES</span><span class="p">)</span>
  3115. <span class="k">with</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">Pool</span><span class="p">(</span><span class="n">PROCESSES</span><span class="p">)</span> <span class="k">as</span> <span class="n">pool</span><span class="p">:</span>
  3116. <span class="c1">#</span>
  3117. <span class="c1"># Tests</span>
  3118. <span class="c1">#</span>
  3119. <span class="n">TASKS</span> <span class="o">=</span> <span class="p">[(</span><span class="n">mul</span><span class="p">,</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="mi">7</span><span class="p">))</span> <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">10</span><span class="p">)]</span> <span class="o">+</span> \
  3120. <span class="p">[(</span><span class="n">plus</span><span class="p">,</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="mi">8</span><span class="p">))</span> <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">10</span><span class="p">)]</span>
  3121. <span class="n">results</span> <span class="o">=</span> <span class="p">[</span><span class="n">pool</span><span class="o">.</span><span class="n">apply_async</span><span class="p">(</span><span class="n">calculate</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">TASKS</span><span class="p">]</span>
  3122. <span class="n">imap_it</span> <span class="o">=</span> <span class="n">pool</span><span class="o">.</span><span class="n">imap</span><span class="p">(</span><span class="n">calculatestar</span><span class="p">,</span> <span class="n">TASKS</span><span class="p">)</span>
  3123. <span class="n">imap_unordered_it</span> <span class="o">=</span> <span class="n">pool</span><span class="o">.</span><span class="n">imap_unordered</span><span class="p">(</span><span class="n">calculatestar</span><span class="p">,</span> <span class="n">TASKS</span><span class="p">)</span>
  3124. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Ordered results using pool.apply_async():&#39;</span><span class="p">)</span>
  3125. <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">results</span><span class="p">:</span>
  3126. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\t</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">r</span><span class="o">.</span><span class="n">get</span><span class="p">())</span>
  3127. <span class="nb">print</span><span class="p">()</span>
  3128. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Ordered results using pool.imap():&#39;</span><span class="p">)</span>
  3129. <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">imap_it</span><span class="p">:</span>
  3130. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\t</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
  3131. <span class="nb">print</span><span class="p">()</span>
  3132. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Unordered results using pool.imap_unordered():&#39;</span><span class="p">)</span>
  3133. <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">imap_unordered_it</span><span class="p">:</span>
  3134. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\t</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
  3135. <span class="nb">print</span><span class="p">()</span>
  3136. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Ordered results using pool.map() --- will block till complete:&#39;</span><span class="p">)</span>
  3137. <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">pool</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">calculatestar</span><span class="p">,</span> <span class="n">TASKS</span><span class="p">):</span>
  3138. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\t</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
  3139. <span class="nb">print</span><span class="p">()</span>
  3140. <span class="c1">#</span>
  3141. <span class="c1"># Test error handling</span>
  3142. <span class="c1">#</span>
  3143. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Testing error handling:&#39;</span><span class="p">)</span>
  3144. <span class="k">try</span><span class="p">:</span>
  3145. <span class="nb">print</span><span class="p">(</span><span class="n">pool</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="p">(</span><span class="mi">5</span><span class="p">,)))</span>
  3146. <span class="k">except</span> <span class="ne">ZeroDivisionError</span><span class="p">:</span>
  3147. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\t</span><span class="s1">Got ZeroDivisionError as expected from pool.apply()&#39;</span><span class="p">)</span>
  3148. <span class="k">else</span><span class="p">:</span>
  3149. <span class="k">raise</span> <span class="ne">AssertionError</span><span class="p">(</span><span class="s1">&#39;expected ZeroDivisionError&#39;</span><span class="p">)</span>
  3150. <span class="k">try</span><span class="p">:</span>
  3151. <span class="nb">print</span><span class="p">(</span><span class="n">pool</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">))))</span>
  3152. <span class="k">except</span> <span class="ne">ZeroDivisionError</span><span class="p">:</span>
  3153. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\t</span><span class="s1">Got ZeroDivisionError as expected from pool.map()&#39;</span><span class="p">)</span>
  3154. <span class="k">else</span><span class="p">:</span>
  3155. <span class="k">raise</span> <span class="ne">AssertionError</span><span class="p">(</span><span class="s1">&#39;expected ZeroDivisionError&#39;</span><span class="p">)</span>
  3156. <span class="k">try</span><span class="p">:</span>
  3157. <span class="nb">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">pool</span><span class="o">.</span><span class="n">imap</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)))))</span>
  3158. <span class="k">except</span> <span class="ne">ZeroDivisionError</span><span class="p">:</span>
  3159. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\t</span><span class="s1">Got ZeroDivisionError as expected from list(pool.imap())&#39;</span><span class="p">)</span>
  3160. <span class="k">else</span><span class="p">:</span>
  3161. <span class="k">raise</span> <span class="ne">AssertionError</span><span class="p">(</span><span class="s1">&#39;expected ZeroDivisionError&#39;</span><span class="p">)</span>
  3162. <span class="n">it</span> <span class="o">=</span> <span class="n">pool</span><span class="o">.</span><span class="n">imap</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)))</span>
  3163. <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">10</span><span class="p">):</span>
  3164. <span class="k">try</span><span class="p">:</span>
  3165. <span class="n">x</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">it</span><span class="p">)</span>
  3166. <span class="k">except</span> <span class="ne">ZeroDivisionError</span><span class="p">:</span>
  3167. <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">5</span><span class="p">:</span>
  3168. <span class="k">pass</span>
  3169. <span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span>
  3170. <span class="k">break</span>
  3171. <span class="k">else</span><span class="p">:</span>
  3172. <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">5</span><span class="p">:</span>
  3173. <span class="k">raise</span> <span class="ne">AssertionError</span><span class="p">(</span><span class="s1">&#39;expected ZeroDivisionError&#39;</span><span class="p">)</span>
  3174. <span class="k">assert</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">9</span>
  3175. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\t</span><span class="s1">Got ZeroDivisionError as expected from IMapIterator.next()&#39;</span><span class="p">)</span>
  3176. <span class="nb">print</span><span class="p">()</span>
  3177. <span class="c1">#</span>
  3178. <span class="c1"># Testing timeouts</span>
  3179. <span class="c1">#</span>
  3180. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Testing ApplyResult.get() with timeout:&#39;</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="p">)</span>
  3181. <span class="n">res</span> <span class="o">=</span> <span class="n">pool</span><span class="o">.</span><span class="n">apply_async</span><span class="p">(</span><span class="n">calculate</span><span class="p">,</span> <span class="n">TASKS</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
  3182. <span class="k">while</span> <span class="mi">1</span><span class="p">:</span>
  3183. <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
  3184. <span class="k">try</span><span class="p">:</span>
  3185. <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n\t</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">res</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="mf">0.02</span><span class="p">))</span>
  3186. <span class="k">break</span>
  3187. <span class="k">except</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">TimeoutError</span><span class="p">:</span>
  3188. <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>
  3189. <span class="nb">print</span><span class="p">()</span>
  3190. <span class="nb">print</span><span class="p">()</span>
  3191. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Testing IMapIterator.next() with timeout:&#39;</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="p">)</span>
  3192. <span class="n">it</span> <span class="o">=</span> <span class="n">pool</span><span class="o">.</span><span class="n">imap</span><span class="p">(</span><span class="n">calculatestar</span><span class="p">,</span> <span class="n">TASKS</span><span class="p">)</span>
  3193. <span class="k">while</span> <span class="mi">1</span><span class="p">:</span>
  3194. <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
  3195. <span class="k">try</span><span class="p">:</span>
  3196. <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n\t</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">it</span><span class="o">.</span><span class="n">next</span><span class="p">(</span><span class="mf">0.02</span><span class="p">))</span>
  3197. <span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span>
  3198. <span class="k">break</span>
  3199. <span class="k">except</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">TimeoutError</span><span class="p">:</span>
  3200. <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>
  3201. <span class="nb">print</span><span class="p">()</span>
  3202. <span class="nb">print</span><span class="p">()</span>
  3203. <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>
  3204. <span class="n">multiprocessing</span><span class="o">.</span><span class="n">freeze_support</span><span class="p">()</span>
  3205. <span class="n">test</span><span class="p">()</span>
  3206. </pre></div>
  3207. </div>
  3208. <p>An example showing how to use queues to feed tasks to a collection of worker
  3209. processes and collect the results:</p>
  3210. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">time</span>
  3211. <span class="kn">import</span> <span class="nn">random</span>
  3212. <span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">Queue</span><span class="p">,</span> <span class="n">current_process</span><span class="p">,</span> <span class="n">freeze_support</span>
  3213. <span class="c1">#</span>
  3214. <span class="c1"># Function run by worker processes</span>
  3215. <span class="c1">#</span>
  3216. <span class="k">def</span> <span class="nf">worker</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="n">output</span><span class="p">):</span>
  3217. <span class="k">for</span> <span class="n">func</span><span class="p">,</span> <span class="n">args</span> <span class="ow">in</span> <span class="nb">iter</span><span class="p">(</span><span class="nb">input</span><span class="o">.</span><span class="n">get</span><span class="p">,</span> <span class="s1">&#39;STOP&#39;</span><span class="p">):</span>
  3218. <span class="n">result</span> <span class="o">=</span> <span class="n">calculate</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
  3219. <span class="n">output</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
  3220. <span class="c1">#</span>
  3221. <span class="c1"># Function used to calculate result</span>
  3222. <span class="c1">#</span>
  3223. <span class="k">def</span> <span class="nf">calculate</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
  3224. <span class="n">result</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
  3225. <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> says that </span><span class="si">%s%s</span><span class="s1"> = </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> \
  3226. <span class="p">(</span><span class="n">current_process</span><span class="p">()</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">result</span><span class="p">)</span>
  3227. <span class="c1">#</span>
  3228. <span class="c1"># Functions referenced by tasks</span>
  3229. <span class="c1">#</span>
  3230. <span class="k">def</span> <span class="nf">mul</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
  3231. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.5</span><span class="o">*</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">())</span>
  3232. <span class="k">return</span> <span class="n">a</span> <span class="o">*</span> <span class="n">b</span>
  3233. <span class="k">def</span> <span class="nf">plus</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
  3234. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.5</span><span class="o">*</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">())</span>
  3235. <span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
  3236. <span class="c1">#</span>
  3237. <span class="c1">#</span>
  3238. <span class="c1">#</span>
  3239. <span class="k">def</span> <span class="nf">test</span><span class="p">():</span>
  3240. <span class="n">NUMBER_OF_PROCESSES</span> <span class="o">=</span> <span class="mi">4</span>
  3241. <span class="n">TASKS1</span> <span class="o">=</span> <span class="p">[(</span><span class="n">mul</span><span class="p">,</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="mi">7</span><span class="p">))</span> <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">20</span><span class="p">)]</span>
  3242. <span class="n">TASKS2</span> <span class="o">=</span> <span class="p">[(</span><span class="n">plus</span><span class="p">,</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="mi">8</span><span class="p">))</span> <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">10</span><span class="p">)]</span>
  3243. <span class="c1"># Create queues</span>
  3244. <span class="n">task_queue</span> <span class="o">=</span> <span class="n">Queue</span><span class="p">()</span>
  3245. <span class="n">done_queue</span> <span class="o">=</span> <span class="n">Queue</span><span class="p">()</span>
  3246. <span class="c1"># Submit tasks</span>
  3247. <span class="k">for</span> <span class="n">task</span> <span class="ow">in</span> <span class="n">TASKS1</span><span class="p">:</span>
  3248. <span class="n">task_queue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">task</span><span class="p">)</span>
  3249. <span class="c1"># Start worker processes</span>
  3250. <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="n">NUMBER_OF_PROCESSES</span><span class="p">):</span>
  3251. <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">worker</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">task_queue</span><span class="p">,</span> <span class="n">done_queue</span><span class="p">))</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  3252. <span class="c1"># Get and print results</span>
  3253. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Unordered results:&#39;</span><span class="p">)</span>
  3254. <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="nb">len</span><span class="p">(</span><span class="n">TASKS1</span><span class="p">)):</span>
  3255. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\t</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">done_queue</span><span class="o">.</span><span class="n">get</span><span class="p">())</span>
  3256. <span class="c1"># Add more tasks using `put()`</span>
  3257. <span class="k">for</span> <span class="n">task</span> <span class="ow">in</span> <span class="n">TASKS2</span><span class="p">:</span>
  3258. <span class="n">task_queue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">task</span><span class="p">)</span>
  3259. <span class="c1"># Get and print some more results</span>
  3260. <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="nb">len</span><span class="p">(</span><span class="n">TASKS2</span><span class="p">)):</span>
  3261. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\t</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">done_queue</span><span class="o">.</span><span class="n">get</span><span class="p">())</span>
  3262. <span class="c1"># Tell child processes to stop</span>
  3263. <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="n">NUMBER_OF_PROCESSES</span><span class="p">):</span>
  3264. <span class="n">task_queue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="s1">&#39;STOP&#39;</span><span class="p">)</span>
  3265. <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>
  3266. <span class="n">freeze_support</span><span class="p">()</span>
  3267. <span class="n">test</span><span class="p">()</span>
  3268. </pre></div>
  3269. </div>
  3270. </section>
  3271. </section>
  3272. <div class="clearer"></div>
  3273. </div>
  3274. </div>
  3275. </div>
  3276. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  3277. <div class="sphinxsidebarwrapper">
  3278. <div>
  3279. <h3><a href="../contents.html">Table of Contents</a></h3>
  3280. <ul>
  3281. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code> — Process-based parallelism</a><ul>
  3282. <li><a class="reference internal" href="#introduction">Introduction</a><ul>
  3283. <li><a class="reference internal" href="#the-process-class">The <code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code> class</a></li>
  3284. <li><a class="reference internal" href="#contexts-and-start-methods">Contexts and start methods</a></li>
  3285. <li><a class="reference internal" href="#exchanging-objects-between-processes">Exchanging objects between processes</a></li>
  3286. <li><a class="reference internal" href="#synchronization-between-processes">Synchronization between processes</a></li>
  3287. <li><a class="reference internal" href="#sharing-state-between-processes">Sharing state between processes</a></li>
  3288. <li><a class="reference internal" href="#using-a-pool-of-workers">Using a pool of workers</a></li>
  3289. </ul>
  3290. </li>
  3291. <li><a class="reference internal" href="#reference">Reference</a><ul>
  3292. <li><a class="reference internal" href="#process-and-exceptions"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code> and exceptions</a></li>
  3293. <li><a class="reference internal" href="#pipes-and-queues">Pipes and Queues</a></li>
  3294. <li><a class="reference internal" href="#miscellaneous">Miscellaneous</a></li>
  3295. <li><a class="reference internal" href="#connection-objects">Connection Objects</a></li>
  3296. <li><a class="reference internal" href="#synchronization-primitives">Synchronization primitives</a></li>
  3297. <li><a class="reference internal" href="#shared-ctypes-objects">Shared <code class="xref py py-mod docutils literal notranslate"><span class="pre">ctypes</span></code> Objects</a><ul>
  3298. <li><a class="reference internal" href="#module-multiprocessing.sharedctypes">The <code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing.sharedctypes</span></code> module</a></li>
  3299. </ul>
  3300. </li>
  3301. <li><a class="reference internal" href="#managers">Managers</a><ul>
  3302. <li><a class="reference internal" href="#customized-managers">Customized managers</a></li>
  3303. <li><a class="reference internal" href="#using-a-remote-manager">Using a remote manager</a></li>
  3304. </ul>
  3305. </li>
  3306. <li><a class="reference internal" href="#proxy-objects">Proxy Objects</a><ul>
  3307. <li><a class="reference internal" href="#cleanup">Cleanup</a></li>
  3308. </ul>
  3309. </li>
  3310. <li><a class="reference internal" href="#module-multiprocessing.pool">Process Pools</a></li>
  3311. <li><a class="reference internal" href="#module-multiprocessing.connection">Listeners and Clients</a><ul>
  3312. <li><a class="reference internal" href="#address-formats">Address Formats</a></li>
  3313. </ul>
  3314. </li>
  3315. <li><a class="reference internal" href="#authentication-keys">Authentication keys</a></li>
  3316. <li><a class="reference internal" href="#logging">Logging</a></li>
  3317. <li><a class="reference internal" href="#module-multiprocessing.dummy">The <code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing.dummy</span></code> module</a></li>
  3318. </ul>
  3319. </li>
  3320. <li><a class="reference internal" href="#programming-guidelines">Programming guidelines</a><ul>
  3321. <li><a class="reference internal" href="#all-start-methods">All start methods</a></li>
  3322. <li><a class="reference internal" href="#the-spawn-and-forkserver-start-methods">The <em>spawn</em> and <em>forkserver</em> start methods</a></li>
  3323. </ul>
  3324. </li>
  3325. <li><a class="reference internal" href="#examples">Examples</a></li>
  3326. </ul>
  3327. </li>
  3328. </ul>
  3329. </div>
  3330. <div>
  3331. <h4>Previous topic</h4>
  3332. <p class="topless"><a href="threading.html"
  3333. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code> — Thread-based parallelism</a></p>
  3334. </div>
  3335. <div>
  3336. <h4>Next topic</h4>
  3337. <p class="topless"><a href="multiprocessing.shared_memory.html"
  3338. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing.shared_memory</span></code> — Shared memory for direct access across processes</a></p>
  3339. </div>
  3340. <div role="note" aria-label="source link">
  3341. <h3>This Page</h3>
  3342. <ul class="this-page-menu">
  3343. <li><a href="../bugs.html">Report a Bug</a></li>
  3344. <li>
  3345. <a href="https://github.com/python/cpython/blob/main/Doc/library/multiprocessing.rst"
  3346. rel="nofollow">Show Source
  3347. </a>
  3348. </li>
  3349. </ul>
  3350. </div>
  3351. </div>
  3352. <div id="sidebarbutton" title="Collapse sidebar">
  3353. <span>«</span>
  3354. </div>
  3355. </div>
  3356. <div class="clearer"></div>
  3357. </div>
  3358. <div class="related" role="navigation" aria-label="related navigation">
  3359. <h3>Navigation</h3>
  3360. <ul>
  3361. <li class="right" style="margin-right: 10px">
  3362. <a href="../genindex.html" title="General Index"
  3363. >index</a></li>
  3364. <li class="right" >
  3365. <a href="../py-modindex.html" title="Python Module Index"
  3366. >modules</a> |</li>
  3367. <li class="right" >
  3368. <a href="multiprocessing.shared_memory.html" title="multiprocessing.shared_memory — Shared memory for direct access across processes"
  3369. >next</a> |</li>
  3370. <li class="right" >
  3371. <a href="threading.html" title="threading — Thread-based parallelism"
  3372. >previous</a> |</li>
  3373. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  3374. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  3375. <li class="switchers">
  3376. <div class="language_switcher_placeholder"></div>
  3377. <div class="version_switcher_placeholder"></div>
  3378. </li>
  3379. <li>
  3380. </li>
  3381. <li id="cpython-language-and-version">
  3382. <a href="../index.html">3.12.3 Documentation</a> &#187;
  3383. </li>
  3384. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  3385. <li class="nav-item nav-item-2"><a href="concurrency.html" >Concurrent Execution</a> &#187;</li>
  3386. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code> — Process-based parallelism</a></li>
  3387. <li class="right">
  3388. <div class="inline-search" role="search">
  3389. <form class="inline-search" action="../search.html" method="get">
  3390. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  3391. <input type="submit" value="Go" />
  3392. </form>
  3393. </div>
  3394. |
  3395. </li>
  3396. <li class="right">
  3397. <label class="theme-selector-label">
  3398. Theme
  3399. <select class="theme-selector" oninput="activateTheme(this.value)">
  3400. <option value="auto" selected>Auto</option>
  3401. <option value="light">Light</option>
  3402. <option value="dark">Dark</option>
  3403. </select>
  3404. </label> |</li>
  3405. </ul>
  3406. </div>
  3407. <div class="footer">
  3408. &copy;
  3409. <a href="../copyright.html">
  3410. Copyright
  3411. </a>
  3412. 2001-2024, Python Software Foundation.
  3413. <br />
  3414. This page is licensed under the Python Software Foundation License Version 2.
  3415. <br />
  3416. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  3417. <br />
  3418. See <a href="/license.html">History and License</a> for more information.<br />
  3419. <br />
  3420. The Python Software Foundation is a non-profit corporation.
  3421. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  3422. <br />
  3423. <br />
  3424. Last updated on Apr 09, 2024 (13:47 UTC).
  3425. <a href="/bugs.html">Found a bug</a>?
  3426. <br />
  3427. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  3428. </div>
  3429. </body>
  3430. </html>
上海开阖软件有限公司 沪ICP备12045867号-1