gooderp18绿色标准版
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

685 lignes
52KB

  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="Subprocesses" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/asyncio-subprocess.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/asyncio/subprocess.py, Lib/asyncio/base_subprocess.py This section describes high-level async/await asyncio APIs to create and manage subprocesses. Here’s an example of how asyncio..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/asyncio/subprocess.py, Lib/asyncio/base_subprocess.py This section describes high-level async/await asyncio APIs to create and manage subprocesses. Here’s an example of how asyncio..." />
  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>Subprocesses &#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="Queues" href="asyncio-queue.html" />
  33. <link rel="prev" title="Synchronization Primitives" href="asyncio-sync.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/asyncio-subprocess.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="#">Subprocesses</a><ul>
  86. <li><a class="reference internal" href="#creating-subprocesses">Creating Subprocesses</a></li>
  87. <li><a class="reference internal" href="#constants">Constants</a></li>
  88. <li><a class="reference internal" href="#interacting-with-subprocesses">Interacting with Subprocesses</a><ul>
  89. <li><a class="reference internal" href="#subprocess-and-threads">Subprocess and Threads</a></li>
  90. <li><a class="reference internal" href="#examples">Examples</a></li>
  91. </ul>
  92. </li>
  93. </ul>
  94. </li>
  95. </ul>
  96. </div>
  97. <div>
  98. <h4>Previous topic</h4>
  99. <p class="topless"><a href="asyncio-sync.html"
  100. title="previous chapter">Synchronization Primitives</a></p>
  101. </div>
  102. <div>
  103. <h4>Next topic</h4>
  104. <p class="topless"><a href="asyncio-queue.html"
  105. title="next chapter">Queues</a></p>
  106. </div>
  107. <div role="note" aria-label="source link">
  108. <h3>This Page</h3>
  109. <ul class="this-page-menu">
  110. <li><a href="../bugs.html">Report a Bug</a></li>
  111. <li>
  112. <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio-subprocess.rst"
  113. rel="nofollow">Show Source
  114. </a>
  115. </li>
  116. </ul>
  117. </div>
  118. </nav>
  119. </div>
  120. </div>
  121. <div class="related" role="navigation" aria-label="related navigation">
  122. <h3>Navigation</h3>
  123. <ul>
  124. <li class="right" style="margin-right: 10px">
  125. <a href="../genindex.html" title="General Index"
  126. accesskey="I">index</a></li>
  127. <li class="right" >
  128. <a href="../py-modindex.html" title="Python Module Index"
  129. >modules</a> |</li>
  130. <li class="right" >
  131. <a href="asyncio-queue.html" title="Queues"
  132. accesskey="N">next</a> |</li>
  133. <li class="right" >
  134. <a href="asyncio-sync.html" title="Synchronization Primitives"
  135. accesskey="P">previous</a> |</li>
  136. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  137. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  138. <li class="switchers">
  139. <div class="language_switcher_placeholder"></div>
  140. <div class="version_switcher_placeholder"></div>
  141. </li>
  142. <li>
  143. </li>
  144. <li id="cpython-language-and-version">
  145. <a href="../index.html">3.12.3 Documentation</a> &#187;
  146. </li>
  147. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  148. <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
  149. <li class="nav-item nav-item-3"><a href="asyncio.html" accesskey="U"><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code> — Asynchronous I/O</a> &#187;</li>
  150. <li class="nav-item nav-item-this"><a href="">Subprocesses</a></li>
  151. <li class="right">
  152. <div class="inline-search" role="search">
  153. <form class="inline-search" action="../search.html" method="get">
  154. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  155. <input type="submit" value="Go" />
  156. </form>
  157. </div>
  158. |
  159. </li>
  160. <li class="right">
  161. <label class="theme-selector-label">
  162. Theme
  163. <select class="theme-selector" oninput="activateTheme(this.value)">
  164. <option value="auto" selected>Auto</option>
  165. <option value="light">Light</option>
  166. <option value="dark">Dark</option>
  167. </select>
  168. </label> |</li>
  169. </ul>
  170. </div>
  171. <div class="document">
  172. <div class="documentwrapper">
  173. <div class="bodywrapper">
  174. <div class="body" role="main">
  175. <section id="subprocesses">
  176. <span id="asyncio-subprocess"></span><h1>Subprocesses<a class="headerlink" href="#subprocesses" title="Link to this heading">¶</a></h1>
  177. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/asyncio/subprocess.py">Lib/asyncio/subprocess.py</a>,
  178. <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/asyncio/base_subprocess.py">Lib/asyncio/base_subprocess.py</a></p>
  179. <hr class="docutils" />
  180. <p>This section describes high-level async/await asyncio APIs to
  181. create and manage subprocesses.</p>
  182. <p id="asyncio-example-subprocess-shell">Here’s an example of how asyncio can run a shell command and
  183. obtain its result:</p>
  184. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
  185. <span class="k">async</span> <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="n">cmd</span><span class="p">):</span>
  186. <span class="n">proc</span> <span class="o">=</span> <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">create_subprocess_shell</span><span class="p">(</span>
  187. <span class="n">cmd</span><span class="p">,</span>
  188. <span class="n">stdout</span><span class="o">=</span><span class="n">asyncio</span><span class="o">.</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span>
  189. <span class="n">stderr</span><span class="o">=</span><span class="n">asyncio</span><span class="o">.</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">)</span>
  190. <span class="n">stdout</span><span class="p">,</span> <span class="n">stderr</span> <span class="o">=</span> <span class="k">await</span> <span class="n">proc</span><span class="o">.</span><span class="n">communicate</span><span class="p">()</span>
  191. <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;[</span><span class="si">{</span><span class="n">cmd</span><span class="si">!r}</span><span class="s1"> exited with </span><span class="si">{</span><span class="n">proc</span><span class="o">.</span><span class="n">returncode</span><span class="si">}</span><span class="s1">]&#39;</span><span class="p">)</span>
  192. <span class="k">if</span> <span class="n">stdout</span><span class="p">:</span>
  193. <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;[stdout]</span><span class="se">\n</span><span class="si">{</span><span class="n">stdout</span><span class="o">.</span><span class="n">decode</span><span class="p">()</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
  194. <span class="k">if</span> <span class="n">stderr</span><span class="p">:</span>
  195. <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;[stderr]</span><span class="se">\n</span><span class="si">{</span><span class="n">stderr</span><span class="o">.</span><span class="n">decode</span><span class="p">()</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
  196. <span class="n">asyncio</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">run</span><span class="p">(</span><span class="s1">&#39;ls /zzz&#39;</span><span class="p">))</span>
  197. </pre></div>
  198. </div>
  199. <p>will print:</p>
  200. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="s1">&#39;ls /zzz&#39;</span> <span class="n">exited</span> <span class="k">with</span> <span class="mi">1</span><span class="p">]</span>
  201. <span class="p">[</span><span class="n">stderr</span><span class="p">]</span>
  202. <span class="n">ls</span><span class="p">:</span> <span class="o">/</span><span class="n">zzz</span><span class="p">:</span> <span class="n">No</span> <span class="n">such</span> <span class="n">file</span> <span class="ow">or</span> <span class="n">directory</span>
  203. </pre></div>
  204. </div>
  205. <p>Because all asyncio subprocess functions are asynchronous and asyncio
  206. provides many tools to work with such functions, it is easy to execute
  207. and monitor multiple subprocesses in parallel. It is indeed trivial
  208. to modify the above example to run several commands simultaneously:</p>
  209. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">async</span> <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
  210. <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">gather</span><span class="p">(</span>
  211. <span class="n">run</span><span class="p">(</span><span class="s1">&#39;ls /zzz&#39;</span><span class="p">),</span>
  212. <span class="n">run</span><span class="p">(</span><span class="s1">&#39;sleep 1; echo &quot;hello&quot;&#39;</span><span class="p">))</span>
  213. <span class="n">asyncio</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">main</span><span class="p">())</span>
  214. </pre></div>
  215. </div>
  216. <p>See also the <a class="reference internal" href="#examples">Examples</a> subsection.</p>
  217. <section id="creating-subprocesses">
  218. <h2>Creating Subprocesses<a class="headerlink" href="#creating-subprocesses" title="Link to this heading">¶</a></h2>
  219. <dl class="py function">
  220. <dt class="sig sig-object py" id="asyncio.create_subprocess_exec">
  221. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">create_subprocess_exec</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">program</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">stdin</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">stdout</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">stderr</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">limit</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><span class="n"><span class="pre">kwds</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.create_subprocess_exec" title="Link to this definition">¶</a></dt>
  222. <dd><p>Create a subprocess.</p>
  223. <p>The <em>limit</em> argument sets the buffer limit for <a class="reference internal" href="asyncio-stream.html#asyncio.StreamReader" title="asyncio.StreamReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code></a>
  224. wrappers for <code class="xref py py-attr docutils literal notranslate"><span class="pre">Process.stdout</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">Process.stderr</span></code>
  225. (if <a class="reference internal" href="subprocess.html#subprocess.PIPE" title="subprocess.PIPE"><code class="xref py py-const docutils literal notranslate"><span class="pre">subprocess.PIPE</span></code></a> is passed to <em>stdout</em> and <em>stderr</em> arguments).</p>
  226. <p>Return a <a class="reference internal" href="#asyncio.subprocess.Process" title="asyncio.subprocess.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> instance.</p>
  227. <p>See the documentation of <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.subprocess_exec" title="asyncio.loop.subprocess_exec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.subprocess_exec()</span></code></a> for other
  228. parameters.</p>
  229. <div class="versionchanged">
  230. <p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p>
  231. </div>
  232. </dd></dl>
  233. <dl class="py function">
  234. <dt class="sig sig-object py" id="asyncio.create_subprocess_shell">
  235. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">create_subprocess_shell</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cmd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stdin</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">stdout</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">stderr</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">limit</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><span class="n"><span class="pre">kwds</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.create_subprocess_shell" title="Link to this definition">¶</a></dt>
  236. <dd><p>Run the <em>cmd</em> shell command.</p>
  237. <p>The <em>limit</em> argument sets the buffer limit for <a class="reference internal" href="asyncio-stream.html#asyncio.StreamReader" title="asyncio.StreamReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code></a>
  238. wrappers for <code class="xref py py-attr docutils literal notranslate"><span class="pre">Process.stdout</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">Process.stderr</span></code>
  239. (if <a class="reference internal" href="subprocess.html#subprocess.PIPE" title="subprocess.PIPE"><code class="xref py py-const docutils literal notranslate"><span class="pre">subprocess.PIPE</span></code></a> is passed to <em>stdout</em> and <em>stderr</em> arguments).</p>
  240. <p>Return a <a class="reference internal" href="#asyncio.subprocess.Process" title="asyncio.subprocess.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> instance.</p>
  241. <p>See the documentation of <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.subprocess_shell" title="asyncio.loop.subprocess_shell"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.subprocess_shell()</span></code></a> for other
  242. parameters.</p>
  243. <div class="admonition important">
  244. <p class="admonition-title">Important</p>
  245. <p>It is the application’s responsibility to ensure that all whitespace and
  246. special characters are quoted appropriately to avoid <a class="reference external" href="https://en.wikipedia.org/wiki/Shell_injection#Shell_injection">shell injection</a>
  247. vulnerabilities. The <a class="reference internal" href="shlex.html#shlex.quote" title="shlex.quote"><code class="xref py py-func docutils literal notranslate"><span class="pre">shlex.quote()</span></code></a> function can be used to properly
  248. escape whitespace and special shell characters in strings that are going
  249. to be used to construct shell commands.</p>
  250. </div>
  251. <div class="versionchanged">
  252. <p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p>
  253. </div>
  254. </dd></dl>
  255. <div class="admonition note">
  256. <p class="admonition-title">Note</p>
  257. <p>Subprocesses are available for Windows if a <a class="reference internal" href="asyncio-eventloop.html#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProactorEventLoop</span></code></a> is
  258. used. See <a class="reference internal" href="asyncio-platforms.html#asyncio-windows-subprocess"><span class="std std-ref">Subprocess Support on Windows</span></a>
  259. for details.</p>
  260. </div>
  261. <div class="admonition seealso">
  262. <p class="admonition-title">See also</p>
  263. <p>asyncio also has the following <em>low-level</em> APIs to work with subprocesses:
  264. <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.subprocess_exec" title="asyncio.loop.subprocess_exec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.subprocess_exec()</span></code></a>, <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.subprocess_shell" title="asyncio.loop.subprocess_shell"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.subprocess_shell()</span></code></a>,
  265. <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.connect_read_pipe" title="asyncio.loop.connect_read_pipe"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.connect_read_pipe()</span></code></a>, <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.connect_write_pipe" title="asyncio.loop.connect_write_pipe"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.connect_write_pipe()</span></code></a>,
  266. as well as the <a class="reference internal" href="asyncio-protocol.html#asyncio-subprocess-transports"><span class="std std-ref">Subprocess Transports</span></a>
  267. and <a class="reference internal" href="asyncio-protocol.html#asyncio-subprocess-protocols"><span class="std std-ref">Subprocess Protocols</span></a>.</p>
  268. </div>
  269. </section>
  270. <section id="constants">
  271. <h2>Constants<a class="headerlink" href="#constants" title="Link to this heading">¶</a></h2>
  272. <dl class="py data">
  273. <dt class="sig sig-object py" id="asyncio.subprocess.PIPE">
  274. <span class="sig-prename descclassname"><span class="pre">asyncio.subprocess.</span></span><span class="sig-name descname"><span class="pre">PIPE</span></span><a class="headerlink" href="#asyncio.subprocess.PIPE" title="Link to this definition">¶</a></dt>
  275. <dd><p>Can be passed to the <em>stdin</em>, <em>stdout</em> or <em>stderr</em> parameters.</p>
  276. <p>If <em>PIPE</em> is passed to <em>stdin</em> argument, the
  277. <a class="reference internal" href="#asyncio.subprocess.Process.stdin" title="asyncio.subprocess.Process.stdin"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Process.stdin</span></code></a> attribute
  278. will point to a <code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code> instance.</p>
  279. <p>If <em>PIPE</em> is passed to <em>stdout</em> or <em>stderr</em> arguments, the
  280. <a class="reference internal" href="#asyncio.subprocess.Process.stdout" title="asyncio.subprocess.Process.stdout"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Process.stdout</span></code></a> and
  281. <a class="reference internal" href="#asyncio.subprocess.Process.stderr" title="asyncio.subprocess.Process.stderr"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Process.stderr</span></code></a>
  282. attributes will point to <code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code> instances.</p>
  283. </dd></dl>
  284. <dl class="py data">
  285. <dt class="sig sig-object py" id="asyncio.subprocess.STDOUT">
  286. <span class="sig-prename descclassname"><span class="pre">asyncio.subprocess.</span></span><span class="sig-name descname"><span class="pre">STDOUT</span></span><a class="headerlink" href="#asyncio.subprocess.STDOUT" title="Link to this definition">¶</a></dt>
  287. <dd><p>Special value that can be used as the <em>stderr</em> argument and indicates
  288. that standard error should be redirected into standard output.</p>
  289. </dd></dl>
  290. <dl class="py data">
  291. <dt class="sig sig-object py" id="asyncio.subprocess.DEVNULL">
  292. <span class="sig-prename descclassname"><span class="pre">asyncio.subprocess.</span></span><span class="sig-name descname"><span class="pre">DEVNULL</span></span><a class="headerlink" href="#asyncio.subprocess.DEVNULL" title="Link to this definition">¶</a></dt>
  293. <dd><p>Special value that can be used as the <em>stdin</em>, <em>stdout</em> or <em>stderr</em> argument
  294. to process creation functions. It indicates that the special file
  295. <a class="reference internal" href="os.html#os.devnull" title="os.devnull"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.devnull</span></code></a> will be used for the corresponding subprocess stream.</p>
  296. </dd></dl>
  297. </section>
  298. <section id="interacting-with-subprocesses">
  299. <h2>Interacting with Subprocesses<a class="headerlink" href="#interacting-with-subprocesses" title="Link to this heading">¶</a></h2>
  300. <p>Both <a class="reference internal" href="#asyncio.create_subprocess_exec" title="asyncio.create_subprocess_exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_subprocess_exec()</span></code></a> and <a class="reference internal" href="#asyncio.create_subprocess_shell" title="asyncio.create_subprocess_shell"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_subprocess_shell()</span></code></a>
  301. functions return instances of the <em>Process</em> class. <em>Process</em> is a high-level
  302. wrapper that allows communicating with subprocesses and watching for
  303. their completion.</p>
  304. <dl class="py class">
  305. <dt class="sig sig-object py" id="asyncio.subprocess.Process">
  306. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">asyncio.subprocess.</span></span><span class="sig-name descname"><span class="pre">Process</span></span><a class="headerlink" href="#asyncio.subprocess.Process" title="Link to this definition">¶</a></dt>
  307. <dd><p>An object that wraps OS processes created by the
  308. <code class="xref py py-func docutils literal notranslate"><span class="pre">create_subprocess_exec()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">create_subprocess_shell()</span></code>
  309. functions.</p>
  310. <p>This class is designed to have a similar API to the
  311. <a class="reference internal" href="subprocess.html#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">subprocess.Popen</span></code></a> class, but there are some
  312. notable differences:</p>
  313. <ul class="simple">
  314. <li><p>unlike Popen, Process instances do not have an equivalent to
  315. the <a class="reference internal" href="subprocess.html#subprocess.Popen.poll" title="subprocess.Popen.poll"><code class="xref py py-meth docutils literal notranslate"><span class="pre">poll()</span></code></a> method;</p></li>
  316. <li><p>the <a class="reference internal" href="#asyncio.subprocess.Process.communicate" title="asyncio.subprocess.Process.communicate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">communicate()</span></code></a> and
  317. <a class="reference internal" href="#asyncio.subprocess.Process.wait" title="asyncio.subprocess.Process.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> methods don’t have a
  318. <em>timeout</em> parameter: use the <a class="reference internal" href="asyncio-task.html#asyncio.wait_for" title="asyncio.wait_for"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait_for()</span></code></a> function;</p></li>
  319. <li><p>the <a class="reference internal" href="#asyncio.subprocess.Process.wait" title="asyncio.subprocess.Process.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Process.wait()</span></code></a> method
  320. is asynchronous, whereas <a class="reference internal" href="subprocess.html#subprocess.Popen.wait" title="subprocess.Popen.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">subprocess.Popen.wait()</span></code></a> method
  321. is implemented as a blocking busy loop;</p></li>
  322. <li><p>the <em>universal_newlines</em> parameter is not supported.</p></li>
  323. </ul>
  324. <p>This class is <a class="reference internal" href="asyncio-dev.html#asyncio-multithreading"><span class="std std-ref">not thread safe</span></a>.</p>
  325. <p>See also the <a class="reference internal" href="#asyncio-subprocess-threads"><span class="std std-ref">Subprocess and Threads</span></a>
  326. section.</p>
  327. <dl class="py method">
  328. <dt class="sig sig-object py" id="asyncio.subprocess.Process.wait">
  329. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-name descname"><span class="pre">wait</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.subprocess.Process.wait" title="Link to this definition">¶</a></dt>
  330. <dd><p>Wait for the child process to terminate.</p>
  331. <p>Set and return the <a class="reference internal" href="#asyncio.subprocess.Process.returncode" title="asyncio.subprocess.Process.returncode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">returncode</span></code></a> attribute.</p>
  332. <div class="admonition note">
  333. <p class="admonition-title">Note</p>
  334. <p>This method can deadlock when using <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code> or
  335. <code class="docutils literal notranslate"><span class="pre">stderr=PIPE</span></code> and the child process generates so much output
  336. that it blocks waiting for the OS pipe buffer to accept
  337. more data. Use the <a class="reference internal" href="#asyncio.subprocess.Process.communicate" title="asyncio.subprocess.Process.communicate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">communicate()</span></code></a> method when using pipes
  338. to avoid this condition.</p>
  339. </div>
  340. </dd></dl>
  341. <dl class="py method">
  342. <dt class="sig sig-object py" id="asyncio.subprocess.Process.communicate">
  343. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-name descname"><span class="pre">communicate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">input</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.subprocess.Process.communicate" title="Link to this definition">¶</a></dt>
  344. <dd><p>Interact with process:</p>
  345. <ol class="arabic simple">
  346. <li><p>send data to <em>stdin</em> (if <em>input</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>);</p></li>
  347. <li><p>closes <em>stdin</em>;</p></li>
  348. <li><p>read data from <em>stdout</em> and <em>stderr</em>, until EOF is reached;</p></li>
  349. <li><p>wait for process to terminate.</p></li>
  350. </ol>
  351. <p>The optional <em>input</em> argument is the data (<a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object)
  352. that will be sent to the child process.</p>
  353. <p>Return a tuple <code class="docutils literal notranslate"><span class="pre">(stdout_data,</span> <span class="pre">stderr_data)</span></code>.</p>
  354. <p>If either <a class="reference internal" href="exceptions.html#BrokenPipeError" title="BrokenPipeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BrokenPipeError</span></code></a> or <a class="reference internal" href="exceptions.html#ConnectionResetError" title="ConnectionResetError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionResetError</span></code></a>
  355. exception is raised when writing <em>input</em> into <em>stdin</em>, the
  356. exception is ignored. This condition occurs when the process
  357. exits before all data are written into <em>stdin</em>.</p>
  358. <p>If it is desired to send data to the process’ <em>stdin</em>,
  359. the process needs to be created with <code class="docutils literal notranslate"><span class="pre">stdin=PIPE</span></code>. Similarly,
  360. to get anything other than <code class="docutils literal notranslate"><span class="pre">None</span></code> in the result tuple, the
  361. process has to be created with <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code> and/or
  362. <code class="docutils literal notranslate"><span class="pre">stderr=PIPE</span></code> arguments.</p>
  363. <p>Note, that the data read is buffered in memory, so do not use
  364. this method if the data size is large or unlimited.</p>
  365. <div class="versionchanged">
  366. <p><span class="versionmodified changed">Changed in version 3.12: </span><em>stdin</em> gets closed when <cite>input=None</cite> too.</p>
  367. </div>
  368. </dd></dl>
  369. <dl class="py method">
  370. <dt class="sig sig-object py" id="asyncio.subprocess.Process.send_signal">
  371. <span class="sig-name descname"><span class="pre">send_signal</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">signal</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.subprocess.Process.send_signal" title="Link to this definition">¶</a></dt>
  372. <dd><p>Sends the signal <em>signal</em> to the child process.</p>
  373. <div class="admonition note">
  374. <p class="admonition-title">Note</p>
  375. <p>On Windows, <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> is an alias for <a class="reference internal" href="#asyncio.subprocess.Process.terminate" title="asyncio.subprocess.Process.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">terminate()</span></code></a>.
  376. <code class="docutils literal notranslate"><span class="pre">CTRL_C_EVENT</span></code> and <code class="docutils literal notranslate"><span class="pre">CTRL_BREAK_EVENT</span></code> can be sent to processes
  377. started with a <em>creationflags</em> parameter which includes
  378. <code class="docutils literal notranslate"><span class="pre">CREATE_NEW_PROCESS_GROUP</span></code>.</p>
  379. </div>
  380. </dd></dl>
  381. <dl class="py method">
  382. <dt class="sig sig-object py" id="asyncio.subprocess.Process.terminate">
  383. <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="#asyncio.subprocess.Process.terminate" title="Link to this definition">¶</a></dt>
  384. <dd><p>Stop the child process.</p>
  385. <p>On POSIX systems this method sends <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> to the
  386. child process.</p>
  387. <p>On Windows the Win32 API function <code class="xref c c-func docutils literal notranslate"><span class="pre">TerminateProcess()</span></code> is
  388. called to stop the child process.</p>
  389. </dd></dl>
  390. <dl class="py method">
  391. <dt class="sig sig-object py" id="asyncio.subprocess.Process.kill">
  392. <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="#asyncio.subprocess.Process.kill" title="Link to this definition">¶</a></dt>
  393. <dd><p>Kill the child process.</p>
  394. <p>On POSIX systems this method sends <code class="xref py py-data docutils literal notranslate"><span class="pre">SIGKILL</span></code> to the child
  395. process.</p>
  396. <p>On Windows this method is an alias for <a class="reference internal" href="#asyncio.subprocess.Process.terminate" title="asyncio.subprocess.Process.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">terminate()</span></code></a>.</p>
  397. </dd></dl>
  398. <dl class="py attribute">
  399. <dt class="sig sig-object py" id="asyncio.subprocess.Process.stdin">
  400. <span class="sig-name descname"><span class="pre">stdin</span></span><a class="headerlink" href="#asyncio.subprocess.Process.stdin" title="Link to this definition">¶</a></dt>
  401. <dd><p>Standard input stream (<code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code>) or <code class="docutils literal notranslate"><span class="pre">None</span></code>
  402. if the process was created with <code class="docutils literal notranslate"><span class="pre">stdin=None</span></code>.</p>
  403. </dd></dl>
  404. <dl class="py attribute">
  405. <dt class="sig sig-object py" id="asyncio.subprocess.Process.stdout">
  406. <span class="sig-name descname"><span class="pre">stdout</span></span><a class="headerlink" href="#asyncio.subprocess.Process.stdout" title="Link to this definition">¶</a></dt>
  407. <dd><p>Standard output stream (<code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code>) or <code class="docutils literal notranslate"><span class="pre">None</span></code>
  408. if the process was created with <code class="docutils literal notranslate"><span class="pre">stdout=None</span></code>.</p>
  409. </dd></dl>
  410. <dl class="py attribute">
  411. <dt class="sig sig-object py" id="asyncio.subprocess.Process.stderr">
  412. <span class="sig-name descname"><span class="pre">stderr</span></span><a class="headerlink" href="#asyncio.subprocess.Process.stderr" title="Link to this definition">¶</a></dt>
  413. <dd><p>Standard error stream (<code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code>) or <code class="docutils literal notranslate"><span class="pre">None</span></code>
  414. if the process was created with <code class="docutils literal notranslate"><span class="pre">stderr=None</span></code>.</p>
  415. </dd></dl>
  416. <div class="admonition warning">
  417. <p class="admonition-title">Warning</p>
  418. <p>Use the <a class="reference internal" href="#asyncio.subprocess.Process.communicate" title="asyncio.subprocess.Process.communicate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">communicate()</span></code></a> method rather than
  419. <a class="reference internal" href="#asyncio.subprocess.Process.stdin" title="asyncio.subprocess.Process.stdin"><code class="xref py py-attr docutils literal notranslate"><span class="pre">process.stdin.write()</span></code></a>,
  420. <a class="reference internal" href="#asyncio.subprocess.Process.stdout" title="asyncio.subprocess.Process.stdout"><code class="xref py py-attr docutils literal notranslate"><span class="pre">await</span> <span class="pre">process.stdout.read()</span></code></a> or
  421. <a class="reference internal" href="#asyncio.subprocess.Process.stderr" title="asyncio.subprocess.Process.stderr"><code class="xref py py-attr docutils literal notranslate"><span class="pre">await</span> <span class="pre">process.stderr.read()</span></code></a>.
  422. This avoids deadlocks due to streams pausing reading or writing
  423. and blocking the child process.</p>
  424. </div>
  425. <dl class="py attribute">
  426. <dt class="sig sig-object py" id="asyncio.subprocess.Process.pid">
  427. <span class="sig-name descname"><span class="pre">pid</span></span><a class="headerlink" href="#asyncio.subprocess.Process.pid" title="Link to this definition">¶</a></dt>
  428. <dd><p>Process identification number (PID).</p>
  429. <p>Note that for processes created by the <code class="xref py py-func docutils literal notranslate"><span class="pre">create_subprocess_shell()</span></code>
  430. function, this attribute is the PID of the spawned shell.</p>
  431. </dd></dl>
  432. <dl class="py attribute">
  433. <dt class="sig sig-object py" id="asyncio.subprocess.Process.returncode">
  434. <span class="sig-name descname"><span class="pre">returncode</span></span><a class="headerlink" href="#asyncio.subprocess.Process.returncode" title="Link to this definition">¶</a></dt>
  435. <dd><p>Return code of the process when it exits.</p>
  436. <p>A <code class="docutils literal notranslate"><span class="pre">None</span></code> value indicates that the process has not terminated yet.</p>
  437. <p>A negative value <code class="docutils literal notranslate"><span class="pre">-N</span></code> indicates that the child was terminated
  438. by signal <code class="docutils literal notranslate"><span class="pre">N</span></code> (POSIX only).</p>
  439. </dd></dl>
  440. </dd></dl>
  441. <section id="subprocess-and-threads">
  442. <span id="asyncio-subprocess-threads"></span><h3>Subprocess and Threads<a class="headerlink" href="#subprocess-and-threads" title="Link to this heading">¶</a></h3>
  443. <p>Standard asyncio event loop supports running subprocesses from different threads by
  444. default.</p>
  445. <p>On Windows subprocesses are provided by <a class="reference internal" href="asyncio-eventloop.html#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProactorEventLoop</span></code></a> only (default),
  446. <a class="reference internal" href="asyncio-eventloop.html#asyncio.SelectorEventLoop" title="asyncio.SelectorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectorEventLoop</span></code></a> has no subprocess support.</p>
  447. <p>On UNIX <em>child watchers</em> are used for subprocess finish waiting, see
  448. <a class="reference internal" href="asyncio-policy.html#asyncio-watchers"><span class="std std-ref">Process Watchers</span></a> for more info.</p>
  449. <div class="versionchanged">
  450. <p><span class="versionmodified changed">Changed in version 3.8: </span>UNIX switched to use <a class="reference internal" href="asyncio-policy.html#asyncio.ThreadedChildWatcher" title="asyncio.ThreadedChildWatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadedChildWatcher</span></code></a> for spawning subprocesses from
  451. different threads without any limitation.</p>
  452. <p>Spawning a subprocess with <em>inactive</em> current child watcher raises
  453. <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>
  454. </div>
  455. <p>Note that alternative event loop implementations might have own limitations;
  456. please refer to their documentation.</p>
  457. <div class="admonition seealso">
  458. <p class="admonition-title">See also</p>
  459. <p>The <a class="reference internal" href="asyncio-dev.html#asyncio-multithreading"><span class="std std-ref">Concurrency and multithreading in asyncio</span></a> section.</p>
  460. </div>
  461. </section>
  462. <section id="examples">
  463. <h3>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h3>
  464. <p>An example using the <a class="reference internal" href="#asyncio.subprocess.Process" title="asyncio.subprocess.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> class to
  465. control a subprocess and the <a class="reference internal" href="asyncio-stream.html#asyncio.StreamReader" title="asyncio.StreamReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code></a> class to read from
  466. its standard output.</p>
  467. <p id="asyncio-example-create-subprocess-exec">The subprocess is created by the <a class="reference internal" href="#asyncio.create_subprocess_exec" title="asyncio.create_subprocess_exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_subprocess_exec()</span></code></a>
  468. function:</p>
  469. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
  470. <span class="kn">import</span> <span class="nn">sys</span>
  471. <span class="k">async</span> <span class="k">def</span> <span class="nf">get_date</span><span class="p">():</span>
  472. <span class="n">code</span> <span class="o">=</span> <span class="s1">&#39;import datetime; print(datetime.datetime.now())&#39;</span>
  473. <span class="c1"># Create the subprocess; redirect the standard output</span>
  474. <span class="c1"># into a pipe.</span>
  475. <span class="n">proc</span> <span class="o">=</span> <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">create_subprocess_exec</span><span class="p">(</span>
  476. <span class="n">sys</span><span class="o">.</span><span class="n">executable</span><span class="p">,</span> <span class="s1">&#39;-c&#39;</span><span class="p">,</span> <span class="n">code</span><span class="p">,</span>
  477. <span class="n">stdout</span><span class="o">=</span><span class="n">asyncio</span><span class="o">.</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">)</span>
  478. <span class="c1"># Read one line of output.</span>
  479. <span class="n">data</span> <span class="o">=</span> <span class="k">await</span> <span class="n">proc</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
  480. <span class="n">line</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span>
  481. <span class="c1"># Wait for the subprocess exit.</span>
  482. <span class="k">await</span> <span class="n">proc</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
  483. <span class="k">return</span> <span class="n">line</span>
  484. <span class="n">date</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">get_date</span><span class="p">())</span>
  485. <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Current date: </span><span class="si">{</span><span class="n">date</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
  486. </pre></div>
  487. </div>
  488. <p>See also the <a class="reference internal" href="asyncio-protocol.html#asyncio-example-subprocess-proto"><span class="std std-ref">same example</span></a>
  489. written using low-level APIs.</p>
  490. </section>
  491. </section>
  492. </section>
  493. <div class="clearer"></div>
  494. </div>
  495. </div>
  496. </div>
  497. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  498. <div class="sphinxsidebarwrapper">
  499. <div>
  500. <h3><a href="../contents.html">Table of Contents</a></h3>
  501. <ul>
  502. <li><a class="reference internal" href="#">Subprocesses</a><ul>
  503. <li><a class="reference internal" href="#creating-subprocesses">Creating Subprocesses</a></li>
  504. <li><a class="reference internal" href="#constants">Constants</a></li>
  505. <li><a class="reference internal" href="#interacting-with-subprocesses">Interacting with Subprocesses</a><ul>
  506. <li><a class="reference internal" href="#subprocess-and-threads">Subprocess and Threads</a></li>
  507. <li><a class="reference internal" href="#examples">Examples</a></li>
  508. </ul>
  509. </li>
  510. </ul>
  511. </li>
  512. </ul>
  513. </div>
  514. <div>
  515. <h4>Previous topic</h4>
  516. <p class="topless"><a href="asyncio-sync.html"
  517. title="previous chapter">Synchronization Primitives</a></p>
  518. </div>
  519. <div>
  520. <h4>Next topic</h4>
  521. <p class="topless"><a href="asyncio-queue.html"
  522. title="next chapter">Queues</a></p>
  523. </div>
  524. <div role="note" aria-label="source link">
  525. <h3>This Page</h3>
  526. <ul class="this-page-menu">
  527. <li><a href="../bugs.html">Report a Bug</a></li>
  528. <li>
  529. <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio-subprocess.rst"
  530. rel="nofollow">Show Source
  531. </a>
  532. </li>
  533. </ul>
  534. </div>
  535. </div>
  536. <div id="sidebarbutton" title="Collapse sidebar">
  537. <span>«</span>
  538. </div>
  539. </div>
  540. <div class="clearer"></div>
  541. </div>
  542. <div class="related" role="navigation" aria-label="related navigation">
  543. <h3>Navigation</h3>
  544. <ul>
  545. <li class="right" style="margin-right: 10px">
  546. <a href="../genindex.html" title="General Index"
  547. >index</a></li>
  548. <li class="right" >
  549. <a href="../py-modindex.html" title="Python Module Index"
  550. >modules</a> |</li>
  551. <li class="right" >
  552. <a href="asyncio-queue.html" title="Queues"
  553. >next</a> |</li>
  554. <li class="right" >
  555. <a href="asyncio-sync.html" title="Synchronization Primitives"
  556. >previous</a> |</li>
  557. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  558. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  559. <li class="switchers">
  560. <div class="language_switcher_placeholder"></div>
  561. <div class="version_switcher_placeholder"></div>
  562. </li>
  563. <li>
  564. </li>
  565. <li id="cpython-language-and-version">
  566. <a href="../index.html">3.12.3 Documentation</a> &#187;
  567. </li>
  568. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  569. <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
  570. <li class="nav-item nav-item-3"><a href="asyncio.html" ><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code> — Asynchronous I/O</a> &#187;</li>
  571. <li class="nav-item nav-item-this"><a href="">Subprocesses</a></li>
  572. <li class="right">
  573. <div class="inline-search" role="search">
  574. <form class="inline-search" action="../search.html" method="get">
  575. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  576. <input type="submit" value="Go" />
  577. </form>
  578. </div>
  579. |
  580. </li>
  581. <li class="right">
  582. <label class="theme-selector-label">
  583. Theme
  584. <select class="theme-selector" oninput="activateTheme(this.value)">
  585. <option value="auto" selected>Auto</option>
  586. <option value="light">Light</option>
  587. <option value="dark">Dark</option>
  588. </select>
  589. </label> |</li>
  590. </ul>
  591. </div>
  592. <div class="footer">
  593. &copy;
  594. <a href="../copyright.html">
  595. Copyright
  596. </a>
  597. 2001-2024, Python Software Foundation.
  598. <br />
  599. This page is licensed under the Python Software Foundation License Version 2.
  600. <br />
  601. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  602. <br />
  603. See <a href="/license.html">History and License</a> for more information.<br />
  604. <br />
  605. The Python Software Foundation is a non-profit corporation.
  606. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  607. <br />
  608. <br />
  609. Last updated on Apr 09, 2024 (13:47 UTC).
  610. <a href="/bugs.html">Found a bug</a>?
  611. <br />
  612. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  613. </div>
  614. </body>
  615. </html>
上海开阖软件有限公司 沪ICP备12045867号-1