parthtamu/rag-code-assistant
0
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="asyncio — Asynchronous I/O" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/asyncio.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Hello World!: asyncio is a library to write concurrent code using the async/await syntax. asyncio is used as a foundation for multiple Python asynchronous frameworks that provide high-performance n..." />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_c0d82eb4.png" />15<meta property="og:image:alt" content="Hello World!: asyncio is a library to write concurrent code using the async/await syntax. asyncio is used as a foundation for multiple Python asynchronous frameworks that provide high-performance n..." />16<meta name="description" content="Hello World!: asyncio is a library to write concurrent code using the async/await syntax. asyncio is used as a foundation for multiple Python asynchronous frameworks that provide high-performance n..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>asyncio — Asynchronous I/O — 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="Runners" href="asyncio-runner.html" />43 <link rel="prev" title="Networking and Interprocess Communication" href="ipc.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.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 <h4>Previous topic</h4>106 <p class="topless"><a href="ipc.html"107 title="previous chapter">Networking and Interprocess Communication</a></p>108 </div>109 <div>110 <h4>Next topic</h4>111 <p class="topless"><a href="asyncio-runner.html"112 title="next chapter">Runners</a></p>113 </div>114 <script>115 document.addEventListener('DOMContentLoaded', () => {116 const title = document.querySelector('meta[property="og:title"]').content;117 const elements = document.querySelectorAll('.improvepage');118 const pageurl = window.location.href.split('?')[0];119 elements.forEach(element => {120 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));121 url.searchParams.set('pagetitle', title);122 url.searchParams.set('pageurl', pageurl);123 url.searchParams.set('pagesource', "library/asyncio.rst");124 element.href = url.toString();125 });126 });127 </script>128 <div role="note" aria-label="source link">129 <h3>This page</h3>130 <ul class="this-page-menu">131 <li><a href="../bugs.html">Report a bug</a></li>132 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>133 <li>134 <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio.rst?plain=1"135 rel="nofollow">Show source136 </a>137 </li>138 139 </ul>140 </div>141 </nav>142 </div>143</div>144 145 146 <div class="related" role="navigation" aria-label="Related">147 <h3>Navigation</h3>148 <ul>149 <li class="right" style="margin-right: 10px">150 <a href="../genindex.html" title="General Index"151 accesskey="I">index</a></li>152 <li class="right" >153 <a href="../py-modindex.html" title="Python Module Index"154 >modules</a> |</li>155 <li class="right" >156 <a href="asyncio-runner.html" title="Runners"157 accesskey="N">next</a> |</li>158 <li class="right" >159 <a href="ipc.html" title="Networking and Interprocess Communication"160 accesskey="P">previous</a> |</li>161 162 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>163 <li><a href="https://www.python.org/">Python</a> »</li>164 <li class="switchers">165 <div class="language_switcher_placeholder"></div>166 <div class="version_switcher_placeholder"></div>167 </li>168 <li>169 170 </li>171 <li id="cpython-language-and-version">172 <a href="../index.html">3.15.0a6 Documentation</a> »173 </li>174 175 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>176 <li class="nav-item nav-item-2"><a href="ipc.html" accesskey="U">Networking and Interprocess Communication</a> »</li>177 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code> — Asynchronous I/O</a></li>178 <li class="right">179 180 181 <div class="inline-search" role="search">182 <form class="inline-search" action="../search.html" method="get">183 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">184 <input type="submit" value="Go">185 </form>186 </div>187 |188 </li>189 <li class="right">190<label class="theme-selector-label">191 Theme192 <select class="theme-selector" oninput="activateTheme(this.value)">193 <option value="auto" selected>Auto</option>194 <option value="light">Light</option>195 <option value="dark">Dark</option>196 </select>197</label> |</li>198 199 </ul>200 </div> 201 202 <div class="document">203 <div class="documentwrapper">204 <div class="bodywrapper">205 <div class="body" role="main">206 207 <section id="module-asyncio">208<span id="asyncio-asynchronous-i-o"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code> — Asynchronous I/O<a class="headerlink" href="#module-asyncio" title="Link to this heading">¶</a></h1>209<hr class="docutils" />210<aside class="sidebar">211<p class="sidebar-title">Hello World!</p>212<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>213 214<span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">main</span><span class="p">():</span>215 <span class="nb">print</span><span class="p">(</span><span class="s1">'Hello ...'</span><span class="p">)</span>216 <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>217 <span class="nb">print</span><span class="p">(</span><span class="s1">'... World!'</span><span class="p">)</span>218 219<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>220</pre></div>221</div>222</aside>223<p>asyncio is a library to write <strong>concurrent</strong> code using224the <strong>async/await</strong> syntax.</p>225<p>asyncio is used as a foundation for multiple Python asynchronous226frameworks that provide high-performance network and web-servers,227database connection libraries, distributed task queues, etc.</p>228<p>asyncio is often a perfect fit for IO-bound and high-level229<strong>structured</strong> network code.</p>230<div class="admonition seealso">231<p class="admonition-title">See also</p>232<dl class="simple">233<dt><a class="reference internal" href="../howto/a-conceptual-overview-of-asyncio.html#a-conceptual-overview-of-asyncio"><span class="std std-ref">A conceptual overview of asyncio</span></a></dt><dd><p>Explanation of the fundamentals of asyncio.</p>234</dd>235</dl>236</div>237<p>asyncio provides a set of <strong>high-level</strong> APIs to:</p>238<ul class="simple">239<li><p><a class="reference internal" href="asyncio-task.html#coroutine"><span class="std std-ref">run Python coroutines</span></a> concurrently and240have full control over their execution;</p></li>241<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>242<li><p>control <a class="reference internal" href="asyncio-subprocess.html#asyncio-subprocess"><span class="std std-ref">subprocesses</span></a>;</p></li>243<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>244<li><p><a class="reference internal" href="asyncio-sync.html#asyncio-sync"><span class="std std-ref">synchronize</span></a> concurrent code;</p></li>245</ul>246<p>Additionally, there are <strong>low-level</strong> APIs for247<em>library and framework developers</em> to:</p>248<ul class="simple">249<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>, which250provide asynchronous APIs for <a class="reference internal" href="asyncio-eventloop.html#loop-create-server"><span class="std std-ref">networking</span></a>,251running <a class="reference internal" href="asyncio-eventloop.html#loop-subprocess-exec"><span class="std std-ref">subprocesses</span></a>,252handling <a class="reference internal" href="asyncio-eventloop.html#loop-add-signal-handler"><span class="std std-ref">OS signals</span></a>, etc;</p></li>253<li><p>implement efficient protocols using254<a class="reference internal" href="asyncio-protocol.html#asyncio-transports-protocols"><span class="std std-ref">transports</span></a>;</p></li>255<li><p><a class="reference internal" href="asyncio-future.html#asyncio-futures"><span class="std std-ref">bridge</span></a> callback-based libraries and code256with async/await syntax.</p></li>257</ul>258<div class="availability docutils container">259<p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not WASI.</p>260<p>This module does not work or is not available on WebAssembly. See261<a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>262</div>263<p class="rubric" id="asyncio-cli">asyncio REPL</p>264<p>You can experiment with an <code class="docutils literal notranslate"><span class="pre">asyncio</span></code> concurrent context in the <a class="reference internal" href="../glossary.html#term-REPL"><span class="xref std std-term">REPL</span></a>:</p>265<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="go">$ python -m asyncio</span>266<span class="go">asyncio REPL ...</span>267<span class="go">Use "await" directly instead of "asyncio.run()".</span>268<span class="go">Type "help", "copyright", "credits" or "license" for more information.</span>269<span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">asyncio</span>270<span class="gp">>>> </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">10</span><span class="p">,</span> <span class="n">result</span><span class="o">=</span><span class="s1">'hello'</span><span class="p">)</span>271<span class="go">'hello'</span>272</pre></div>273</div>274<p>This REPL provides limited compatibility with <span class="target" id="index-0"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHON_BASIC_REPL"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHON_BASIC_REPL</span></code></a>.275It is recommended that the default REPL is used276for full functionality and the latest features.</p>277<p class="audit-hook" id="audit_event_cpython_run_stdin_2">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">cpython.run_stdin</span></code> with no arguments.</p>278<div class="versionchanged">279<p><span class="versionmodified changed">Changed in version 3.12.5: </span>(also 3.11.10, 3.10.15, 3.9.20, and 3.8.20)280Emits audit events.</p>281</div>282<div class="versionchanged">283<p><span class="versionmodified changed">Changed in version 3.13: </span>Uses PyREPL if possible, in which case <span class="target" id="index-1"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONSTARTUP"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONSTARTUP</span></code></a> is284also executed. Emits audit events.</p>285</div>286<p class="rubric">Reference</p>287<div class="toctree-wrapper compound">288<p class="caption" role="heading"><span class="caption-text">High-level APIs</span></p>289<ul>290<li class="toctree-l1"><a class="reference internal" href="asyncio-runner.html">Runners</a></li>291<li class="toctree-l1"><a class="reference internal" href="asyncio-task.html">Coroutines and Tasks</a></li>292<li class="toctree-l1"><a class="reference internal" href="asyncio-stream.html">Streams</a></li>293<li class="toctree-l1"><a class="reference internal" href="asyncio-sync.html">Synchronization Primitives</a></li>294<li class="toctree-l1"><a class="reference internal" href="asyncio-subprocess.html">Subprocesses</a></li>295<li class="toctree-l1"><a class="reference internal" href="asyncio-queue.html">Queues</a></li>296<li class="toctree-l1"><a class="reference internal" href="asyncio-exceptions.html">Exceptions</a></li>297<li class="toctree-l1"><a class="reference internal" href="asyncio-graph.html">Call Graph Introspection</a></li>298</ul>299</div>300<div class="toctree-wrapper compound">301<p class="caption" role="heading"><span class="caption-text">Low-level APIs</span></p>302<ul>303<li class="toctree-l1"><a class="reference internal" href="asyncio-eventloop.html">Event Loop</a></li>304<li class="toctree-l1"><a class="reference internal" href="asyncio-future.html">Futures</a></li>305<li class="toctree-l1"><a class="reference internal" href="asyncio-protocol.html">Transports and Protocols</a></li>306<li class="toctree-l1"><a class="reference internal" href="asyncio-policy.html">Policies</a></li>307<li class="toctree-l1"><a class="reference internal" href="asyncio-platforms.html">Platform Support</a></li>308<li class="toctree-l1"><a class="reference internal" href="asyncio-extending.html">Extending</a></li>309</ul>310</div>311<div class="toctree-wrapper compound">312<p class="caption" role="heading"><span class="caption-text">Guides and Tutorials</span></p>313<ul>314<li class="toctree-l1"><a class="reference internal" href="asyncio-api-index.html">High-level API Index</a></li>315<li class="toctree-l1"><a class="reference internal" href="asyncio-llapi-index.html">Low-level API Index</a></li>316<li class="toctree-l1"><a class="reference internal" href="asyncio-dev.html">Developing with asyncio</a></li>317</ul>318</div>319<div class="admonition note">320<p class="admonition-title">Note</p>321<p>The source code for asyncio can be found in <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/asyncio/">Lib/asyncio/</a>.</p>322</div>323</section>324 325 326 <div class="clearer"></div>327 </div>328 </div>329 </div>330 <div class="sphinxsidebar" role="navigation" aria-label="Main">331 <div class="sphinxsidebarwrapper">332 <div>333 <h4>Previous topic</h4>334 <p class="topless"><a href="ipc.html"335 title="previous chapter">Networking and Interprocess Communication</a></p>336 </div>337 <div>338 <h4>Next topic</h4>339 <p class="topless"><a href="asyncio-runner.html"340 title="next chapter">Runners</a></p>341 </div>342 <script>343 document.addEventListener('DOMContentLoaded', () => {344 const title = document.querySelector('meta[property="og:title"]').content;345 const elements = document.querySelectorAll('.improvepage');346 const pageurl = window.location.href.split('?')[0];347 elements.forEach(element => {348 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));349 url.searchParams.set('pagetitle', title);350 url.searchParams.set('pageurl', pageurl);351 url.searchParams.set('pagesource', "library/asyncio.rst");352 element.href = url.toString();353 });354 });355 </script>356 <div role="note" aria-label="source link">357 <h3>This page</h3>358 <ul class="this-page-menu">359 <li><a href="../bugs.html">Report a bug</a></li>360 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>361 <li>362 <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio.rst?plain=1"363 rel="nofollow">Show source364 </a>365 </li>366 367 </ul>368 </div>369 </div>370<div id="sidebarbutton" title="Collapse sidebar">371<span>«</span>372</div>373 374 </div>375 <div class="clearer"></div>376 </div> 377 <div class="related" role="navigation" aria-label="Related">378 <h3>Navigation</h3>379 <ul>380 <li class="right" style="margin-right: 10px">381 <a href="../genindex.html" title="General Index"382 >index</a></li>383 <li class="right" >384 <a href="../py-modindex.html" title="Python Module Index"385 >modules</a> |</li>386 <li class="right" >387 <a href="asyncio-runner.html" title="Runners"388 >next</a> |</li>389 <li class="right" >390 <a href="ipc.html" title="Networking and Interprocess Communication"391 >previous</a> |</li>392 393 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>394 <li><a href="https://www.python.org/">Python</a> »</li>395 <li class="switchers">396 <div class="language_switcher_placeholder"></div>397 <div class="version_switcher_placeholder"></div>398 </li>399 <li>400 401 </li>402 <li id="cpython-language-and-version">403 <a href="../index.html">3.15.0a6 Documentation</a> »404 </li>405 406 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>407 <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> »</li>408 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code> — Asynchronous I/O</a></li>409 <li class="right">410 411 412 <div class="inline-search" role="search">413 <form class="inline-search" action="../search.html" method="get">414 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">415 <input type="submit" value="Go">416 </form>417 </div>418 |419 </li>420 <li class="right">421<label class="theme-selector-label">422 Theme423 <select class="theme-selector" oninput="activateTheme(this.value)">424 <option value="auto" selected>Auto</option>425 <option value="light">Light</option>426 <option value="dark">Dark</option>427 </select>428</label> |</li>429 430 </ul>431 </div> 432 <div class="footer">433 © <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.434 <br>435 This page is licensed under the Python Software Foundation License Version 2.436 <br>437 Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.438 <br>439 440 See <a href="/license.html">History and License</a> for more information.<br>441 442 443 <br>444 445 The Python Software Foundation is a non-profit corporation.446<a href="https://www.python.org/psf/donations/">Please donate.</a>447<br>448 <br>449 Last updated on Mar 10, 2026 (08:58 UTC).450 451 <a href="/bugs.html">Found a bug</a>?452 453 <br>454 455 Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.456 </div>457 458 </body>459</html>