Files
hustoj/web/python-docs/library/asyncio-subprocess.html
2025-03-09 17:10:56 +08:00

539 lines
47 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>子进程集 &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="队列集" href="asyncio-queue.html" />
<link rel="prev" title="同步原语" href="asyncio-sync.html" />
<link rel="canonical" href="https://docs.python.org/3/library/asyncio-subprocess.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<nav class="nav-content" role="navigation">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<div class="version_switcher_placeholder"></div>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero"
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" fill="#444"></path>
</svg>
<input type="text" name="q" aria-label="快速搜索"/>
<input type="submit" value="转向"/>
</form>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">子进程集</a><ul>
<li><a class="reference internal" href="#creating-subprocesses">创建子进程</a></li>
<li><a class="reference internal" href="#constants">常量</a></li>
<li><a class="reference internal" href="#interacting-with-subprocesses">与子进程交互</a><ul>
<li><a class="reference internal" href="#subprocess-and-threads">子进程和线程</a></li>
<li><a class="reference internal" href="#examples">例子</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="asyncio-sync.html"
title="上一章">同步原语</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="asyncio-queue.html"
title="下一章">队列集</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/asyncio-subprocess.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
这个文档所针对的是一个已不再受支持的 Python 旧版本。
你应当升级版本,并阅读
<a href="/3/library/asyncio-subprocess.html"> Python 当前稳定版本的文档</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="asyncio-queue.html" title="队列集"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="asyncio-sync.html" title="同步原语"
accesskey="P">上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="ipc.html" >网络和进程间通信</a> &#187;</li>
<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> --- 异步 I/O</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="subprocesses">
<span id="asyncio-subprocess"></span><h1>子进程集<a class="headerlink" href="#subprocesses" title="永久链接至标题"></a></h1>
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/asyncio/subprocess.py">Lib/asyncio/subprocess.py</a>, <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/asyncio/base_subprocess.py">Lib/asyncio/base_subprocess.py</a></p>
<hr class="docutils" />
<p>本节介绍了用于创建和管理子进程的高层级 async/await asyncio API。</p>
<p id="asyncio-example-subprocess-shell">下面的例子演示了如何用 asyncio 运行一个 shell 命令并获取其结果:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
<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>
<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>
<span class="n">cmd</span><span class="p">,</span>
<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>
<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>
<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>
<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>
<span class="k">if</span> <span class="n">stdout</span><span class="p">:</span>
<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>
<span class="k">if</span> <span class="n">stderr</span><span class="p">:</span>
<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>
<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>
</pre></div>
</div>
<p>将打印:</p>
<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>
<span class="p">[</span><span class="n">stderr</span><span class="p">]</span>
<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>
</pre></div>
</div>
<p>由于所有 asyncio 子进程函数都是异步的并且 asyncio 提供了许多工具用来配合这些函数使用,因此并行地执行和监视多个子进程十分容易。 要修改上面的例子来同时运行多个命令确实是非常简单的:</p>
<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>
<span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">gather</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>
<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>
<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>
</pre></div>
</div>
<p>另请参阅 <a class="reference internal" href="#examples">Examples</a> 小节。</p>
<section id="creating-subprocesses">
<h2>创建子进程<a class="headerlink" href="#creating-subprocesses" title="永久链接至标题"></a></h2>
<dl class="function">
<dt id="asyncio.create_subprocess_exec">
<em class="property">coroutine </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">create_subprocess_exec</code><span class="sig-paren">(</span><em class="sig-param">program</em>, <em class="sig-param">*args</em>, <em class="sig-param">stdin=None</em>, <em class="sig-param">stdout=None</em>, <em class="sig-param">stderr=None</em>, <em class="sig-param">loop=None</em>, <em class="sig-param">limit=None</em>, <em class="sig-param">**kwds</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.create_subprocess_exec" title="永久链接至目标"></a></dt>
<dd><p>创建一个子进程。</p>
<p><em>limit</em> 参数为 <code class="xref py py-attr docutils literal notranslate"><span class="pre">Process.stdout</span></code><code class="xref py py-attr docutils literal notranslate"><span class="pre">Process.stderr</span></code> 设置 <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> 包装器的缓冲区上限(如果将 <a class="reference internal" href="subprocess.html#subprocess.PIPE" title="subprocess.PIPE"><code class="xref py py-attr docutils literal notranslate"><span class="pre">subprocess.PIPE</span></code></a> 传给了 <em>stdout</em><em>stderr</em> 参数)。</p>
<p>返回一个 <a class="reference internal" href="#asyncio.asyncio.subprocess.Process" title="asyncio.asyncio.subprocess.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> 实例。</p>
<p>有关其他形参的说明请查阅 <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> 的文档。</p>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.8, will be removed in version 3.10: </span><em>loop</em> 形参。</p>
</div>
</dd></dl>
<dl class="function">
<dt id="asyncio.create_subprocess_shell">
<em class="property">coroutine </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">create_subprocess_shell</code><span class="sig-paren">(</span><em class="sig-param">cmd</em>, <em class="sig-param">stdin=None</em>, <em class="sig-param">stdout=None</em>, <em class="sig-param">stderr=None</em>, <em class="sig-param">loop=None</em>, <em class="sig-param">limit=None</em>, <em class="sig-param">**kwds</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.create_subprocess_shell" title="永久链接至目标"></a></dt>
<dd><p>运行 <em>cmd</em> shell 命令。</p>
<p><em>limit</em> 参数为 <code class="xref py py-attr docutils literal notranslate"><span class="pre">Process.stdout</span></code><code class="xref py py-attr docutils literal notranslate"><span class="pre">Process.stderr</span></code> 设置 <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> 包装器的缓冲区上限(如果将 <a class="reference internal" href="subprocess.html#subprocess.PIPE" title="subprocess.PIPE"><code class="xref py py-attr docutils literal notranslate"><span class="pre">subprocess.PIPE</span></code></a> 传给了 <em>stdout</em><em>stderr</em> 参数)。</p>
<p>返回一个 <a class="reference internal" href="#asyncio.asyncio.subprocess.Process" title="asyncio.asyncio.subprocess.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> 实例。</p>
<p>有关其他形参的说明请查阅 <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> 的文档。</p>
<div class="admonition important">
<p class="admonition-title">重要</p>
<p>应用程序要负责确保正确地转义所有空白字符和特殊字符以防止 <a class="reference external" href="https://en.wikipedia.org/wiki/Shell_injection#Shell_injection">shell 注入</a> 漏洞。 <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> 函数可以被用来正确地转义字符串中可以被用来构造 shell 命令的空白字符和特殊 shell 字符。</p>
</div>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.8, will be removed in version 3.10: </span><em>loop</em> 形参。</p>
</div>
</dd></dl>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>如果使用了 <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> 则子进程将在 Windows 中可用。 详情参见 <a class="reference internal" href="asyncio-platforms.html#asyncio-windows-subprocess"><span class="std std-ref">Windows 上的子进程支持</span></a></p>
</div>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<p>asyncio 还有下列 <em>低层级</em> API 可配合子进程使用: <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>, <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> 以及 <a class="reference internal" href="asyncio-protocol.html#asyncio-subprocess-transports"><span class="std std-ref">子进程传输</span></a><a class="reference internal" href="asyncio-protocol.html#asyncio-subprocess-protocols"><span class="std std-ref">子进程协议</span></a></p>
</div>
</section>
<section id="constants">
<h2>常量<a class="headerlink" href="#constants" title="永久链接至标题"></a></h2>
<dl class="data">
<dt id="asyncio.asyncio.subprocess.PIPE">
<code class="sig-prename descclassname">asyncio.subprocess.</code><code class="sig-name descname">PIPE</code><a class="headerlink" href="#asyncio.asyncio.subprocess.PIPE" title="永久链接至目标"></a></dt>
<dd><p>可以被传递给 <em>stdin</em>, <em>stdout</em><em>stderr</em> 形参。</p>
<p>如果 <em>PIPE</em> 被传递给 <em>stdin</em> 参数,则 <a class="reference internal" href="#asyncio.asyncio.subprocess.Process.stdin" title="asyncio.asyncio.subprocess.Process.stdin"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Process.stdin</span></code></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> 实例。</p>
<p>如果 <em>PIPE</em> 被传递给 <em>stdout</em><em>stderr</em> 参数,则 <a class="reference internal" href="#asyncio.asyncio.subprocess.Process.stdout" title="asyncio.asyncio.subprocess.Process.stdout"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Process.stdout</span></code></a><a class="reference internal" href="#asyncio.asyncio.subprocess.Process.stderr" title="asyncio.asyncio.subprocess.Process.stderr"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Process.stderr</span></code></a> 属性将会指向 <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> 实例。</p>
</dd></dl>
<dl class="data">
<dt id="asyncio.asyncio.subprocess.STDOUT">
<code class="sig-prename descclassname">asyncio.subprocess.</code><code class="sig-name descname">STDOUT</code><a class="headerlink" href="#asyncio.asyncio.subprocess.STDOUT" title="永久链接至目标"></a></dt>
<dd><p>可以用作 <em>stderr</em> 参数的特殊值,表示标准错误应当被重定向到标准输出。</p>
</dd></dl>
<dl class="data">
<dt id="asyncio.asyncio.subprocess.DEVNULL">
<code class="sig-prename descclassname">asyncio.subprocess.</code><code class="sig-name descname">DEVNULL</code><a class="headerlink" href="#asyncio.asyncio.subprocess.DEVNULL" title="永久链接至目标"></a></dt>
<dd><p>可以用作 <em>stdin</em>, <em>stdout</em><em>stderr</em> 参数来处理创建函数的特殊值。 它表示将为相应的子进程流使用特殊文件 <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></p>
</dd></dl>
</section>
<section id="interacting-with-subprocesses">
<h2>与子进程交互<a class="headerlink" href="#interacting-with-subprocesses" title="永久链接至标题"></a></h2>
<p><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><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> 函数都返回 <em>Process</em> 类的实例。 <em>Process</em> 是一个高层级包装器,它允许与子进程通信并监视其完成情况。</p>
<dl class="class">
<dt id="asyncio.asyncio.subprocess.Process">
<em class="property">class </em><code class="sig-prename descclassname">asyncio.subprocess.</code><code class="sig-name descname">Process</code><a class="headerlink" href="#asyncio.asyncio.subprocess.Process" title="永久链接至目标"></a></dt>
<dd><p>一个用于包装 <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> 函数创建的 OS 进程的对象。</p>
<p>这个类被设计为具有与 <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> 类相似的 API但两者有一些重要的差异:</p>
<ul class="simple">
<li><p>不同于 PopenProcess 实例没有与 <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> 方法等价的方法;</p></li>
<li><p><a class="reference internal" href="#asyncio.asyncio.subprocess.Process.communicate" title="asyncio.asyncio.subprocess.Process.communicate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">communicate()</span></code></a><a class="reference internal" href="#asyncio.asyncio.subprocess.Process.wait" title="asyncio.asyncio.subprocess.Process.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> 方法没有 <em>timeout</em> 形参;要使用 <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> 函数;</p></li>
<li><p><a class="reference internal" href="#asyncio.asyncio.subprocess.Process.wait" title="asyncio.asyncio.subprocess.Process.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Process.wait()</span></code></a> 方法是异步的,而 <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> 方法则被实现为阻塞型忙循环;</p></li>
<li><p><em>universal_newlines</em> 形参不被支持。</p></li>
</ul>
<p>这个类不是线程安全的(<a class="reference internal" href="asyncio-dev.html#asyncio-multithreading"><span class="std std-ref">not thread safe</span></a>)。</p>
<p>请参阅 <a class="reference internal" href="#asyncio-subprocess-threads"><span class="std std-ref">子进程和线程</span></a> 部分。</p>
<dl class="method">
<dt id="asyncio.asyncio.subprocess.Process.wait">
<em class="property">coroutine </em><code class="sig-name descname">wait</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.asyncio.subprocess.Process.wait" title="永久链接至目标"></a></dt>
<dd><p>等待子进程终结。</p>
<p>设置并返回 <a class="reference internal" href="#asyncio.asyncio.subprocess.Process.returncode" title="asyncio.asyncio.subprocess.Process.returncode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">returncode</span></code></a> 属性。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>当使用 <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code><code class="docutils literal notranslate"><span class="pre">stderr=PIPE</span></code> 并且子进程产生了足以阻塞 OS 管道缓冲区等待接收更多的数据的输出时,此方法会发生死锁。 当使用管道时请使用 <a class="reference internal" href="#asyncio.asyncio.subprocess.Process.communicate" title="asyncio.asyncio.subprocess.Process.communicate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">communicate()</span></code></a> 方法来避免这种情况。</p>
</div>
</dd></dl>
<dl class="method">
<dt id="asyncio.asyncio.subprocess.Process.communicate">
<em class="property">coroutine </em><code class="sig-name descname">communicate</code><span class="sig-paren">(</span><em class="sig-param">input=None</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.asyncio.subprocess.Process.communicate" title="永久链接至目标"></a></dt>
<dd><p>与进程交互:</p>
<ol class="arabic simple">
<li><p>发送数据到 <em>stdin</em> (如果 <em>input</em> 不为 <code class="docutils literal notranslate"><span class="pre">None</span></code>)</p></li>
<li><p><em>stdout</em><em>stderr</em> 读取数据,直至到达 EOF</p></li>
<li><p>等待进程终结。</p></li>
</ol>
<p>可选的 <em>input</em> 参数为将被发送到子进程的数据 (<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> 对象)。</p>
<p>返回一个元组 <code class="docutils literal notranslate"><span class="pre">(stdout_data,</span> <span class="pre">stderr_data)</span></code></p>
<p>如果在将 <em>input</em> 写入到 <em>stdin</em> 时引发了 <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><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> 异常,异常会被忽略。 此条件会在进程先于所有数据被写入到 <em>stdin</em> 之前退出时发生。</p>
<p>如果想要将数据发送到进程的 <em>stdin</em>,则创建进程时必须使用 <code class="docutils literal notranslate"><span class="pre">stdin=PIPE</span></code>。 类似地,要在结果元组中获得任何不为 <code class="docutils literal notranslate"><span class="pre">None</span></code> 的值,则创建进程时必须使用 <code class="docutils literal notranslate"><span class="pre">stdout=PIPE</span></code> 和/或 <code class="docutils literal notranslate"><span class="pre">stderr=PIPE</span></code> 参数。</p>
<p>注意,数据读取在内存中是带缓冲的,因此如果数据量过大或不受则不要使用此方法。</p>
</dd></dl>
<dl class="method">
<dt id="asyncio.asyncio.subprocess.Process.send_signal">
<code class="sig-name descname">send_signal</code><span class="sig-paren">(</span><em class="sig-param">signal</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.asyncio.subprocess.Process.send_signal" title="永久链接至目标"></a></dt>
<dd><p>将信号 <em>signal</em> 发送给子进程。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>在 Windows 上,<code class="xref py py-data docutils literal notranslate"><span class="pre">SIGTERM</span></code><a class="reference internal" href="#asyncio.asyncio.subprocess.Process.terminate" title="asyncio.asyncio.subprocess.Process.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">terminate()</span></code></a> 的别名。 <code class="docutils literal notranslate"><span class="pre">CTRL_C_EVENT</span></code><code class="docutils literal notranslate"><span class="pre">CTRL_BREAK_EVENT</span></code> 可被发送给创建时附带 <em>creationflags</em> 形参且其中包括 <code class="docutils literal notranslate"><span class="pre">CREATE_NEW_PROCESS_GROUP</span></code> 的进程。</p>
</div>
</dd></dl>
<dl class="method">
<dt id="asyncio.asyncio.subprocess.Process.terminate">
<code class="sig-name descname">terminate</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.asyncio.subprocess.Process.terminate" title="永久链接至目标"></a></dt>
<dd><p>停止子进程。</p>
<p>在 POSIX 系统中此方法会发送 <a class="reference internal" href="signal.html#signal.SIGTERM" title="signal.SIGTERM"><code class="xref py py-data docutils literal notranslate"><span class="pre">signal.SIGTERM</span></code></a> 给子进程。</p>
<p>在 Windows 上会调用 Win32 API 函数 <code class="xref c c-func docutils literal notranslate"><span class="pre">TerminateProcess()</span></code> 以停止子进程。</p>
</dd></dl>
<dl class="method">
<dt id="asyncio.asyncio.subprocess.Process.kill">
<code class="sig-name descname">kill</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.asyncio.subprocess.Process.kill" title="永久链接至目标"></a></dt>
<dd><p>杀掉子进程。</p>
<p>在 POSIX 系统中此方法会发送 <code class="xref py py-data docutils literal notranslate"><span class="pre">SIGKILL</span></code> 给子进程。</p>
<p>在 Windows 上此方法是 <a class="reference internal" href="#asyncio.asyncio.subprocess.Process.terminate" title="asyncio.asyncio.subprocess.Process.terminate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">terminate()</span></code></a> 的别名。</p>
</dd></dl>
<dl class="attribute">
<dt id="asyncio.asyncio.subprocess.Process.stdin">
<code class="sig-name descname">stdin</code><a class="headerlink" href="#asyncio.asyncio.subprocess.Process.stdin" title="永久链接至目标"></a></dt>
<dd><p>标准输入流 (<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>) 或者如果进程创建时设置了``stdin=None`` 则为 <code class="docutils literal notranslate"><span class="pre">None</span></code></p>
</dd></dl>
<dl class="attribute">
<dt id="asyncio.asyncio.subprocess.Process.stdout">
<code class="sig-name descname">stdout</code><a class="headerlink" href="#asyncio.asyncio.subprocess.Process.stdout" title="永久链接至目标"></a></dt>
<dd><p>标准输出流 (<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>) 或者如果进程创建时设置了 <code class="docutils literal notranslate"><span class="pre">stdout=None</span></code> 则为 <code class="docutils literal notranslate"><span class="pre">None</span></code></p>
</dd></dl>
<dl class="attribute">
<dt id="asyncio.asyncio.subprocess.Process.stderr">
<code class="sig-name descname">stderr</code><a class="headerlink" href="#asyncio.asyncio.subprocess.Process.stderr" title="永久链接至目标"></a></dt>
<dd><p>标准错误流 (<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>) 或者如果进程创建时设置了 <code class="docutils literal notranslate"><span class="pre">stderr=None</span></code> 则为 <code class="docutils literal notranslate"><span class="pre">None</span></code></p>
</dd></dl>
<div class="admonition warning">
<p class="admonition-title">警告</p>
<p>使用 <a class="reference internal" href="#asyncio.asyncio.subprocess.Process.communicate" title="asyncio.asyncio.subprocess.Process.communicate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">communicate()</span></code></a> 方法而非 <a class="reference internal" href="#asyncio.asyncio.subprocess.Process.stdin" title="asyncio.asyncio.subprocess.Process.stdin"><code class="xref py py-attr docutils literal notranslate"><span class="pre">process.stdin.write()</span></code></a>, <a class="reference internal" href="#asyncio.asyncio.subprocess.Process.stdout" title="asyncio.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><a class="reference internal" href="#asyncio.asyncio.subprocess.Process.stderr" title="asyncio.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>。 这可以避免由于流暂停读取或写入并阻塞子进程而导致的死锁。</p>
</div>
<dl class="attribute">
<dt id="asyncio.asyncio.subprocess.Process.pid">
<code class="sig-name descname">pid</code><a class="headerlink" href="#asyncio.asyncio.subprocess.Process.pid" title="永久链接至目标"></a></dt>
<dd><p>进程标识号PID</p>
<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> 函数所创建的进程,这个属性将是所生成的 shell 的 PID。</p>
</dd></dl>
<dl class="attribute">
<dt id="asyncio.asyncio.subprocess.Process.returncode">
<code class="sig-name descname">returncode</code><a class="headerlink" href="#asyncio.asyncio.subprocess.Process.returncode" title="永久链接至目标"></a></dt>
<dd><p>当进程退出时返回其代号。</p>
<p><code class="docutils literal notranslate"><span class="pre">None</span></code> 值表示进程尚未终止。</p>
<p>一个负值 <code class="docutils literal notranslate"><span class="pre">-N</span></code> 表示子进程被信号 <code class="docutils literal notranslate"><span class="pre">N</span></code> 中断 (仅 POSIX).</p>
</dd></dl>
</dd></dl>
<section id="subprocess-and-threads">
<span id="asyncio-subprocess-threads"></span><h3>子进程和线程<a class="headerlink" href="#subprocess-and-threads" title="永久链接至标题"></a></h3>
<p>标准 asyncio 事件循环默认支持从不同线程中运行子进程。</p>
<p>在 Windows 上子进程(默认)只由 <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> 提供,<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> 没有子进程支持。</p>
<p>在 UNIX 上会使用 <em>child watchers</em> 来让子进程结束等待,详情请参阅 <a class="reference internal" href="asyncio-policy.html#asyncio-watchers"><span class="std std-ref">进程监视器</span></a></p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.8 版更改: </span>UNIX 对于从不同线程中无限制地生成子进程会切换为使用 <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></p>
<p>使用 <em>不活动的</em> 当前子监视器生成子进程将引发 <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>
</div>
<p>请注意其他的事件循环实现可能有其本身的限制;请查看它们各自的文档。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<p><a class="reference internal" href="asyncio-dev.html#asyncio-multithreading"><span class="std std-ref">asyncio 中的并发和多线程</span></a> 章节。</p>
</div>
</section>
<section id="examples">
<h3>例子<a class="headerlink" href="#examples" title="永久链接至标题"></a></h3>
<p>一个使用 <a class="reference internal" href="#asyncio.asyncio.subprocess.Process" title="asyncio.asyncio.subprocess.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> 类来控制子进程并用 <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> 类来从其标准输出读取信息的例子。</p>
<p id="asyncio-example-create-subprocess-exec">这个子进程是由 <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> 函数创建的:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="k">async</span> <span class="k">def</span> <span class="nf">get_date</span><span class="p">():</span>
<span class="n">code</span> <span class="o">=</span> <span class="s1">&#39;import datetime; print(datetime.datetime.now())&#39;</span>
<span class="c1"># Create the subprocess; redirect the standard output</span>
<span class="c1"># into a pipe.</span>
<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>
<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>
<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>
<span class="c1"># Read one line of output.</span>
<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>
<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>
<span class="c1"># Wait for the subprocess exit.</span>
<span class="k">await</span> <span class="n">proc</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
<span class="k">return</span> <span class="n">line</span>
<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>
<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>
</pre></div>
</div>
<p>另请参阅使用低层级 API 编写的 <a class="reference internal" href="asyncio-protocol.html#asyncio-example-subprocess-proto"><span class="std std-ref">相同示例</span></a></p>
</section>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">子进程集</a><ul>
<li><a class="reference internal" href="#creating-subprocesses">创建子进程</a></li>
<li><a class="reference internal" href="#constants">常量</a></li>
<li><a class="reference internal" href="#interacting-with-subprocesses">与子进程交互</a><ul>
<li><a class="reference internal" href="#subprocess-and-threads">子进程和线程</a></li>
<li><a class="reference internal" href="#examples">例子</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="asyncio-sync.html"
title="上一章">同步原语</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="asyncio-queue.html"
title="下一章">队列集</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/asyncio-subprocess.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="asyncio-queue.html" title="队列集"
>下一页</a> |</li>
<li class="right" >
<a href="asyncio-sync.html" title="同步原语"
>上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="ipc.html" >网络和进程间通信</a> &#187;</li>
<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> --- 异步 I/O</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
最后更新于 12月 09, 2024.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</div>
</body>
</html>