Team Ai
Apppublic

parthtamu/rag-code-assistant

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
asyncio-subprocess.html716 linesDownload Raw Back to docs
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4  <head>5    <meta charset="utf-8" />6    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="Subprocesses" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/asyncio-subprocess.html" />10<meta property="og:site_name" content="Python documentation" />11<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..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_asyncio-subprocess_a9c60f48.png" />15<meta property="og:image:alt" 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..." />16<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..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20    <title>Subprocesses &#8212; Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21    22    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23    <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24    <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25    <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26    <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27    28    <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29    <script src="../_static/doctools.js?v=9bcbadda"></script>30    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31    <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32    33    <script src="../_static/sidebar.js"></script>34    35    <link rel="search" type="application/opensearchdescription+xml"36          title="Search within Python 3.15.0a6 documentation"37          href="../_static/opensearch.xml"/>38    <link rel="author" title="About these documents" href="../about.html" />39    <link rel="index" title="Index" href="../genindex.html" />40    <link rel="search" title="Search" href="../search.html" />41    <link rel="copyright" title="Copyright" href="../copyright.html" />42    <link rel="next" title="Queues" href="asyncio-queue.html" />43    <link rel="prev" title="Synchronization Primitives" href="asyncio-sync.html" />44    45      46      <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47      48      <link rel="canonical" href="https://docs.python.org/3/library/asyncio-subprocess.html">49      50    51 52    53    <style>54      @media only screen {55        table.full-width-table {56            width: 100%;57        }58      }59    </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61    <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62            <script type="text/javascript" src="../_static/copybutton.js"></script>63            <script type="text/javascript" src="../_static/menu.js"></script>64            <script type="text/javascript" src="../_static/search-focus.js"></script>65            <script type="text/javascript" src="../_static/themetoggle.js"></script> 66            <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67            <meta name="readthedocs-addons-api-version" content="1">68 69  </head>70<body>71<div class="mobile-nav">72    <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73           aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74    <nav class="nav-content" role="navigation">75        <label for="menuToggler" class="toggler__label">76            <span></span>77        </label>78        <span class="nav-items-wrapper">79            <a href="https://www.python.org/" class="nav-logo">80                <img src="../_static/py.svg" alt="Python logo">81            </a>82            <span class="version_switcher_placeholder"></span>83            <form role="search" class="search" action="../search.html" method="get">84                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85                    <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>86                </svg>87                <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88                <input type="submit" value="Go">89            </form>90        </span>91    </nav>92    <div class="menu-wrapper">93        <nav class="menu" role="navigation" aria-label="main navigation">94            <div class="language_switcher_placeholder"></div>95            96<label class="theme-selector-label">97    Theme98    <select class="theme-selector" oninput="activateTheme(this.value)">99        <option value="auto" selected>Auto</option>100        <option value="light">Light</option>101        <option value="dark">Dark</option>102    </select>103</label>104  <div>105    <h3><a href="../contents.html">Table of Contents</a></h3>106    <ul>107<li><a class="reference internal" href="#">Subprocesses</a><ul>108<li><a class="reference internal" href="#creating-subprocesses">Creating Subprocesses</a></li>109<li><a class="reference internal" href="#constants">Constants</a></li>110<li><a class="reference internal" href="#interacting-with-subprocesses">Interacting with Subprocesses</a><ul>111<li><a class="reference internal" href="#subprocess-and-threads">Subprocess and Threads</a></li>112<li><a class="reference internal" href="#examples">Examples</a></li>113</ul>114</li>115</ul>116</li>117</ul>118 119  </div>120  <div>121    <h4>Previous topic</h4>122    <p class="topless"><a href="asyncio-sync.html"123                          title="previous chapter">Synchronization Primitives</a></p>124  </div>125  <div>126    <h4>Next topic</h4>127    <p class="topless"><a href="asyncio-queue.html"128                          title="next chapter">Queues</a></p>129  </div>130  <script>131    document.addEventListener('DOMContentLoaded', () => {132        const title = document.querySelector('meta[property="og:title"]').content;133        const elements = document.querySelectorAll('.improvepage');134        const pageurl = window.location.href.split('?')[0];135        elements.forEach(element => {136            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));137            url.searchParams.set('pagetitle', title);138            url.searchParams.set('pageurl', pageurl);139            url.searchParams.set('pagesource', "library/asyncio-subprocess.rst");140            element.href = url.toString();141        });142    });143  </script>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><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>149      <li>150        <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio-subprocess.rst?plain=1"151            rel="nofollow">Show source152        </a>153      </li>154      155    </ul>156  </div>157        </nav>158    </div>159</div>160 161  162    <div class="related" role="navigation" aria-label="Related">163      <h3>Navigation</h3>164      <ul>165        <li class="right" style="margin-right: 10px">166          <a href="../genindex.html" title="General Index"167             accesskey="I">index</a></li>168        <li class="right" >169          <a href="../py-modindex.html" title="Python Module Index"170             >modules</a> |</li>171        <li class="right" >172          <a href="asyncio-queue.html" title="Queues"173             accesskey="N">next</a> |</li>174        <li class="right" >175          <a href="asyncio-sync.html" title="Synchronization Primitives"176             accesskey="P">previous</a> |</li>177 178          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>179          <li><a href="https://www.python.org/">Python</a> &#187;</li>180          <li class="switchers">181            <div class="language_switcher_placeholder"></div>182            <div class="version_switcher_placeholder"></div>183          </li>184          <li>185              186          </li>187    <li id="cpython-language-and-version">188      <a href="../index.html">3.15.0a6 Documentation</a> &#187;189    </li>190 191          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>192          <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>193          <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>194        <li class="nav-item nav-item-this"><a href="">Subprocesses</a></li>195                <li class="right">196                    197 198    <div class="inline-search" role="search">199        <form class="inline-search" action="../search.html" method="get">200          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">201          <input type="submit" value="Go">202        </form>203    </div>204                     |205                </li>206            <li class="right">207<label class="theme-selector-label">208    Theme209    <select class="theme-selector" oninput="activateTheme(this.value)">210        <option value="auto" selected>Auto</option>211        <option value="light">Light</option>212        <option value="dark">Dark</option>213    </select>214</label> |</li>215            216      </ul>217    </div>    218 219    <div class="document">220      <div class="documentwrapper">221        <div class="bodywrapper">222          <div class="body" role="main">223            224  <section id="subprocesses">225<span id="asyncio-subprocess"></span><h1>Subprocesses<a class="headerlink" href="#subprocesses" title="Link to this heading">¶</a></h1>226<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/asyncio/subprocess.py">Lib/asyncio/subprocess.py</a>,227<a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/asyncio/base_subprocess.py">Lib/asyncio/base_subprocess.py</a></p>228<hr class="docutils" />229<p>This section describes high-level async/await asyncio APIs to230create and manage subprocesses.</p>231<p id="asyncio-example-subprocess-shell">Here’s an example of how asyncio can run a shell command and232obtain its result:</p>233<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">asyncio</span>234 235<span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">run</span><span class="p">(</span><span class="n">cmd</span><span class="p">):</span>236    <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>237        <span class="n">cmd</span><span class="p">,</span>238        <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>239        <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>240 241    <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>242 243    <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>244    <span class="k">if</span> <span class="n">stdout</span><span class="p">:</span>245        <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>246    <span class="k">if</span> <span class="n">stderr</span><span class="p">:</span>247        <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>248 249<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>250</pre></div>251</div>252<p>will print:</p>253<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>254<span class="p">[</span><span class="n">stderr</span><span class="p">]</span>255<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>256</pre></div>257</div>258<p>Because all asyncio subprocess functions are asynchronous and asyncio259provides many tools to work with such functions, it is easy to execute260and monitor multiple subprocesses in parallel.  It is indeed trivial261to modify the above example to run several commands simultaneously:</p>262<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">main</span><span class="p">():</span>263    <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">gather</span><span class="p">(</span>264        <span class="n">run</span><span class="p">(</span><span class="s1">&#39;ls /zzz&#39;</span><span class="p">),</span>265        <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>266 267<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>268</pre></div>269</div>270<p>See also the <a class="reference internal" href="#examples">Examples</a> subsection.</p>271<section id="creating-subprocesses">272<h2>Creating Subprocesses<a class="headerlink" href="#creating-subprocesses" title="Link to this heading">¶</a></h2>273<dl class="py function">274<dt class="sig sig-object py" id="asyncio.create_subprocess_exec">275<em class="property"><span class="k"><span class="pre">async</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">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>276<dd><p>Create a subprocess.</p>277<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>278wrappers for <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">stdout</span></code></a> and <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">stderr</span></code></a>279(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>280<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>281<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 other282parameters.</p>283<p>If the process object is garbage collected while the process is still284running, the child process will be killed.</p>285<div class="versionchanged">286<p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p>287</div>288</dd></dl>289 290<dl class="py function">291<dt class="sig sig-object py" id="asyncio.create_subprocess_shell">292<em class="property"><span class="k"><span class="pre">async</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">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>293<dd><p>Run the <em>cmd</em> shell command.</p>294<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>295wrappers for <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">stdout</span></code></a> and <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">stderr</span></code></a>296(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>297<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>298<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 other299parameters.</p>300<p>If the process object is garbage collected while the process is still301running, the child process will be killed.</p>302<div class="admonition important">303<p class="admonition-title">Important</p>304<p>It is the application’s responsibility to ensure that all whitespace and305special characters are quoted appropriately to avoid <a class="reference external" href="https://en.wikipedia.org/wiki/Shell_injection#Shell_injection">shell injection</a>306vulnerabilities. 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 properly307escape whitespace and special shell characters in strings that are going308to be used to construct shell commands.</p>309</div>310<div class="versionchanged">311<p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p>312</div>313</dd></dl>314 315<div class="admonition note">316<p class="admonition-title">Note</p>317<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> is318used. See <a class="reference internal" href="asyncio-platforms.html#asyncio-windows-subprocess"><span class="std std-ref">Subprocess Support on Windows</span></a>319for details.</p>320</div>321<div class="admonition seealso">322<p class="admonition-title">See also</p>323<p>asyncio also has the following <em>low-level</em> APIs to work with subprocesses:324<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>,325<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>,326as well as the <a class="reference internal" href="asyncio-protocol.html#asyncio-subprocess-transports"><span class="std std-ref">Subprocess Transports</span></a>327and <a class="reference internal" href="asyncio-protocol.html#asyncio-subprocess-protocols"><span class="std std-ref">Subprocess Protocols</span></a>.</p>328</div>329</section>330<section id="constants">331<h2>Constants<a class="headerlink" href="#constants" title="Link to this heading">¶</a></h2>332<dl class="py data">333<dt class="sig sig-object py" id="asyncio.subprocess.PIPE">334<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>335<dd><p>Can be passed to the <em>stdin</em>, <em>stdout</em> or <em>stderr</em> parameters.</p>336<p>If <em>PIPE</em> is passed to <em>stdin</em> argument, the337<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> attribute338will point to a <a class="reference internal" href="asyncio-stream.html#asyncio.StreamWriter" title="asyncio.StreamWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code></a> instance.</p>339<p>If <em>PIPE</em> is passed to <em>stdout</em> or <em>stderr</em> arguments, the340<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> and341<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>342attributes will point to <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> instances.</p>343</dd></dl>344 345<dl class="py data">346<dt class="sig sig-object py" id="asyncio.subprocess.STDOUT">347<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>348<dd><p>Special value that can be used as the <em>stderr</em> argument and indicates349that standard error should be redirected into standard output.</p>350</dd></dl>351 352<dl class="py data">353<dt class="sig sig-object py" id="asyncio.subprocess.DEVNULL">354<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>355<dd><p>Special value that can be used as the <em>stdin</em>, <em>stdout</em> or <em>stderr</em> argument356to process creation functions.  It indicates that the special file357<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>358</dd></dl>359 360</section>361<section id="interacting-with-subprocesses">362<h2>Interacting with Subprocesses<a class="headerlink" href="#interacting-with-subprocesses" title="Link to this heading">¶</a></h2>363<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>364functions return instances of the <em>Process</em> class.  <em>Process</em> is a high-level365wrapper that allows communicating with subprocesses and watching for366their completion.</p>367<dl class="py class">368<dt class="sig sig-object py" id="asyncio.subprocess.Process">369<em class="property"><span class="k"><span class="pre">class</span></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>370<dd><p>An object that wraps OS processes created by the371<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>372functions.</p>373<p>This class is designed to have a similar API to the374<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 some375notable differences:</p>376<ul class="simple">377<li><p>unlike Popen, Process instances do not have an equivalent to378the <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>379<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> and380<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 a381<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>382<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> method383is 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> method384is implemented as a blocking busy loop;</p></li>385<li><p>the <em>universal_newlines</em> parameter is not supported.</p></li>386</ul>387<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>388<p>See also the <a class="reference internal" href="#asyncio-subprocess-threads"><span class="std std-ref">Subprocess and Threads</span></a>389section.</p>390<dl class="py method">391<dt class="sig sig-object py" id="asyncio.subprocess.Process.wait">392<em class="property"><span class="k"><span class="pre">async</span></span><span class="w"> </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>393<dd><p>Wait for the child process to terminate.</p>394<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>395<div class="admonition note">396<p class="admonition-title">Note</p>397<p>This method can deadlock when using <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code> or398<code class="docutils literal notranslate"><span class="pre">stderr=PIPE</span></code> and the child process generates so much output399that it blocks waiting for the OS pipe buffer to accept400more 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 pipes401to avoid this condition.</p>402</div>403</dd></dl>404 405<dl class="py method">406<dt class="sig sig-object py" id="asyncio.subprocess.Process.communicate">407<em class="property"><span class="k"><span class="pre">async</span></span><span class="w"> </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>408<dd><p>Interact with process:</p>409<ol class="arabic simple">410<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>411<li><p>closes <em>stdin</em>;</p></li>412<li><p>read data from <em>stdout</em> and <em>stderr</em>, until EOF is reached;</p></li>413<li><p>wait for process to terminate.</p></li>414</ol>415<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)416that will be sent to the child process.</p>417<p>Return a tuple <code class="docutils literal notranslate"><span class="pre">(stdout_data,</span> <span class="pre">stderr_data)</span></code>.</p>418<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>419exception is raised when writing <em>input</em> into <em>stdin</em>, the420exception is ignored.  This condition occurs when the process421exits before all data are written into <em>stdin</em>.</p>422<p>If it is desired to send data to the process’ <em>stdin</em>,423the process needs to be created with <code class="docutils literal notranslate"><span class="pre">stdin=PIPE</span></code>.  Similarly,424to get anything other than <code class="docutils literal notranslate"><span class="pre">None</span></code> in the result tuple, the425process has to be created with <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code> and/or426<code class="docutils literal notranslate"><span class="pre">stderr=PIPE</span></code> arguments.</p>427<p>Note, that the data read is buffered in memory, so do not use428this method if the data size is large or unlimited.</p>429<div class="versionchanged">430<p><span class="versionmodified changed">Changed in version 3.12: </span><em>stdin</em> gets closed when <code class="docutils literal notranslate"><span class="pre">input=None</span></code> too.</p>431</div>432</dd></dl>433 434<dl class="py method">435<dt class="sig sig-object py" id="asyncio.subprocess.Process.send_signal">436<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>437<dd><p>Sends the signal <em>signal</em> to the child process.</p>438<div class="admonition note">439<p class="admonition-title">Note</p>440<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>.441<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 processes442started with a <em>creationflags</em> parameter which includes443<code class="docutils literal notranslate"><span class="pre">CREATE_NEW_PROCESS_GROUP</span></code>.</p>444</div>445</dd></dl>446 447<dl class="py method">448<dt class="sig sig-object py" id="asyncio.subprocess.Process.terminate">449<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>450<dd><p>Stop the child process.</p>451<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 the452child process.</p>453<p>On Windows the Win32 API function <code class="xref c c-func docutils literal notranslate"><span class="pre">TerminateProcess()</span></code> is454called to stop the child process.</p>455</dd></dl>456 457<dl class="py method">458<dt class="sig sig-object py" id="asyncio.subprocess.Process.kill">459<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>460<dd><p>Kill the child process.</p>461<p>On POSIX systems this method sends <a class="reference internal" href="signal.html#signal.SIGKILL" title="signal.SIGKILL"><code class="xref py py-data docutils literal notranslate"><span class="pre">SIGKILL</span></code></a> to the child462process.</p>463<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>464</dd></dl>465 466<dl class="py attribute">467<dt class="sig sig-object py" id="asyncio.subprocess.Process.stdin">468<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>469<dd><p>Standard input stream (<a class="reference internal" href="asyncio-stream.html#asyncio.StreamWriter" title="asyncio.StreamWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code></a>) or <code class="docutils literal notranslate"><span class="pre">None</span></code>470if the process was created with <code class="docutils literal notranslate"><span class="pre">stdin=None</span></code>.</p>471</dd></dl>472 473<dl class="py attribute">474<dt class="sig sig-object py" id="asyncio.subprocess.Process.stdout">475<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>476<dd><p>Standard output stream (<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>) or <code class="docutils literal notranslate"><span class="pre">None</span></code>477if the process was created with <code class="docutils literal notranslate"><span class="pre">stdout=None</span></code>.</p>478</dd></dl>479 480<dl class="py attribute">481<dt class="sig sig-object py" id="asyncio.subprocess.Process.stderr">482<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>483<dd><p>Standard error stream (<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>) or <code class="docutils literal notranslate"><span class="pre">None</span></code>484if the process was created with <code class="docutils literal notranslate"><span class="pre">stderr=None</span></code>.</p>485</dd></dl>486 487<div class="admonition warning">488<p class="admonition-title">Warning</p>489<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 than490<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>,491<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> or492<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>.493This avoids deadlocks due to streams pausing reading or writing494and blocking the child process.</p>495</div>496<dl class="py attribute">497<dt class="sig sig-object py" id="asyncio.subprocess.Process.pid">498<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>499<dd><p>Process identification number (PID).</p>500<p>Note that for processes created by the <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>501function, this attribute is the PID of the spawned shell.</p>502</dd></dl>503 504<dl class="py attribute">505<dt class="sig sig-object py" id="asyncio.subprocess.Process.returncode">506<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>507<dd><p>Return code of the process when it exits.</p>508<p>A <code class="docutils literal notranslate"><span class="pre">None</span></code> value indicates that the process has not terminated yet.</p>509<p>A negative value <code class="docutils literal notranslate"><span class="pre">-N</span></code> indicates that the child was terminated510by signal <code class="docutils literal notranslate"><span class="pre">N</span></code> (POSIX only).</p>511</dd></dl>512 513</dd></dl>514 515<section id="subprocess-and-threads">516<span id="asyncio-subprocess-threads"></span><h3>Subprocess and Threads<a class="headerlink" href="#subprocess-and-threads" title="Link to this heading">¶</a></h3>517<p>Standard asyncio event loop supports running subprocesses from different threads by518default.</p>519<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),520<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>521<p>Note that alternative event loop implementations might have own limitations;522please refer to their documentation.</p>523<div class="admonition seealso">524<p class="admonition-title">See also</p>525<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>526</div>527</section>528<section id="examples">529<h3>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h3>530<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 to531control 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 from532its standard output.</p>533<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>534function:</p>535<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">asyncio</span>536<span class="kn">import</span><span class="w"> </span><span class="nn">sys</span>537 538<span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">get_date</span><span class="p">():</span>539    <span class="n">code</span> <span class="o">=</span> <span class="s1">&#39;import datetime; print(datetime.datetime.now())&#39;</span>540 541    <span class="c1"># Create the subprocess; redirect the standard output</span>542    <span class="c1"># into a pipe.</span>543    <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>544        <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>545        <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>546 547    <span class="c1"># Read one line of output.</span>548    <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>549    <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>550 551    <span class="c1"># Wait for the subprocess exit.</span>552    <span class="k">await</span> <span class="n">proc</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>553    <span class="k">return</span> <span class="n">line</span>554 555<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>556<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>557</pre></div>558</div>559<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>560written using low-level APIs.</p>561</section>562</section>563</section>564 565 566            <div class="clearer"></div>567          </div>568        </div>569      </div>570      <div class="sphinxsidebar" role="navigation" aria-label="Main">571        <div class="sphinxsidebarwrapper">572  <div>573    <h3><a href="../contents.html">Table of Contents</a></h3>574    <ul>575<li><a class="reference internal" href="#">Subprocesses</a><ul>576<li><a class="reference internal" href="#creating-subprocesses">Creating Subprocesses</a></li>577<li><a class="reference internal" href="#constants">Constants</a></li>578<li><a class="reference internal" href="#interacting-with-subprocesses">Interacting with Subprocesses</a><ul>579<li><a class="reference internal" href="#subprocess-and-threads">Subprocess and Threads</a></li>580<li><a class="reference internal" href="#examples">Examples</a></li>581</ul>582</li>583</ul>584</li>585</ul>586 587  </div>588  <div>589    <h4>Previous topic</h4>590    <p class="topless"><a href="asyncio-sync.html"591                          title="previous chapter">Synchronization Primitives</a></p>592  </div>593  <div>594    <h4>Next topic</h4>595    <p class="topless"><a href="asyncio-queue.html"596                          title="next chapter">Queues</a></p>597  </div>598  <script>599    document.addEventListener('DOMContentLoaded', () => {600        const title = document.querySelector('meta[property="og:title"]').content;601        const elements = document.querySelectorAll('.improvepage');602        const pageurl = window.location.href.split('?')[0];603        elements.forEach(element => {604            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));605            url.searchParams.set('pagetitle', title);606            url.searchParams.set('pageurl', pageurl);607            url.searchParams.set('pagesource', "library/asyncio-subprocess.rst");608            element.href = url.toString();609        });610    });611  </script>612  <div role="note" aria-label="source link">613    <h3>This page</h3>614    <ul class="this-page-menu">615      <li><a href="../bugs.html">Report a bug</a></li>616      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>617      <li>618        <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio-subprocess.rst?plain=1"619            rel="nofollow">Show source620        </a>621      </li>622      623    </ul>624  </div>625        </div>626<div id="sidebarbutton" title="Collapse sidebar">627<span>«</span>628</div>629 630      </div>631      <div class="clearer"></div>632    </div>  633    <div class="related" role="navigation" aria-label="Related">634      <h3>Navigation</h3>635      <ul>636        <li class="right" style="margin-right: 10px">637          <a href="../genindex.html" title="General Index"638             >index</a></li>639        <li class="right" >640          <a href="../py-modindex.html" title="Python Module Index"641             >modules</a> |</li>642        <li class="right" >643          <a href="asyncio-queue.html" title="Queues"644             >next</a> |</li>645        <li class="right" >646          <a href="asyncio-sync.html" title="Synchronization Primitives"647             >previous</a> |</li>648 649          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>650          <li><a href="https://www.python.org/">Python</a> &#187;</li>651          <li class="switchers">652            <div class="language_switcher_placeholder"></div>653            <div class="version_switcher_placeholder"></div>654          </li>655          <li>656              657          </li>658    <li id="cpython-language-and-version">659      <a href="../index.html">3.15.0a6 Documentation</a> &#187;660    </li>661 662          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>663          <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>664          <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>665        <li class="nav-item nav-item-this"><a href="">Subprocesses</a></li>666                <li class="right">667                    668 669    <div class="inline-search" role="search">670        <form class="inline-search" action="../search.html" method="get">671          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">672          <input type="submit" value="Go">673        </form>674    </div>675                     |676                </li>677            <li class="right">678<label class="theme-selector-label">679    Theme680    <select class="theme-selector" oninput="activateTheme(this.value)">681        <option value="auto" selected>Auto</option>682        <option value="light">Light</option>683        <option value="dark">Dark</option>684    </select>685</label> |</li>686            687      </ul>688    </div>  689    <div class="footer">690    &copy; <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.691    <br>692    This page is licensed under the Python Software Foundation License Version 2.693    <br>694    Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.695    <br>696    697      See <a href="/license.html">History and License</a> for more information.<br>698    699    700    <br>701 702    The Python Software Foundation is a non-profit corporation.703<a href="https://www.python.org/psf/donations/">Please donate.</a>704<br>705    <br>706      Last updated on Mar 10, 2026 (08:58 UTC).707    708      <a href="/bugs.html">Found a bug</a>?709    710    <br>711 712    Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.713    </div>714 715  </body>716</html>