python-project/python-3.7.4-docs-html/library/asyncio.html

256 lines
13 KiB
HTML

2019-07-15 11:16:41 +07:00
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>asyncio — Asynchronous I/O &#8212; Python 3.7.4 documentation</title>
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/language_data.js"></script>
<script type="text/javascript" src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Python 3.7.4 documentation"
href="../_static/opensearch.xml"/>
<link rel="author" title="About these documents" href="../about.html" />
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="next" title="Coroutines and Tasks" href="asyncio-task.html" />
<link rel="prev" title="Networking and Interprocess Communication" href="ipc.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
<link rel="canonical" href="https://docs.python.org/3/library/asyncio.html" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/switchers.js"></script>
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="asyncio-task.html" title="Coroutines and Tasks"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="ipc.html" title="Networking and Interprocess Communication"
accesskey="P">previous</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li>
<span class="language_switcher_placeholder">en</span>
<span class="version_switcher_placeholder">3.7.4</span>
<a href="../index.html">Documentation </a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="ipc.html" accesskey="U">Networking and Interprocess Communication</a> &#187;</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="module-asyncio">
<span id="asyncio-asynchronous-i-o"></span><h1><a class="reference internal" href="#module-asyncio" title="asyncio: Asynchronous I/O."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code></a> — Asynchronous I/O<a class="headerlink" href="#module-asyncio" title="Permalink to this headline"></a></h1>
<hr class="docutils" />
<div class="sidebar">
<p class="sidebar-title">Hello World!</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">main</span><span class="p">():</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Hello ...&#39;</span><span class="p">)</span>
<span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... World!&#39;</span><span class="p">)</span>
<span class="c1"># Python 3.7+</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>
</div>
<p>asyncio is a library to write <strong>concurrent</strong> code using
the <strong>async/await</strong> syntax.</p>
<p>asyncio is used as a foundation for multiple Python asynchronous
frameworks that provide high-performance network and web-servers,
database connection libraries, distributed task queues, etc.</p>
<p>asyncio is often a perfect fit for IO-bound and high-level
<strong>structured</strong> network code.</p>
<p>asyncio provides a set of <strong>high-level</strong> APIs to:</p>
<ul class="simple">
<li><p><a class="reference internal" href="asyncio-task.html#coroutine"><span class="std std-ref">run Python coroutines</span></a> concurrently and
have full control over their execution;</p></li>
<li><p>perform <a class="reference internal" href="asyncio-stream.html#asyncio-streams"><span class="std std-ref">network IO and IPC</span></a>;</p></li>
<li><p>control <a class="reference internal" href="asyncio-subprocess.html#asyncio-subprocess"><span class="std std-ref">subprocesses</span></a>;</p></li>
<li><p>distribute tasks via <a class="reference internal" href="asyncio-queue.html#asyncio-queues"><span class="std std-ref">queues</span></a>;</p></li>
<li><p><a class="reference internal" href="asyncio-sync.html#asyncio-sync"><span class="std std-ref">synchronize</span></a> concurrent code;</p></li>
</ul>
<p>Additionally, there are <strong>low-level</strong> APIs for
<em>library and framework developers</em> to:</p>
<ul class="simple">
<li><p>create and manage <a class="reference internal" href="asyncio-eventloop.html#asyncio-event-loop"><span class="std std-ref">event loops</span></a>, which
provide asynchronous APIs for <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.create_server" title="asyncio.loop.create_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">networking</span></code></a>,
running <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">subprocesses</span></code></a>,
handling <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.add_signal_handler" title="asyncio.loop.add_signal_handler"><code class="xref py py-meth docutils literal notranslate"><span class="pre">OS</span> <span class="pre">signals</span></code></a>, etc;</p></li>
<li><p>implement efficient protocols using
<a class="reference internal" href="asyncio-protocol.html#asyncio-transports-protocols"><span class="std std-ref">transports</span></a>;</p></li>
<li><p><a class="reference internal" href="asyncio-future.html#asyncio-futures"><span class="std std-ref">bridge</span></a> callback-based libraries and code
with async/await syntax.</p></li>
</ul>
<p class="rubric">Reference</p>
<div class="toctree-wrapper compound">
<p class="caption"><span class="caption-text">High-level APIs</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="asyncio-task.html">Coroutines and Tasks</a></li>
<li class="toctree-l1"><a class="reference internal" href="asyncio-stream.html">Streams</a></li>
<li class="toctree-l1"><a class="reference internal" href="asyncio-sync.html">Synchronization Primitives</a></li>
<li class="toctree-l1"><a class="reference internal" href="asyncio-subprocess.html">Subprocesses</a></li>
<li class="toctree-l1"><a class="reference internal" href="asyncio-queue.html">Queues</a></li>
<li class="toctree-l1"><a class="reference internal" href="asyncio-exceptions.html">Exceptions</a></li>
</ul>
</div>
<div class="toctree-wrapper compound">
<p class="caption"><span class="caption-text">Low-level APIs</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="asyncio-eventloop.html">Event Loop</a></li>
<li class="toctree-l1"><a class="reference internal" href="asyncio-future.html">Futures</a></li>
<li class="toctree-l1"><a class="reference internal" href="asyncio-protocol.html">Transports and Protocols</a></li>
<li class="toctree-l1"><a class="reference internal" href="asyncio-policy.html">Policies</a></li>
<li class="toctree-l1"><a class="reference internal" href="asyncio-platforms.html">Platform Support</a></li>
</ul>
</div>
<div class="toctree-wrapper compound">
<p class="caption"><span class="caption-text">Guides and Tutorials</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="asyncio-api-index.html">High-level API Index</a></li>
<li class="toctree-l1"><a class="reference internal" href="asyncio-llapi-index.html">Low-level API Index</a></li>
<li class="toctree-l1"><a class="reference internal" href="asyncio-dev.html">Developing with asyncio</a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="ipc.html"
title="previous chapter">Networking and Interprocess Communication</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="asyncio-task.html"
title="next chapter">Coroutines and Tasks</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.7/Doc/library/asyncio.rst"
rel="nofollow">Show Source
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="asyncio-task.html" title="Coroutines and Tasks"
>next</a> |</li>
<li class="right" >
<a href="ipc.html" title="Networking and Interprocess Communication"
>previous</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li>
<span class="language_switcher_placeholder">en</span>
<span class="version_switcher_placeholder">3.7.4</span>
<a href="../index.html">Documentation </a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">Copyright</a> 2001-2019, Python Software Foundation.
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
Last updated on Jul 13, 2019.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1.
</div>
</body>
</html>