gooderp18绿色标准版
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

2201 行
219KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="Event Loop" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/asyncio-eventloop.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/asyncio/events.py, Lib/asyncio/base_events.py Preface The event loop is the core of every asyncio application. Event loops run asynchronous tasks and callbacks, perform network IO ..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/asyncio/events.py, Lib/asyncio/base_events.py Preface The event loop is the core of every asyncio application. Event loops run asynchronous tasks and callbacks, perform network IO ..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>Event Loop &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="Futures" href="asyncio-future.html" />
  33. <link rel="prev" title="Exceptions" href="asyncio-exceptions.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/asyncio-eventloop.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">Event Loop</a><ul>
  86. <li><a class="reference internal" href="#event-loop-methods">Event Loop Methods</a><ul>
  87. <li><a class="reference internal" href="#running-and-stopping-the-loop">Running and stopping the loop</a></li>
  88. <li><a class="reference internal" href="#scheduling-callbacks">Scheduling callbacks</a></li>
  89. <li><a class="reference internal" href="#scheduling-delayed-callbacks">Scheduling delayed callbacks</a></li>
  90. <li><a class="reference internal" href="#creating-futures-and-tasks">Creating Futures and Tasks</a></li>
  91. <li><a class="reference internal" href="#opening-network-connections">Opening network connections</a></li>
  92. <li><a class="reference internal" href="#creating-network-servers">Creating network servers</a></li>
  93. <li><a class="reference internal" href="#transferring-files">Transferring files</a></li>
  94. <li><a class="reference internal" href="#tls-upgrade">TLS Upgrade</a></li>
  95. <li><a class="reference internal" href="#watching-file-descriptors">Watching file descriptors</a></li>
  96. <li><a class="reference internal" href="#working-with-socket-objects-directly">Working with socket objects directly</a></li>
  97. <li><a class="reference internal" href="#dns">DNS</a></li>
  98. <li><a class="reference internal" href="#working-with-pipes">Working with pipes</a></li>
  99. <li><a class="reference internal" href="#unix-signals">Unix signals</a></li>
  100. <li><a class="reference internal" href="#executing-code-in-thread-or-process-pools">Executing code in thread or process pools</a></li>
  101. <li><a class="reference internal" href="#error-handling-api">Error Handling API</a></li>
  102. <li><a class="reference internal" href="#enabling-debug-mode">Enabling debug mode</a></li>
  103. <li><a class="reference internal" href="#running-subprocesses">Running Subprocesses</a></li>
  104. </ul>
  105. </li>
  106. <li><a class="reference internal" href="#callback-handles">Callback Handles</a></li>
  107. <li><a class="reference internal" href="#server-objects">Server Objects</a></li>
  108. <li><a class="reference internal" href="#event-loop-implementations">Event Loop Implementations</a></li>
  109. <li><a class="reference internal" href="#examples">Examples</a><ul>
  110. <li><a class="reference internal" href="#hello-world-with-call-soon">Hello World with call_soon()</a></li>
  111. <li><a class="reference internal" href="#display-the-current-date-with-call-later">Display the current date with call_later()</a></li>
  112. <li><a class="reference internal" href="#watch-a-file-descriptor-for-read-events">Watch a file descriptor for read events</a></li>
  113. <li><a class="reference internal" href="#set-signal-handlers-for-sigint-and-sigterm">Set signal handlers for SIGINT and SIGTERM</a></li>
  114. </ul>
  115. </li>
  116. </ul>
  117. </li>
  118. </ul>
  119. </div>
  120. <div>
  121. <h4>Previous topic</h4>
  122. <p class="topless"><a href="asyncio-exceptions.html"
  123. title="previous chapter">Exceptions</a></p>
  124. </div>
  125. <div>
  126. <h4>Next topic</h4>
  127. <p class="topless"><a href="asyncio-future.html"
  128. title="next chapter">Futures</a></p>
  129. </div>
  130. <div role="note" aria-label="source link">
  131. <h3>This Page</h3>
  132. <ul class="this-page-menu">
  133. <li><a href="../bugs.html">Report a Bug</a></li>
  134. <li>
  135. <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio-eventloop.rst"
  136. rel="nofollow">Show Source
  137. </a>
  138. </li>
  139. </ul>
  140. </div>
  141. </nav>
  142. </div>
  143. </div>
  144. <div class="related" role="navigation" aria-label="related navigation">
  145. <h3>Navigation</h3>
  146. <ul>
  147. <li class="right" style="margin-right: 10px">
  148. <a href="../genindex.html" title="General Index"
  149. accesskey="I">index</a></li>
  150. <li class="right" >
  151. <a href="../py-modindex.html" title="Python Module Index"
  152. >modules</a> |</li>
  153. <li class="right" >
  154. <a href="asyncio-future.html" title="Futures"
  155. accesskey="N">next</a> |</li>
  156. <li class="right" >
  157. <a href="asyncio-exceptions.html" title="Exceptions"
  158. accesskey="P">previous</a> |</li>
  159. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  160. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  161. <li class="switchers">
  162. <div class="language_switcher_placeholder"></div>
  163. <div class="version_switcher_placeholder"></div>
  164. </li>
  165. <li>
  166. </li>
  167. <li id="cpython-language-and-version">
  168. <a href="../index.html">3.12.3 Documentation</a> &#187;
  169. </li>
  170. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  171. <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
  172. <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>
  173. <li class="nav-item nav-item-this"><a href="">Event Loop</a></li>
  174. <li class="right">
  175. <div class="inline-search" role="search">
  176. <form class="inline-search" action="../search.html" method="get">
  177. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  178. <input type="submit" value="Go" />
  179. </form>
  180. </div>
  181. |
  182. </li>
  183. <li class="right">
  184. <label class="theme-selector-label">
  185. Theme
  186. <select class="theme-selector" oninput="activateTheme(this.value)">
  187. <option value="auto" selected>Auto</option>
  188. <option value="light">Light</option>
  189. <option value="dark">Dark</option>
  190. </select>
  191. </label> |</li>
  192. </ul>
  193. </div>
  194. <div class="document">
  195. <div class="documentwrapper">
  196. <div class="bodywrapper">
  197. <div class="body" role="main">
  198. <section id="event-loop">
  199. <span id="asyncio-event-loop"></span><h1>Event Loop<a class="headerlink" href="#event-loop" title="Link to this heading">¶</a></h1>
  200. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/asyncio/events.py">Lib/asyncio/events.py</a>,
  201. <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/asyncio/base_events.py">Lib/asyncio/base_events.py</a></p>
  202. <hr class="docutils" />
  203. <p class="rubric">Preface</p>
  204. <p>The event loop is the core of every asyncio application.
  205. Event loops run asynchronous tasks and callbacks, perform network
  206. IO operations, and run subprocesses.</p>
  207. <p>Application developers should typically use the high-level asyncio functions,
  208. such as <a class="reference internal" href="asyncio-runner.html#asyncio.run" title="asyncio.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.run()</span></code></a>, and should rarely need to reference the loop
  209. object or call its methods. This section is intended mostly for authors
  210. of lower-level code, libraries, and frameworks, who need finer control over
  211. the event loop behavior.</p>
  212. <p class="rubric">Obtaining the Event Loop</p>
  213. <p>The following low-level functions can be used to get, set, or create
  214. an event loop:</p>
  215. <dl class="py function">
  216. <dt class="sig sig-object py" id="asyncio.get_running_loop">
  217. <span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">get_running_loop</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.get_running_loop" title="Link to this definition">¶</a></dt>
  218. <dd><p>Return the running event loop in the current OS thread.</p>
  219. <p>Raise a <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> if there is no running event loop.</p>
  220. <p>This function can only be called from a coroutine or a callback.</p>
  221. <div class="versionadded">
  222. <p><span class="versionmodified added">New in version 3.7.</span></p>
  223. </div>
  224. </dd></dl>
  225. <dl class="py function">
  226. <dt class="sig sig-object py" id="asyncio.get_event_loop">
  227. <span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">get_event_loop</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.get_event_loop" title="Link to this definition">¶</a></dt>
  228. <dd><p>Get the current event loop.</p>
  229. <p>When called from a coroutine or a callback (e.g. scheduled with
  230. call_soon or similar API), this function will always return the
  231. running event loop.</p>
  232. <p>If there is no running event loop set, the function will return
  233. the result of the <code class="docutils literal notranslate"><span class="pre">get_event_loop_policy().get_event_loop()</span></code> call.</p>
  234. <p>Because this function has rather complex behavior (especially
  235. when custom event loop policies are in use), using the
  236. <a class="reference internal" href="#asyncio.get_running_loop" title="asyncio.get_running_loop"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_running_loop()</span></code></a> function is preferred to <a class="reference internal" href="#asyncio.get_event_loop" title="asyncio.get_event_loop"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_event_loop()</span></code></a>
  237. in coroutines and callbacks.</p>
  238. <p>As noted above, consider using the higher-level <a class="reference internal" href="asyncio-runner.html#asyncio.run" title="asyncio.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.run()</span></code></a> function,
  239. instead of using these lower level functions to manually create and close an
  240. event loop.</p>
  241. <div class="deprecated">
  242. <p><span class="versionmodified deprecated">Deprecated since version 3.12: </span>Deprecation warning is emitted if there is no current event loop.
  243. In some future Python release this will become an error.</p>
  244. </div>
  245. </dd></dl>
  246. <dl class="py function">
  247. <dt class="sig sig-object py" id="asyncio.set_event_loop">
  248. <span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">set_event_loop</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">loop</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.set_event_loop" title="Link to this definition">¶</a></dt>
  249. <dd><p>Set <em>loop</em> as the current event loop for the current OS thread.</p>
  250. </dd></dl>
  251. <dl class="py function">
  252. <dt class="sig sig-object py" id="asyncio.new_event_loop">
  253. <span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">new_event_loop</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.new_event_loop" title="Link to this definition">¶</a></dt>
  254. <dd><p>Create and return a new event loop object.</p>
  255. </dd></dl>
  256. <p>Note that the behaviour of <a class="reference internal" href="#asyncio.get_event_loop" title="asyncio.get_event_loop"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_event_loop()</span></code></a>, <a class="reference internal" href="#asyncio.set_event_loop" title="asyncio.set_event_loop"><code class="xref py py-func docutils literal notranslate"><span class="pre">set_event_loop()</span></code></a>,
  257. and <a class="reference internal" href="#asyncio.new_event_loop" title="asyncio.new_event_loop"><code class="xref py py-func docutils literal notranslate"><span class="pre">new_event_loop()</span></code></a> functions can be altered by
  258. <a class="reference internal" href="asyncio-policy.html#asyncio-policies"><span class="std std-ref">setting a custom event loop policy</span></a>.</p>
  259. <p class="rubric">Contents</p>
  260. <p>This documentation page contains the following sections:</p>
  261. <ul class="simple">
  262. <li><p>The <a class="reference internal" href="#event-loop-methods">Event Loop Methods</a> section is the reference documentation of
  263. the event loop APIs;</p></li>
  264. <li><p>The <a class="reference internal" href="#callback-handles">Callback Handles</a> section documents the <a class="reference internal" href="#asyncio.Handle" title="asyncio.Handle"><code class="xref py py-class docutils literal notranslate"><span class="pre">Handle</span></code></a> and
  265. <a class="reference internal" href="#asyncio.TimerHandle" title="asyncio.TimerHandle"><code class="xref py py-class docutils literal notranslate"><span class="pre">TimerHandle</span></code></a> instances which are returned from scheduling
  266. methods such as <a class="reference internal" href="#asyncio.loop.call_soon" title="asyncio.loop.call_soon"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.call_soon()</span></code></a> and <a class="reference internal" href="#asyncio.loop.call_later" title="asyncio.loop.call_later"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.call_later()</span></code></a>;</p></li>
  267. <li><p>The <a class="reference internal" href="#server-objects">Server Objects</a> section documents types returned from
  268. event loop methods like <a class="reference internal" href="#asyncio.loop.create_server" title="asyncio.loop.create_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_server()</span></code></a>;</p></li>
  269. <li><p>The <a class="reference internal" href="#event-loop-implementations">Event Loop Implementations</a> section documents the
  270. <a class="reference internal" href="#asyncio.SelectorEventLoop" title="asyncio.SelectorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectorEventLoop</span></code></a> and <a class="reference internal" href="#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProactorEventLoop</span></code></a> classes;</p></li>
  271. <li><p>The <a class="reference internal" href="#examples">Examples</a> section showcases how to work with some event
  272. loop APIs.</p></li>
  273. </ul>
  274. <section id="event-loop-methods">
  275. <span id="asyncio-event-loop-methods"></span><h2>Event Loop Methods<a class="headerlink" href="#event-loop-methods" title="Link to this heading">¶</a></h2>
  276. <p>Event loops have <strong>low-level</strong> APIs for the following:</p>
  277. <nav class="contents local" id="contents">
  278. <ul class="simple">
  279. <li><p><a class="reference internal" href="#running-and-stopping-the-loop" id="id1">Running and stopping the loop</a></p></li>
  280. <li><p><a class="reference internal" href="#scheduling-callbacks" id="id2">Scheduling callbacks</a></p></li>
  281. <li><p><a class="reference internal" href="#scheduling-delayed-callbacks" id="id3">Scheduling delayed callbacks</a></p></li>
  282. <li><p><a class="reference internal" href="#creating-futures-and-tasks" id="id4">Creating Futures and Tasks</a></p></li>
  283. <li><p><a class="reference internal" href="#opening-network-connections" id="id5">Opening network connections</a></p></li>
  284. <li><p><a class="reference internal" href="#creating-network-servers" id="id6">Creating network servers</a></p></li>
  285. <li><p><a class="reference internal" href="#transferring-files" id="id7">Transferring files</a></p></li>
  286. <li><p><a class="reference internal" href="#tls-upgrade" id="id8">TLS Upgrade</a></p></li>
  287. <li><p><a class="reference internal" href="#watching-file-descriptors" id="id9">Watching file descriptors</a></p></li>
  288. <li><p><a class="reference internal" href="#working-with-socket-objects-directly" id="id10">Working with socket objects directly</a></p></li>
  289. <li><p><a class="reference internal" href="#dns" id="id11">DNS</a></p></li>
  290. <li><p><a class="reference internal" href="#working-with-pipes" id="id12">Working with pipes</a></p></li>
  291. <li><p><a class="reference internal" href="#unix-signals" id="id13">Unix signals</a></p></li>
  292. <li><p><a class="reference internal" href="#executing-code-in-thread-or-process-pools" id="id14">Executing code in thread or process pools</a></p></li>
  293. <li><p><a class="reference internal" href="#error-handling-api" id="id15">Error Handling API</a></p></li>
  294. <li><p><a class="reference internal" href="#enabling-debug-mode" id="id16">Enabling debug mode</a></p></li>
  295. <li><p><a class="reference internal" href="#running-subprocesses" id="id17">Running Subprocesses</a></p></li>
  296. </ul>
  297. </nav>
  298. <section id="running-and-stopping-the-loop">
  299. <h3><a class="toc-backref" href="#id1" role="doc-backlink">Running and stopping the loop</a><a class="headerlink" href="#running-and-stopping-the-loop" title="Link to this heading">¶</a></h3>
  300. <dl class="py method">
  301. <dt class="sig sig-object py" id="asyncio.loop.run_until_complete">
  302. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">run_until_complete</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">future</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.run_until_complete" title="Link to this definition">¶</a></dt>
  303. <dd><p>Run until the <em>future</em> (an instance of <a class="reference internal" href="asyncio-future.html#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a>) has
  304. completed.</p>
  305. <p>If the argument is a <a class="reference internal" href="asyncio-task.html#coroutine"><span class="std std-ref">coroutine object</span></a> it
  306. is implicitly scheduled to run as a <a class="reference internal" href="asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Task</span></code></a>.</p>
  307. <p>Return the Future’s result or raise its exception.</p>
  308. </dd></dl>
  309. <dl class="py method">
  310. <dt class="sig sig-object py" id="asyncio.loop.run_forever">
  311. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">run_forever</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.run_forever" title="Link to this definition">¶</a></dt>
  312. <dd><p>Run the event loop until <a class="reference internal" href="#asyncio.loop.stop" title="asyncio.loop.stop"><code class="xref py py-meth docutils literal notranslate"><span class="pre">stop()</span></code></a> is called.</p>
  313. <p>If <a class="reference internal" href="#asyncio.loop.stop" title="asyncio.loop.stop"><code class="xref py py-meth docutils literal notranslate"><span class="pre">stop()</span></code></a> is called before <a class="reference internal" href="#asyncio.loop.run_forever" title="asyncio.loop.run_forever"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run_forever()</span></code></a> is called,
  314. the loop will poll the I/O selector once with a timeout of zero,
  315. run all callbacks scheduled in response to I/O events (and
  316. those that were already scheduled), and then exit.</p>
  317. <p>If <a class="reference internal" href="#asyncio.loop.stop" title="asyncio.loop.stop"><code class="xref py py-meth docutils literal notranslate"><span class="pre">stop()</span></code></a> is called while <a class="reference internal" href="#asyncio.loop.run_forever" title="asyncio.loop.run_forever"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run_forever()</span></code></a> is running,
  318. the loop will run the current batch of callbacks and then exit.
  319. Note that new callbacks scheduled by callbacks will not run in this
  320. case; instead, they will run the next time <a class="reference internal" href="#asyncio.loop.run_forever" title="asyncio.loop.run_forever"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run_forever()</span></code></a> or
  321. <a class="reference internal" href="#asyncio.loop.run_until_complete" title="asyncio.loop.run_until_complete"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run_until_complete()</span></code></a> is called.</p>
  322. </dd></dl>
  323. <dl class="py method">
  324. <dt class="sig sig-object py" id="asyncio.loop.stop">
  325. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">stop</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.stop" title="Link to this definition">¶</a></dt>
  326. <dd><p>Stop the event loop.</p>
  327. </dd></dl>
  328. <dl class="py method">
  329. <dt class="sig sig-object py" id="asyncio.loop.is_running">
  330. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">is_running</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.is_running" title="Link to this definition">¶</a></dt>
  331. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the event loop is currently running.</p>
  332. </dd></dl>
  333. <dl class="py method">
  334. <dt class="sig sig-object py" id="asyncio.loop.is_closed">
  335. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">is_closed</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.is_closed" title="Link to this definition">¶</a></dt>
  336. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the event loop was closed.</p>
  337. </dd></dl>
  338. <dl class="py method">
  339. <dt class="sig sig-object py" id="asyncio.loop.close">
  340. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.close" title="Link to this definition">¶</a></dt>
  341. <dd><p>Close the event loop.</p>
  342. <p>The loop must not be running when this function is called.
  343. Any pending callbacks will be discarded.</p>
  344. <p>This method clears all queues and shuts down the executor, but does
  345. not wait for the executor to finish.</p>
  346. <p>This method is idempotent and irreversible. No other methods
  347. should be called after the event loop is closed.</p>
  348. </dd></dl>
  349. <dl class="py method">
  350. <dt class="sig sig-object py" id="asyncio.loop.shutdown_asyncgens">
  351. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">shutdown_asyncgens</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.shutdown_asyncgens" title="Link to this definition">¶</a></dt>
  352. <dd><p>Schedule all currently open <a class="reference internal" href="../glossary.html#term-asynchronous-generator"><span class="xref std std-term">asynchronous generator</span></a> objects to
  353. close with an <a class="reference internal" href="../reference/expressions.html#agen.aclose" title="agen.aclose"><code class="xref py py-meth docutils literal notranslate"><span class="pre">aclose()</span></code></a> call. After calling this method,
  354. the event loop will issue a warning if a new asynchronous generator
  355. is iterated. This should be used to reliably finalize all scheduled
  356. asynchronous generators.</p>
  357. <p>Note that there is no need to call this function when
  358. <a class="reference internal" href="asyncio-runner.html#asyncio.run" title="asyncio.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.run()</span></code></a> is used.</p>
  359. <p>Example:</p>
  360. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
  361. <span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
  362. <span class="k">finally</span><span class="p">:</span>
  363. <span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">loop</span><span class="o">.</span><span class="n">shutdown_asyncgens</span><span class="p">())</span>
  364. <span class="n">loop</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  365. </pre></div>
  366. </div>
  367. <div class="versionadded">
  368. <p><span class="versionmodified added">New in version 3.6.</span></p>
  369. </div>
  370. </dd></dl>
  371. <dl class="py method">
  372. <dt class="sig sig-object py" id="asyncio.loop.shutdown_default_executor">
  373. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">shutdown_default_executor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">timeout</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.loop.shutdown_default_executor" title="Link to this definition">¶</a></dt>
  374. <dd><p>Schedule the closure of the default executor and wait for it to join all of
  375. the threads in the <a class="reference internal" href="concurrent.futures.html#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code></a>.
  376. Once this method has been called,
  377. using the default executor with <a class="reference internal" href="#asyncio.loop.run_in_executor" title="asyncio.loop.run_in_executor"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.run_in_executor()</span></code></a>
  378. will raise a <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>
  379. <p>The <em>timeout</em> parameter specifies the amount of time
  380. (in <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> seconds) the executor will be given to finish joining.
  381. With the default, <code class="docutils literal notranslate"><span class="pre">None</span></code>,
  382. the executor is allowed an unlimited amount of time.</p>
  383. <p>If the <em>timeout</em> is reached, a <a class="reference internal" href="exceptions.html#RuntimeWarning" title="RuntimeWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeWarning</span></code></a> is emitted
  384. and the default executor is terminated
  385. without waiting for its threads to finish joining.</p>
  386. <div class="admonition note">
  387. <p class="admonition-title">Note</p>
  388. <p>Do not call this method when using <a class="reference internal" href="asyncio-runner.html#asyncio.run" title="asyncio.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.run()</span></code></a>,
  389. as the latter handles default executor shutdown automatically.</p>
  390. </div>
  391. <div class="versionadded">
  392. <p><span class="versionmodified added">New in version 3.9.</span></p>
  393. </div>
  394. <div class="versionchanged">
  395. <p><span class="versionmodified changed">Changed in version 3.12: </span>Added the <em>timeout</em> parameter.</p>
  396. </div>
  397. </dd></dl>
  398. </section>
  399. <section id="scheduling-callbacks">
  400. <h3><a class="toc-backref" href="#id2" role="doc-backlink">Scheduling callbacks</a><a class="headerlink" href="#scheduling-callbacks" title="Link to this heading">¶</a></h3>
  401. <dl class="py method">
  402. <dt class="sig sig-object py" id="asyncio.loop.call_soon">
  403. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">call_soon</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">callback</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">context</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.loop.call_soon" title="Link to this definition">¶</a></dt>
  404. <dd><p>Schedule the <em>callback</em> <a class="reference internal" href="../glossary.html#term-callback"><span class="xref std std-term">callback</span></a> to be called with
  405. <em>args</em> arguments at the next iteration of the event loop.</p>
  406. <p>Return an instance of <a class="reference internal" href="#asyncio.Handle" title="asyncio.Handle"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Handle</span></code></a>,
  407. which can be used later to cancel the callback.</p>
  408. <p>Callbacks are called in the order in which they are registered.
  409. Each callback will be called exactly once.</p>
  410. <p>The optional keyword-only <em>context</em> argument specifies a
  411. custom <a class="reference internal" href="contextvars.html#contextvars.Context" title="contextvars.Context"><code class="xref py py-class docutils literal notranslate"><span class="pre">contextvars.Context</span></code></a> for the <em>callback</em> to run in.
  412. Callbacks use the current context when no <em>context</em> is provided.</p>
  413. <p>Unlike <a class="reference internal" href="#asyncio.loop.call_soon_threadsafe" title="asyncio.loop.call_soon_threadsafe"><code class="xref py py-meth docutils literal notranslate"><span class="pre">call_soon_threadsafe()</span></code></a>, this method is not thread-safe.</p>
  414. </dd></dl>
  415. <dl class="py method">
  416. <dt class="sig sig-object py" id="asyncio.loop.call_soon_threadsafe">
  417. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">call_soon_threadsafe</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">callback</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">context</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.loop.call_soon_threadsafe" title="Link to this definition">¶</a></dt>
  418. <dd><p>A thread-safe variant of <a class="reference internal" href="#asyncio.loop.call_soon" title="asyncio.loop.call_soon"><code class="xref py py-meth docutils literal notranslate"><span class="pre">call_soon()</span></code></a>. When scheduling callbacks from
  419. another thread, this function <em>must</em> be used, since <a class="reference internal" href="#asyncio.loop.call_soon" title="asyncio.loop.call_soon"><code class="xref py py-meth docutils literal notranslate"><span class="pre">call_soon()</span></code></a> is not
  420. thread-safe.</p>
  421. <p>Raises <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> if called on a loop that’s been closed.
  422. This can happen on a secondary thread when the main application is
  423. shutting down.</p>
  424. <p>See the <a class="reference internal" href="asyncio-dev.html#asyncio-multithreading"><span class="std std-ref">concurrency and multithreading</span></a>
  425. section of the documentation.</p>
  426. <div class="versionchanged">
  427. <p><span class="versionmodified changed">Changed in version 3.7: </span>The <em>context</em> keyword-only parameter was added. See <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0567/"><strong>PEP 567</strong></a>
  428. for more details.</p>
  429. </div>
  430. </dd></dl>
  431. <div class="admonition note" id="asyncio-pass-keywords">
  432. <p class="admonition-title">Note</p>
  433. <p>Most <a class="reference internal" href="asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code></a> scheduling functions don’t allow passing
  434. keyword arguments. To do that, use <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a>:</p>
  435. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># will schedule &quot;print(&quot;Hello&quot;, flush=True)&quot;</span>
  436. <span class="n">loop</span><span class="o">.</span><span class="n">call_soon</span><span class="p">(</span>
  437. <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="nb">print</span><span class="p">,</span> <span class="s2">&quot;Hello&quot;</span><span class="p">,</span> <span class="n">flush</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
  438. </pre></div>
  439. </div>
  440. <p>Using partial objects is usually more convenient than using lambdas,
  441. as asyncio can render partial objects better in debug and error
  442. messages.</p>
  443. </div>
  444. </section>
  445. <section id="scheduling-delayed-callbacks">
  446. <span id="asyncio-delayed-calls"></span><h3><a class="toc-backref" href="#id3" role="doc-backlink">Scheduling delayed callbacks</a><a class="headerlink" href="#scheduling-delayed-callbacks" title="Link to this heading">¶</a></h3>
  447. <p>Event loop provides mechanisms to schedule callback functions
  448. to be called at some point in the future. Event loop uses monotonic
  449. clocks to track time.</p>
  450. <dl class="py method">
  451. <dt class="sig sig-object py" id="asyncio.loop.call_later">
  452. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">call_later</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">delay</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">callback</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">context</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.loop.call_later" title="Link to this definition">¶</a></dt>
  453. <dd><p>Schedule <em>callback</em> to be called after the given <em>delay</em>
  454. number of seconds (can be either an int or a float).</p>
  455. <p>An instance of <a class="reference internal" href="#asyncio.TimerHandle" title="asyncio.TimerHandle"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.TimerHandle</span></code></a> is returned which can
  456. be used to cancel the callback.</p>
  457. <p><em>callback</em> will be called exactly once. If two callbacks are
  458. scheduled for exactly the same time, the order in which they
  459. are called is undefined.</p>
  460. <p>The optional positional <em>args</em> will be passed to the callback when
  461. it is called. If you want the callback to be called with keyword
  462. arguments use <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a>.</p>
  463. <p>An optional keyword-only <em>context</em> argument allows specifying a
  464. custom <a class="reference internal" href="contextvars.html#contextvars.Context" title="contextvars.Context"><code class="xref py py-class docutils literal notranslate"><span class="pre">contextvars.Context</span></code></a> for the <em>callback</em> to run in.
  465. The current context is used when no <em>context</em> is provided.</p>
  466. <div class="versionchanged">
  467. <p><span class="versionmodified changed">Changed in version 3.7: </span>The <em>context</em> keyword-only parameter was added. See <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0567/"><strong>PEP 567</strong></a>
  468. for more details.</p>
  469. </div>
  470. <div class="versionchanged">
  471. <p><span class="versionmodified changed">Changed in version 3.8: </span>In Python 3.7 and earlier with the default event loop implementation,
  472. the <em>delay</em> could not exceed one day.
  473. This has been fixed in Python 3.8.</p>
  474. </div>
  475. </dd></dl>
  476. <dl class="py method">
  477. <dt class="sig sig-object py" id="asyncio.loop.call_at">
  478. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">call_at</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">when</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">callback</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">context</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.loop.call_at" title="Link to this definition">¶</a></dt>
  479. <dd><p>Schedule <em>callback</em> to be called at the given absolute timestamp
  480. <em>when</em> (an int or a float), using the same time reference as
  481. <a class="reference internal" href="#asyncio.loop.time" title="asyncio.loop.time"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.time()</span></code></a>.</p>
  482. <p>This method’s behavior is the same as <a class="reference internal" href="#asyncio.loop.call_later" title="asyncio.loop.call_later"><code class="xref py py-meth docutils literal notranslate"><span class="pre">call_later()</span></code></a>.</p>
  483. <p>An instance of <a class="reference internal" href="#asyncio.TimerHandle" title="asyncio.TimerHandle"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.TimerHandle</span></code></a> is returned which can
  484. be used to cancel the callback.</p>
  485. <div class="versionchanged">
  486. <p><span class="versionmodified changed">Changed in version 3.7: </span>The <em>context</em> keyword-only parameter was added. See <span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0567/"><strong>PEP 567</strong></a>
  487. for more details.</p>
  488. </div>
  489. <div class="versionchanged">
  490. <p><span class="versionmodified changed">Changed in version 3.8: </span>In Python 3.7 and earlier with the default event loop implementation,
  491. the difference between <em>when</em> and the current time could not exceed
  492. one day. This has been fixed in Python 3.8.</p>
  493. </div>
  494. </dd></dl>
  495. <dl class="py method">
  496. <dt class="sig sig-object py" id="asyncio.loop.time">
  497. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">time</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.time" title="Link to this definition">¶</a></dt>
  498. <dd><p>Return the current time, as a <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> value, according to
  499. the event loop’s internal monotonic clock.</p>
  500. </dd></dl>
  501. <div class="admonition note">
  502. <p class="admonition-title">Note</p>
  503. <div class="versionchanged">
  504. <p><span class="versionmodified changed">Changed in version 3.8: </span>In Python 3.7 and earlier timeouts (relative <em>delay</em> or absolute <em>when</em>)
  505. should not exceed one day. This has been fixed in Python 3.8.</p>
  506. </div>
  507. </div>
  508. <div class="admonition seealso">
  509. <p class="admonition-title">See also</p>
  510. <p>The <a class="reference internal" href="asyncio-task.html#asyncio.sleep" title="asyncio.sleep"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.sleep()</span></code></a> function.</p>
  511. </div>
  512. </section>
  513. <section id="creating-futures-and-tasks">
  514. <h3><a class="toc-backref" href="#id4" role="doc-backlink">Creating Futures and Tasks</a><a class="headerlink" href="#creating-futures-and-tasks" title="Link to this heading">¶</a></h3>
  515. <dl class="py method">
  516. <dt class="sig sig-object py" id="asyncio.loop.create_future">
  517. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">create_future</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.create_future" title="Link to this definition">¶</a></dt>
  518. <dd><p>Create an <a class="reference internal" href="asyncio-future.html#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Future</span></code></a> object attached to the event loop.</p>
  519. <p>This is the preferred way to create Futures in asyncio. This lets
  520. third-party event loops provide alternative implementations of
  521. the Future object (with better performance or instrumentation).</p>
  522. <div class="versionadded">
  523. <p><span class="versionmodified added">New in version 3.5.2.</span></p>
  524. </div>
  525. </dd></dl>
  526. <dl class="py method">
  527. <dt class="sig sig-object py" id="asyncio.loop.create_task">
  528. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">create_task</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">coro</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</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">context</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.loop.create_task" title="Link to this definition">¶</a></dt>
  529. <dd><p>Schedule the execution of <a class="reference internal" href="asyncio-task.html#coroutine"><span class="std std-ref">coroutine</span></a> <em>coro</em>.
  530. Return a <a class="reference internal" href="asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal notranslate"><span class="pre">Task</span></code></a> object.</p>
  531. <p>Third-party event loops can use their own subclass of <a class="reference internal" href="asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal notranslate"><span class="pre">Task</span></code></a>
  532. for interoperability. In this case, the result type is a subclass
  533. of <a class="reference internal" href="asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal notranslate"><span class="pre">Task</span></code></a>.</p>
  534. <p>If the <em>name</em> argument is provided and not <code class="docutils literal notranslate"><span class="pre">None</span></code>, it is set as
  535. the name of the task using <a class="reference internal" href="asyncio-task.html#asyncio.Task.set_name" title="asyncio.Task.set_name"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Task.set_name()</span></code></a>.</p>
  536. <p>An optional keyword-only <em>context</em> argument allows specifying a
  537. custom <a class="reference internal" href="contextvars.html#contextvars.Context" title="contextvars.Context"><code class="xref py py-class docutils literal notranslate"><span class="pre">contextvars.Context</span></code></a> for the <em>coro</em> to run in.
  538. The current context copy is created when no <em>context</em> is provided.</p>
  539. <div class="versionchanged">
  540. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added the <em>name</em> parameter.</p>
  541. </div>
  542. <div class="versionchanged">
  543. <p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>context</em> parameter.</p>
  544. </div>
  545. </dd></dl>
  546. <dl class="py method">
  547. <dt class="sig sig-object py" id="asyncio.loop.set_task_factory">
  548. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">set_task_factory</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">factory</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.set_task_factory" title="Link to this definition">¶</a></dt>
  549. <dd><p>Set a task factory that will be used by
  550. <a class="reference internal" href="#asyncio.loop.create_task" title="asyncio.loop.create_task"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_task()</span></code></a>.</p>
  551. <p>If <em>factory</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> the default task factory will be set.
  552. Otherwise, <em>factory</em> must be a <em>callable</em> with the signature matching
  553. <code class="docutils literal notranslate"><span class="pre">(loop,</span> <span class="pre">coro,</span> <span class="pre">context=None)</span></code>, where <em>loop</em> is a reference to the active
  554. event loop, and <em>coro</em> is a coroutine object. The callable
  555. must return a <a class="reference internal" href="asyncio-future.html#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Future</span></code></a>-compatible object.</p>
  556. </dd></dl>
  557. <dl class="py method">
  558. <dt class="sig sig-object py" id="asyncio.loop.get_task_factory">
  559. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">get_task_factory</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.get_task_factory" title="Link to this definition">¶</a></dt>
  560. <dd><p>Return a task factory or <code class="docutils literal notranslate"><span class="pre">None</span></code> if the default one is in use.</p>
  561. </dd></dl>
  562. </section>
  563. <section id="opening-network-connections">
  564. <h3><a class="toc-backref" href="#id5" role="doc-backlink">Opening network connections</a><a class="headerlink" href="#opening-network-connections" title="Link to this heading">¶</a></h3>
  565. <dl class="py method">
  566. <dt class="sig sig-object py" id="asyncio.loop.create_connection">
  567. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">create_connection</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">protocol_factory</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">host</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">port</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></em>, <em class="sig-param"><span class="n"><span class="pre">ssl</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">family</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">proto</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sock</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">local_addr</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">server_hostname</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">ssl_handshake_timeout</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">ssl_shutdown_timeout</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">happy_eyeballs_delay</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">interleave</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">all_errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.create_connection" title="Link to this definition">¶</a></dt>
  568. <dd><p>Open a streaming transport connection to a given
  569. address specified by <em>host</em> and <em>port</em>.</p>
  570. <p>The socket family can be either <a class="reference internal" href="socket.html#socket.AF_INET" title="socket.AF_INET"><code class="xref py py-const docutils literal notranslate"><span class="pre">AF_INET</span></code></a> or
  571. <a class="reference internal" href="socket.html#socket.AF_INET6" title="socket.AF_INET6"><code class="xref py py-const docutils literal notranslate"><span class="pre">AF_INET6</span></code></a> depending on <em>host</em> (or the <em>family</em>
  572. argument, if provided).</p>
  573. <p>The socket type will be <a class="reference internal" href="socket.html#socket.SOCK_STREAM" title="socket.SOCK_STREAM"><code class="xref py py-const docutils literal notranslate"><span class="pre">SOCK_STREAM</span></code></a>.</p>
  574. <p><em>protocol_factory</em> must be a callable returning an
  575. <a class="reference internal" href="asyncio-protocol.html#asyncio-protocol"><span class="std std-ref">asyncio protocol</span></a> implementation.</p>
  576. <p>This method will try to establish the connection in the background.
  577. When successful, it returns a <code class="docutils literal notranslate"><span class="pre">(transport,</span> <span class="pre">protocol)</span></code> pair.</p>
  578. <p>The chronological synopsis of the underlying operation is as follows:</p>
  579. <ol class="arabic simple">
  580. <li><p>The connection is established and a <a class="reference internal" href="asyncio-protocol.html#asyncio-transport"><span class="std std-ref">transport</span></a>
  581. is created for it.</p></li>
  582. <li><p><em>protocol_factory</em> is called without arguments and is expected to
  583. return a <a class="reference internal" href="asyncio-protocol.html#asyncio-protocol"><span class="std std-ref">protocol</span></a> instance.</p></li>
  584. <li><p>The protocol instance is coupled with the transport by calling its
  585. <a class="reference internal" href="asyncio-protocol.html#asyncio.BaseProtocol.connection_made" title="asyncio.BaseProtocol.connection_made"><code class="xref py py-meth docutils literal notranslate"><span class="pre">connection_made()</span></code></a> method.</p></li>
  586. <li><p>A <code class="docutils literal notranslate"><span class="pre">(transport,</span> <span class="pre">protocol)</span></code> tuple is returned on success.</p></li>
  587. </ol>
  588. <p>The created transport is an implementation-dependent bidirectional
  589. stream.</p>
  590. <p>Other arguments:</p>
  591. <ul>
  592. <li><p><em>ssl</em>: if given and not false, a SSL/TLS transport is created
  593. (by default a plain TCP transport is created). If <em>ssl</em> is
  594. a <a class="reference internal" href="ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code></a> object, this context is used to create
  595. the transport; if <em>ssl</em> is <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>, a default context returned
  596. from <a class="reference internal" href="ssl.html#ssl.create_default_context" title="ssl.create_default_context"><code class="xref py py-func docutils literal notranslate"><span class="pre">ssl.create_default_context()</span></code></a> is used.</p>
  597. <div class="admonition seealso">
  598. <p class="admonition-title">See also</p>
  599. <p><a class="reference internal" href="ssl.html#ssl-security"><span class="std std-ref">SSL/TLS security considerations</span></a></p>
  600. </div>
  601. </li>
  602. <li><p><em>server_hostname</em> sets or overrides the hostname that the target
  603. server’s certificate will be matched against. Should only be passed
  604. if <em>ssl</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>. By default the value of the <em>host</em> argument
  605. is used. If <em>host</em> is empty, there is no default and you must pass a
  606. value for <em>server_hostname</em>. If <em>server_hostname</em> is an empty
  607. string, hostname matching is disabled (which is a serious security
  608. risk, allowing for potential man-in-the-middle attacks).</p></li>
  609. <li><p><em>family</em>, <em>proto</em>, <em>flags</em> are the optional address family, protocol
  610. and flags to be passed through to getaddrinfo() for <em>host</em> resolution.
  611. If given, these should all be integers from the corresponding
  612. <a class="reference internal" href="socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> module constants.</p></li>
  613. <li><p><em>happy_eyeballs_delay</em>, if given, enables Happy Eyeballs for this
  614. connection. It should
  615. be a floating-point number representing the amount of time in seconds
  616. to wait for a connection attempt to complete, before starting the next
  617. attempt in parallel. This is the “Connection Attempt Delay” as defined
  618. in <span class="target" id="index-3"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc8305.html"><strong>RFC 8305</strong></a>. A sensible default value recommended by the RFC is <code class="docutils literal notranslate"><span class="pre">0.25</span></code>
  619. (250 milliseconds).</p></li>
  620. <li><p><em>interleave</em> controls address reordering when a host name resolves to
  621. multiple IP addresses.
  622. If <code class="docutils literal notranslate"><span class="pre">0</span></code> or unspecified, no reordering is done, and addresses are
  623. tried in the order returned by <a class="reference internal" href="#asyncio.loop.getaddrinfo" title="asyncio.loop.getaddrinfo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getaddrinfo()</span></code></a>. If a positive integer
  624. is specified, the addresses are interleaved by address family, and the
  625. given integer is interpreted as “First Address Family Count” as defined
  626. in <span class="target" id="index-4"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc8305.html"><strong>RFC 8305</strong></a>. The default is <code class="docutils literal notranslate"><span class="pre">0</span></code> if <em>happy_eyeballs_delay</em> is not
  627. specified, and <code class="docutils literal notranslate"><span class="pre">1</span></code> if it is.</p></li>
  628. <li><p><em>sock</em>, if given, should be an existing, already connected
  629. <a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal notranslate"><span class="pre">socket.socket</span></code></a> object to be used by the transport.
  630. If <em>sock</em> is given, none of <em>host</em>, <em>port</em>, <em>family</em>, <em>proto</em>, <em>flags</em>,
  631. <em>happy_eyeballs_delay</em>, <em>interleave</em>
  632. and <em>local_addr</em> should be specified.</p>
  633. <div class="admonition note">
  634. <p class="admonition-title">Note</p>
  635. <p>The <em>sock</em> argument transfers ownership of the socket to the
  636. transport created. To close the socket, call the transport’s
  637. <a class="reference internal" href="asyncio-protocol.html#asyncio.BaseTransport.close" title="asyncio.BaseTransport.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> method.</p>
  638. </div>
  639. </li>
  640. <li><p><em>local_addr</em>, if given, is a <code class="docutils literal notranslate"><span class="pre">(local_host,</span> <span class="pre">local_port)</span></code> tuple used
  641. to bind the socket locally. The <em>local_host</em> and <em>local_port</em>
  642. are looked up using <code class="docutils literal notranslate"><span class="pre">getaddrinfo()</span></code>, similarly to <em>host</em> and <em>port</em>.</p></li>
  643. <li><p><em>ssl_handshake_timeout</em> is (for a TLS connection) the time in seconds
  644. to wait for the TLS handshake to complete before aborting the connection.
  645. <code class="docutils literal notranslate"><span class="pre">60.0</span></code> seconds if <code class="docutils literal notranslate"><span class="pre">None</span></code> (default).</p></li>
  646. <li><p><em>ssl_shutdown_timeout</em> is the time in seconds to wait for the SSL shutdown
  647. to complete before aborting the connection. <code class="docutils literal notranslate"><span class="pre">30.0</span></code> seconds if <code class="docutils literal notranslate"><span class="pre">None</span></code>
  648. (default).</p></li>
  649. <li><p><em>all_errors</em> determines what exceptions are raised when a connection cannot
  650. be created. By default, only a single <code class="docutils literal notranslate"><span class="pre">Exception</span></code> is raised: the first
  651. exception if there is only one or all errors have same message, or a single
  652. <code class="docutils literal notranslate"><span class="pre">OSError</span></code> with the error messages combined. When <code class="docutils literal notranslate"><span class="pre">all_errors</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code>,
  653. an <code class="docutils literal notranslate"><span class="pre">ExceptionGroup</span></code> will be raised containing all exceptions (even if there
  654. is only one).</p></li>
  655. </ul>
  656. <div class="versionchanged">
  657. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added support for SSL/TLS in <a class="reference internal" href="#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProactorEventLoop</span></code></a>.</p>
  658. </div>
  659. <div class="versionchanged">
  660. <p><span class="versionmodified changed">Changed in version 3.6: </span>The socket option <a class="reference internal" href="socket.html#socket-unix-constants"><span class="std std-ref">socket.TCP_NODELAY</span></a> is set by default
  661. for all TCP connections.</p>
  662. </div>
  663. <div class="versionchanged">
  664. <p><span class="versionmodified changed">Changed in version 3.7: </span>Added the <em>ssl_handshake_timeout</em> parameter.</p>
  665. </div>
  666. <div class="versionchanged">
  667. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added the <em>happy_eyeballs_delay</em> and <em>interleave</em> parameters.</p>
  668. <p>Happy Eyeballs Algorithm: Success with Dual-Stack Hosts.
  669. When a server’s IPv4 path and protocol are working, but the server’s
  670. IPv6 path and protocol are not working, a dual-stack client
  671. application experiences significant connection delay compared to an
  672. IPv4-only client. This is undesirable because it causes the
  673. dual-stack client to have a worse user experience. This document
  674. specifies requirements for algorithms that reduce this user-visible
  675. delay and provides an algorithm.</p>
  676. <p>For more information: <a class="reference external" href="https://datatracker.ietf.org/doc/html/rfc6555">https://datatracker.ietf.org/doc/html/rfc6555</a></p>
  677. </div>
  678. <div class="versionchanged">
  679. <p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>ssl_shutdown_timeout</em> parameter.</p>
  680. </div>
  681. <div class="versionchanged">
  682. <p><span class="versionmodified changed">Changed in version 3.12: </span><em>all_errors</em> was added.</p>
  683. </div>
  684. <div class="admonition seealso">
  685. <p class="admonition-title">See also</p>
  686. <p>The <a class="reference internal" href="asyncio-stream.html#asyncio.open_connection" title="asyncio.open_connection"><code class="xref py py-func docutils literal notranslate"><span class="pre">open_connection()</span></code></a> function is a high-level alternative
  687. API. It returns a pair of (<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="asyncio-stream.html#asyncio.StreamWriter" title="asyncio.StreamWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code></a>)
  688. that can be used directly in async/await code.</p>
  689. </div>
  690. </dd></dl>
  691. <dl class="py method">
  692. <dt class="sig sig-object py" id="asyncio.loop.create_datagram_endpoint">
  693. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">create_datagram_endpoint</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">protocol_factory</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">local_addr</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">remote_addr</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></em>, <em class="sig-param"><span class="n"><span class="pre">family</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">proto</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">reuse_port</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">allow_broadcast</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">sock</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.loop.create_datagram_endpoint" title="Link to this definition">¶</a></dt>
  694. <dd><p>Create a datagram connection.</p>
  695. <p>The socket family can be either <a class="reference internal" href="socket.html#socket.AF_INET" title="socket.AF_INET"><code class="xref py py-const docutils literal notranslate"><span class="pre">AF_INET</span></code></a>,
  696. <a class="reference internal" href="socket.html#socket.AF_INET6" title="socket.AF_INET6"><code class="xref py py-const docutils literal notranslate"><span class="pre">AF_INET6</span></code></a>, or <a class="reference internal" href="socket.html#socket.AF_UNIX" title="socket.AF_UNIX"><code class="xref py py-const docutils literal notranslate"><span class="pre">AF_UNIX</span></code></a>,
  697. depending on <em>host</em> (or the <em>family</em> argument, if provided).</p>
  698. <p>The socket type will be <a class="reference internal" href="socket.html#socket.SOCK_DGRAM" title="socket.SOCK_DGRAM"><code class="xref py py-const docutils literal notranslate"><span class="pre">SOCK_DGRAM</span></code></a>.</p>
  699. <p><em>protocol_factory</em> must be a callable returning a
  700. <a class="reference internal" href="asyncio-protocol.html#asyncio-protocol"><span class="std std-ref">protocol</span></a> implementation.</p>
  701. <p>A tuple of <code class="docutils literal notranslate"><span class="pre">(transport,</span> <span class="pre">protocol)</span></code> is returned on success.</p>
  702. <p>Other arguments:</p>
  703. <ul>
  704. <li><p><em>local_addr</em>, if given, is a <code class="docutils literal notranslate"><span class="pre">(local_host,</span> <span class="pre">local_port)</span></code> tuple used
  705. to bind the socket locally. The <em>local_host</em> and <em>local_port</em>
  706. are looked up using <a class="reference internal" href="#asyncio.loop.getaddrinfo" title="asyncio.loop.getaddrinfo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getaddrinfo()</span></code></a>.</p></li>
  707. <li><p><em>remote_addr</em>, if given, is a <code class="docutils literal notranslate"><span class="pre">(remote_host,</span> <span class="pre">remote_port)</span></code> tuple used
  708. to connect the socket to a remote address. The <em>remote_host</em> and
  709. <em>remote_port</em> are looked up using <a class="reference internal" href="#asyncio.loop.getaddrinfo" title="asyncio.loop.getaddrinfo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getaddrinfo()</span></code></a>.</p></li>
  710. <li><p><em>family</em>, <em>proto</em>, <em>flags</em> are the optional address family, protocol
  711. and flags to be passed through to <a class="reference internal" href="#asyncio.loop.getaddrinfo" title="asyncio.loop.getaddrinfo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getaddrinfo()</span></code></a> for <em>host</em>
  712. resolution. If given, these should all be integers from the
  713. corresponding <a class="reference internal" href="socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> module constants.</p></li>
  714. <li><p><em>reuse_port</em> tells the kernel to allow this endpoint to be bound to the
  715. same port as other existing endpoints are bound to, so long as they all
  716. set this flag when being created. This option is not supported on Windows
  717. and some Unixes. If the <a class="reference internal" href="socket.html#socket-unix-constants"><span class="std std-ref">socket.SO_REUSEPORT</span></a> constant is not
  718. defined then this capability is unsupported.</p></li>
  719. <li><p><em>allow_broadcast</em> tells the kernel to allow this endpoint to send
  720. messages to the broadcast address.</p></li>
  721. <li><p><em>sock</em> can optionally be specified in order to use a preexisting,
  722. already connected, <a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal notranslate"><span class="pre">socket.socket</span></code></a> object to be used by the
  723. transport. If specified, <em>local_addr</em> and <em>remote_addr</em> should be omitted
  724. (must be <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>).</p>
  725. <div class="admonition note">
  726. <p class="admonition-title">Note</p>
  727. <p>The <em>sock</em> argument transfers ownership of the socket to the
  728. transport created. To close the socket, call the transport’s
  729. <a class="reference internal" href="asyncio-protocol.html#asyncio.BaseTransport.close" title="asyncio.BaseTransport.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> method.</p>
  730. </div>
  731. </li>
  732. </ul>
  733. <p>See <a class="reference internal" href="asyncio-protocol.html#asyncio-udp-echo-client-protocol"><span class="std std-ref">UDP echo client protocol</span></a> and
  734. <a class="reference internal" href="asyncio-protocol.html#asyncio-udp-echo-server-protocol"><span class="std std-ref">UDP echo server protocol</span></a> examples.</p>
  735. <div class="versionchanged">
  736. <p><span class="versionmodified changed">Changed in version 3.4.4: </span>The <em>family</em>, <em>proto</em>, <em>flags</em>, <em>reuse_address</em>, <em>reuse_port</em>,
  737. <em>allow_broadcast</em>, and <em>sock</em> parameters were added.</p>
  738. </div>
  739. <div class="versionchanged">
  740. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added support for Windows.</p>
  741. </div>
  742. <div class="versionchanged">
  743. <p><span class="versionmodified changed">Changed in version 3.8.1: </span>The <em>reuse_address</em> parameter is no longer supported, as using
  744. <a class="reference internal" href="socket.html#socket-unix-constants"><span class="std std-ref">socket.SO_REUSEADDR</span></a>
  745. poses a significant security concern for
  746. UDP. Explicitly passing <code class="docutils literal notranslate"><span class="pre">reuse_address=True</span></code> will raise an exception.</p>
  747. <p>When multiple processes with differing UIDs assign sockets to an
  748. identical UDP socket address with <code class="docutils literal notranslate"><span class="pre">SO_REUSEADDR</span></code>, incoming packets can
  749. become randomly distributed among the sockets.</p>
  750. <p>For supported platforms, <em>reuse_port</em> can be used as a replacement for
  751. similar functionality. With <em>reuse_port</em>,
  752. <a class="reference internal" href="socket.html#socket-unix-constants"><span class="std std-ref">socket.SO_REUSEPORT</span></a>
  753. is used instead, which specifically
  754. prevents processes with differing UIDs from assigning sockets to the same
  755. socket address.</p>
  756. </div>
  757. <div class="versionchanged">
  758. <p><span class="versionmodified changed">Changed in version 3.11: </span>The <em>reuse_address</em> parameter, disabled since Python 3.8.1,
  759. 3.7.6 and 3.6.10, has been entirely removed.</p>
  760. </div>
  761. </dd></dl>
  762. <dl class="py method">
  763. <dt class="sig sig-object py" id="asyncio.loop.create_unix_connection">
  764. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">create_unix_connection</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">protocol_factory</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path</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></em>, <em class="sig-param"><span class="n"><span class="pre">ssl</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">sock</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">server_hostname</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">ssl_handshake_timeout</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">ssl_shutdown_timeout</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.loop.create_unix_connection" title="Link to this definition">¶</a></dt>
  765. <dd><p>Create a Unix connection.</p>
  766. <p>The socket family will be <a class="reference internal" href="socket.html#socket.AF_UNIX" title="socket.AF_UNIX"><code class="xref py py-const docutils literal notranslate"><span class="pre">AF_UNIX</span></code></a>; socket
  767. type will be <a class="reference internal" href="socket.html#socket.SOCK_STREAM" title="socket.SOCK_STREAM"><code class="xref py py-const docutils literal notranslate"><span class="pre">SOCK_STREAM</span></code></a>.</p>
  768. <p>A tuple of <code class="docutils literal notranslate"><span class="pre">(transport,</span> <span class="pre">protocol)</span></code> is returned on success.</p>
  769. <p><em>path</em> is the name of a Unix domain socket and is required,
  770. unless a <em>sock</em> parameter is specified. Abstract Unix sockets,
  771. <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>, <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>, and <a class="reference internal" href="pathlib.html#pathlib.Path" title="pathlib.Path"><code class="xref py py-class docutils literal notranslate"><span class="pre">Path</span></code></a> paths are
  772. supported.</p>
  773. <p>See the documentation of the <a class="reference internal" href="#asyncio.loop.create_connection" title="asyncio.loop.create_connection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_connection()</span></code></a> method
  774. for information about arguments to this method.</p>
  775. <div class="availability docutils container">
  776. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  777. </div>
  778. <div class="versionchanged">
  779. <p><span class="versionmodified changed">Changed in version 3.7: </span>Added the <em>ssl_handshake_timeout</em> parameter.
  780. The <em>path</em> parameter can now be a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  781. </div>
  782. <div class="versionchanged">
  783. <p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>ssl_shutdown_timeout</em> parameter.</p>
  784. </div>
  785. </dd></dl>
  786. </section>
  787. <section id="creating-network-servers">
  788. <h3><a class="toc-backref" href="#id6" role="doc-backlink">Creating network servers</a><a class="headerlink" href="#creating-network-servers" title="Link to this heading">¶</a></h3>
  789. <dl class="py method" id="loop-create-server">
  790. <dt class="sig sig-object py" id="asyncio.loop.create_server">
  791. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">create_server</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">protocol_factory</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">host</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">port</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></em>, <em class="sig-param"><span class="n"><span class="pre">family</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">socket.AF_UNSPEC</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">socket.AI_PASSIVE</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sock</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">backlog</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">100</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ssl</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">reuse_address</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">reuse_port</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">ssl_handshake_timeout</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">ssl_shutdown_timeout</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">start_serving</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.create_server" title="Link to this definition">¶</a></dt>
  792. <dd><p>Create a TCP server (socket type <a class="reference internal" href="socket.html#socket.SOCK_STREAM" title="socket.SOCK_STREAM"><code class="xref py py-const docutils literal notranslate"><span class="pre">SOCK_STREAM</span></code></a>) listening
  793. on <em>port</em> of the <em>host</em> address.</p>
  794. <p>Returns a <a class="reference internal" href="#asyncio.Server" title="asyncio.Server"><code class="xref py py-class docutils literal notranslate"><span class="pre">Server</span></code></a> object.</p>
  795. <p>Arguments:</p>
  796. <ul>
  797. <li><p><em>protocol_factory</em> must be a callable returning a
  798. <a class="reference internal" href="asyncio-protocol.html#asyncio-protocol"><span class="std std-ref">protocol</span></a> implementation.</p></li>
  799. <li><p>The <em>host</em> parameter can be set to several types which determine where
  800. the server would be listening:</p>
  801. <ul class="simple">
  802. <li><p>If <em>host</em> is a string, the TCP server is bound to a single network
  803. interface specified by <em>host</em>.</p></li>
  804. <li><p>If <em>host</em> is a sequence of strings, the TCP server is bound to all
  805. network interfaces specified by the sequence.</p></li>
  806. <li><p>If <em>host</em> is an empty string or <code class="docutils literal notranslate"><span class="pre">None</span></code>, all interfaces are
  807. assumed and a list of multiple sockets will be returned (most likely
  808. one for IPv4 and another one for IPv6).</p></li>
  809. </ul>
  810. </li>
  811. <li><p>The <em>port</em> parameter can be set to specify which port the server should
  812. listen on. If <code class="docutils literal notranslate"><span class="pre">0</span></code> or <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default), a random unused port will
  813. be selected (note that if <em>host</em> resolves to multiple network interfaces,
  814. a different random port will be selected for each interface).</p></li>
  815. <li><p><em>family</em> can be set to either <a class="reference internal" href="socket.html#socket.AF_INET" title="socket.AF_INET"><code class="xref py py-const docutils literal notranslate"><span class="pre">socket.AF_INET</span></code></a> or
  816. <a class="reference internal" href="socket.html#socket.AF_INET6" title="socket.AF_INET6"><code class="xref py py-const docutils literal notranslate"><span class="pre">AF_INET6</span></code></a> to force the socket to use IPv4 or IPv6.
  817. If not set, the <em>family</em> will be determined from host name
  818. (defaults to <a class="reference internal" href="socket.html#socket.AF_UNSPEC" title="socket.AF_UNSPEC"><code class="xref py py-const docutils literal notranslate"><span class="pre">AF_UNSPEC</span></code></a>).</p></li>
  819. <li><p><em>flags</em> is a bitmask for <a class="reference internal" href="#asyncio.loop.getaddrinfo" title="asyncio.loop.getaddrinfo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getaddrinfo()</span></code></a>.</p></li>
  820. <li><p><em>sock</em> can optionally be specified in order to use a preexisting
  821. socket object. If specified, <em>host</em> and <em>port</em> must not be specified.</p>
  822. <div class="admonition note">
  823. <p class="admonition-title">Note</p>
  824. <p>The <em>sock</em> argument transfers ownership of the socket to the
  825. server created. To close the socket, call the server’s
  826. <a class="reference internal" href="#asyncio.Server.close" title="asyncio.Server.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> method.</p>
  827. </div>
  828. </li>
  829. <li><p><em>backlog</em> is the maximum number of queued connections passed to
  830. <a class="reference internal" href="socket.html#socket.socket.listen" title="socket.socket.listen"><code class="xref py py-meth docutils literal notranslate"><span class="pre">listen()</span></code></a> (defaults to 100).</p></li>
  831. <li><p><em>ssl</em> can be set to an <a class="reference internal" href="ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> instance to enable
  832. TLS over the accepted connections.</p></li>
  833. <li><p><em>reuse_address</em> tells the kernel to reuse a local socket in
  834. <code class="docutils literal notranslate"><span class="pre">TIME_WAIT</span></code> state, without waiting for its natural timeout to
  835. expire. If not specified will automatically be set to <code class="docutils literal notranslate"><span class="pre">True</span></code> on
  836. Unix.</p></li>
  837. <li><p><em>reuse_port</em> tells the kernel to allow this endpoint to be bound to the
  838. same port as other existing endpoints are bound to, so long as they all
  839. set this flag when being created. This option is not supported on
  840. Windows.</p></li>
  841. <li><p><em>ssl_handshake_timeout</em> is (for a TLS server) the time in seconds to wait
  842. for the TLS handshake to complete before aborting the connection.
  843. <code class="docutils literal notranslate"><span class="pre">60.0</span></code> seconds if <code class="docutils literal notranslate"><span class="pre">None</span></code> (default).</p></li>
  844. <li><p><em>ssl_shutdown_timeout</em> is the time in seconds to wait for the SSL shutdown
  845. to complete before aborting the connection. <code class="docutils literal notranslate"><span class="pre">30.0</span></code> seconds if <code class="docutils literal notranslate"><span class="pre">None</span></code>
  846. (default).</p></li>
  847. <li><p><em>start_serving</em> set to <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default) causes the created server
  848. to start accepting connections immediately. When set to <code class="docutils literal notranslate"><span class="pre">False</span></code>,
  849. the user should await on <a class="reference internal" href="#asyncio.Server.start_serving" title="asyncio.Server.start_serving"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Server.start_serving()</span></code></a> or
  850. <a class="reference internal" href="#asyncio.Server.serve_forever" title="asyncio.Server.serve_forever"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Server.serve_forever()</span></code></a> to make the server to start accepting
  851. connections.</p></li>
  852. </ul>
  853. <div class="versionchanged">
  854. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added support for SSL/TLS in <a class="reference internal" href="#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProactorEventLoop</span></code></a>.</p>
  855. </div>
  856. <div class="versionchanged">
  857. <p><span class="versionmodified changed">Changed in version 3.5.1: </span>The <em>host</em> parameter can be a sequence of strings.</p>
  858. </div>
  859. <div class="versionchanged">
  860. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added <em>ssl_handshake_timeout</em> and <em>start_serving</em> parameters.
  861. The socket option <a class="reference internal" href="socket.html#socket-unix-constants"><span class="std std-ref">socket.TCP_NODELAY</span></a> is set by default
  862. for all TCP connections.</p>
  863. </div>
  864. <div class="versionchanged">
  865. <p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>ssl_shutdown_timeout</em> parameter.</p>
  866. </div>
  867. <div class="admonition seealso">
  868. <p class="admonition-title">See also</p>
  869. <p>The <a class="reference internal" href="asyncio-stream.html#asyncio.start_server" title="asyncio.start_server"><code class="xref py py-func docutils literal notranslate"><span class="pre">start_server()</span></code></a> function is a higher-level alternative API
  870. that returns a pair of <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> and <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>
  871. that can be used in an async/await code.</p>
  872. </div>
  873. </dd></dl>
  874. <dl class="py method">
  875. <dt class="sig sig-object py" id="asyncio.loop.create_unix_server">
  876. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">create_unix_server</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">protocol_factory</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path</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></em>, <em class="sig-param"><span class="n"><span class="pre">sock</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">backlog</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">100</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ssl</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">ssl_handshake_timeout</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">ssl_shutdown_timeout</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">start_serving</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.create_unix_server" title="Link to this definition">¶</a></dt>
  877. <dd><p>Similar to <a class="reference internal" href="#asyncio.loop.create_server" title="asyncio.loop.create_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_server()</span></code></a> but works with the
  878. <a class="reference internal" href="socket.html#socket.AF_UNIX" title="socket.AF_UNIX"><code class="xref py py-const docutils literal notranslate"><span class="pre">AF_UNIX</span></code></a> socket family.</p>
  879. <p><em>path</em> is the name of a Unix domain socket, and is required,
  880. unless a <em>sock</em> argument is provided. Abstract Unix sockets,
  881. <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>, <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>, and <a class="reference internal" href="pathlib.html#pathlib.Path" title="pathlib.Path"><code class="xref py py-class docutils literal notranslate"><span class="pre">Path</span></code></a> paths
  882. are supported.</p>
  883. <p>See the documentation of the <a class="reference internal" href="#asyncio.loop.create_server" title="asyncio.loop.create_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_server()</span></code></a> method
  884. for information about arguments to this method.</p>
  885. <div class="availability docutils container">
  886. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  887. </div>
  888. <div class="versionchanged">
  889. <p><span class="versionmodified changed">Changed in version 3.7: </span>Added the <em>ssl_handshake_timeout</em> and <em>start_serving</em> parameters.
  890. The <em>path</em> parameter can now be a <a class="reference internal" href="pathlib.html#pathlib.Path" title="pathlib.Path"><code class="xref py py-class docutils literal notranslate"><span class="pre">Path</span></code></a> object.</p>
  891. </div>
  892. <div class="versionchanged">
  893. <p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>ssl_shutdown_timeout</em> parameter.</p>
  894. </div>
  895. </dd></dl>
  896. <dl class="py method">
  897. <dt class="sig sig-object py" id="asyncio.loop.connect_accepted_socket">
  898. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">connect_accepted_socket</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">protocol_factory</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sock</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ssl</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">ssl_handshake_timeout</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">ssl_shutdown_timeout</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.loop.connect_accepted_socket" title="Link to this definition">¶</a></dt>
  899. <dd><p>Wrap an already accepted connection into a transport/protocol pair.</p>
  900. <p>This method can be used by servers that accept connections outside
  901. of asyncio but that use asyncio to handle them.</p>
  902. <p>Parameters:</p>
  903. <ul>
  904. <li><p><em>protocol_factory</em> must be a callable returning a
  905. <a class="reference internal" href="asyncio-protocol.html#asyncio-protocol"><span class="std std-ref">protocol</span></a> implementation.</p></li>
  906. <li><p><em>sock</em> is a preexisting socket object returned from
  907. <a class="reference internal" href="socket.html#socket.socket.accept" title="socket.socket.accept"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.accept</span></code></a>.</p>
  908. <div class="admonition note">
  909. <p class="admonition-title">Note</p>
  910. <p>The <em>sock</em> argument transfers ownership of the socket to the
  911. transport created. To close the socket, call the transport’s
  912. <a class="reference internal" href="asyncio-protocol.html#asyncio.BaseTransport.close" title="asyncio.BaseTransport.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> method.</p>
  913. </div>
  914. </li>
  915. <li><p><em>ssl</em> can be set to an <a class="reference internal" href="ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> to enable SSL over
  916. the accepted connections.</p></li>
  917. <li><p><em>ssl_handshake_timeout</em> is (for an SSL connection) the time in seconds to
  918. wait for the SSL handshake to complete before aborting the connection.
  919. <code class="docutils literal notranslate"><span class="pre">60.0</span></code> seconds if <code class="docutils literal notranslate"><span class="pre">None</span></code> (default).</p></li>
  920. <li><p><em>ssl_shutdown_timeout</em> is the time in seconds to wait for the SSL shutdown
  921. to complete before aborting the connection. <code class="docutils literal notranslate"><span class="pre">30.0</span></code> seconds if <code class="docutils literal notranslate"><span class="pre">None</span></code>
  922. (default).</p></li>
  923. </ul>
  924. <p>Returns a <code class="docutils literal notranslate"><span class="pre">(transport,</span> <span class="pre">protocol)</span></code> pair.</p>
  925. <div class="versionadded">
  926. <p><span class="versionmodified added">New in version 3.5.3.</span></p>
  927. </div>
  928. <div class="versionchanged">
  929. <p><span class="versionmodified changed">Changed in version 3.7: </span>Added the <em>ssl_handshake_timeout</em> parameter.</p>
  930. </div>
  931. <div class="versionchanged">
  932. <p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>ssl_shutdown_timeout</em> parameter.</p>
  933. </div>
  934. </dd></dl>
  935. </section>
  936. <section id="transferring-files">
  937. <h3><a class="toc-backref" href="#id7" role="doc-backlink">Transferring files</a><a class="headerlink" href="#transferring-files" title="Link to this heading">¶</a></h3>
  938. <dl class="py method">
  939. <dt class="sig sig-object py" id="asyncio.loop.sendfile">
  940. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">sendfile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">transport</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">count</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></em>, <em class="sig-param"><span class="n"><span class="pre">fallback</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.sendfile" title="Link to this definition">¶</a></dt>
  941. <dd><p>Send a <em>file</em> over a <em>transport</em>. Return the total number of bytes
  942. sent.</p>
  943. <p>The method uses high-performance <a class="reference internal" href="os.html#os.sendfile" title="os.sendfile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">os.sendfile()</span></code></a> if available.</p>
  944. <p><em>file</em> must be a regular file object opened in binary mode.</p>
  945. <p><em>offset</em> tells from where to start reading the file. If specified,
  946. <em>count</em> is the total number of bytes to transmit as opposed to
  947. sending the file until EOF is reached. File position is always updated,
  948. even when this method raises an error, and
  949. <a class="reference internal" href="io.html#io.IOBase.tell" title="io.IOBase.tell"><code class="xref py py-meth docutils literal notranslate"><span class="pre">file.tell()</span></code></a> can be used to obtain the actual
  950. number of bytes sent.</p>
  951. <p><em>fallback</em> set to <code class="docutils literal notranslate"><span class="pre">True</span></code> makes asyncio to manually read and send
  952. the file when the platform does not support the sendfile system call
  953. (e.g. Windows or SSL socket on Unix).</p>
  954. <p>Raise <a class="reference internal" href="asyncio-exceptions.html#asyncio.SendfileNotAvailableError" title="asyncio.SendfileNotAvailableError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SendfileNotAvailableError</span></code></a> if the system does not support
  955. the <em>sendfile</em> syscall and <em>fallback</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  956. <div class="versionadded">
  957. <p><span class="versionmodified added">New in version 3.7.</span></p>
  958. </div>
  959. </dd></dl>
  960. </section>
  961. <section id="tls-upgrade">
  962. <h3><a class="toc-backref" href="#id8" role="doc-backlink">TLS Upgrade</a><a class="headerlink" href="#tls-upgrade" title="Link to this heading">¶</a></h3>
  963. <dl class="py method">
  964. <dt class="sig sig-object py" id="asyncio.loop.start_tls">
  965. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">start_tls</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">transport</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">protocol</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sslcontext</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">server_side</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">server_hostname</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">ssl_handshake_timeout</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">ssl_shutdown_timeout</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.loop.start_tls" title="Link to this definition">¶</a></dt>
  966. <dd><p>Upgrade an existing transport-based connection to TLS.</p>
  967. <p>Create a TLS coder/decoder instance and insert it between the <em>transport</em>
  968. and the <em>protocol</em>. The coder/decoder implements both <em>transport</em>-facing
  969. protocol and <em>protocol</em>-facing transport.</p>
  970. <p>Return the created two-interface instance. After <em>await</em>, the <em>protocol</em>
  971. must stop using the original <em>transport</em> and communicate with the returned
  972. object only because the coder caches <em>protocol</em>-side data and sporadically
  973. exchanges extra TLS session packets with <em>transport</em>.</p>
  974. <p>In some situations (e.g. when the passed transport is already closing) this
  975. may return <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  976. <p>Parameters:</p>
  977. <ul class="simple">
  978. <li><p><em>transport</em> and <em>protocol</em> instances that methods like
  979. <a class="reference internal" href="#asyncio.loop.create_server" title="asyncio.loop.create_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_server()</span></code></a> and
  980. <a class="reference internal" href="#asyncio.loop.create_connection" title="asyncio.loop.create_connection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_connection()</span></code></a> return.</p></li>
  981. <li><p><em>sslcontext</em>: a configured instance of <a class="reference internal" href="ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a>.</p></li>
  982. <li><p><em>server_side</em> pass <code class="docutils literal notranslate"><span class="pre">True</span></code> when a server-side connection is being
  983. upgraded (like the one created by <a class="reference internal" href="#asyncio.loop.create_server" title="asyncio.loop.create_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_server()</span></code></a>).</p></li>
  984. <li><p><em>server_hostname</em>: sets or overrides the host name that the target
  985. server’s certificate will be matched against.</p></li>
  986. <li><p><em>ssl_handshake_timeout</em> is (for a TLS connection) the time in seconds to
  987. wait for the TLS handshake to complete before aborting the connection.
  988. <code class="docutils literal notranslate"><span class="pre">60.0</span></code> seconds if <code class="docutils literal notranslate"><span class="pre">None</span></code> (default).</p></li>
  989. <li><p><em>ssl_shutdown_timeout</em> is the time in seconds to wait for the SSL shutdown
  990. to complete before aborting the connection. <code class="docutils literal notranslate"><span class="pre">30.0</span></code> seconds if <code class="docutils literal notranslate"><span class="pre">None</span></code>
  991. (default).</p></li>
  992. </ul>
  993. <div class="versionadded">
  994. <p><span class="versionmodified added">New in version 3.7.</span></p>
  995. </div>
  996. <div class="versionchanged">
  997. <p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>ssl_shutdown_timeout</em> parameter.</p>
  998. </div>
  999. </dd></dl>
  1000. </section>
  1001. <section id="watching-file-descriptors">
  1002. <h3><a class="toc-backref" href="#id9" role="doc-backlink">Watching file descriptors</a><a class="headerlink" href="#watching-file-descriptors" title="Link to this heading">¶</a></h3>
  1003. <dl class="py method">
  1004. <dt class="sig sig-object py" id="asyncio.loop.add_reader">
  1005. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">add_reader</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">callback</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><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.add_reader" title="Link to this definition">¶</a></dt>
  1006. <dd><p>Start monitoring the <em>fd</em> file descriptor for read availability and
  1007. invoke <em>callback</em> with the specified arguments once <em>fd</em> is available for
  1008. reading.</p>
  1009. </dd></dl>
  1010. <dl class="py method">
  1011. <dt class="sig sig-object py" id="asyncio.loop.remove_reader">
  1012. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">remove_reader</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.remove_reader" title="Link to this definition">¶</a></dt>
  1013. <dd><p>Stop monitoring the <em>fd</em> file descriptor for read availability. Returns
  1014. <code class="docutils literal notranslate"><span class="pre">True</span></code> if <em>fd</em> was previously being monitored for reads.</p>
  1015. </dd></dl>
  1016. <dl class="py method">
  1017. <dt class="sig sig-object py" id="asyncio.loop.add_writer">
  1018. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">add_writer</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">callback</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><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.add_writer" title="Link to this definition">¶</a></dt>
  1019. <dd><p>Start monitoring the <em>fd</em> file descriptor for write availability and
  1020. invoke <em>callback</em> with the specified arguments once <em>fd</em> is available for
  1021. writing.</p>
  1022. <p>Use <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a> <a class="reference internal" href="#asyncio-pass-keywords"><span class="std std-ref">to pass keyword arguments</span></a> to <em>callback</em>.</p>
  1023. </dd></dl>
  1024. <dl class="py method">
  1025. <dt class="sig sig-object py" id="asyncio.loop.remove_writer">
  1026. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">remove_writer</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.remove_writer" title="Link to this definition">¶</a></dt>
  1027. <dd><p>Stop monitoring the <em>fd</em> file descriptor for write availability. Returns
  1028. <code class="docutils literal notranslate"><span class="pre">True</span></code> if <em>fd</em> was previously being monitored for writes.</p>
  1029. </dd></dl>
  1030. <p>See also <a class="reference internal" href="asyncio-platforms.html#asyncio-platform-support"><span class="std std-ref">Platform Support</span></a> section
  1031. for some limitations of these methods.</p>
  1032. </section>
  1033. <section id="working-with-socket-objects-directly">
  1034. <h3><a class="toc-backref" href="#id10" role="doc-backlink">Working with socket objects directly</a><a class="headerlink" href="#working-with-socket-objects-directly" title="Link to this heading">¶</a></h3>
  1035. <p>In general, protocol implementations that use transport-based APIs
  1036. such as <a class="reference internal" href="#asyncio.loop.create_connection" title="asyncio.loop.create_connection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_connection()</span></code></a> and <a class="reference internal" href="#asyncio.loop.create_server" title="asyncio.loop.create_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_server()</span></code></a>
  1037. are faster than implementations that work with sockets directly.
  1038. However, there are some use cases when performance is not critical, and
  1039. working with <a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal notranslate"><span class="pre">socket</span></code></a> objects directly is more
  1040. convenient.</p>
  1041. <dl class="py method">
  1042. <dt class="sig sig-object py" id="asyncio.loop.sock_recv">
  1043. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">sock_recv</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sock</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">nbytes</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.sock_recv" title="Link to this definition">¶</a></dt>
  1044. <dd><p>Receive up to <em>nbytes</em> from <em>sock</em>. Asynchronous version of
  1045. <a class="reference internal" href="socket.html#socket.socket.recv" title="socket.socket.recv"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.recv()</span></code></a>.</p>
  1046. <p>Return the received data as a bytes object.</p>
  1047. <p><em>sock</em> must be a non-blocking socket.</p>
  1048. <div class="versionchanged">
  1049. <p><span class="versionmodified changed">Changed in version 3.7: </span>Even though this method was always documented as a coroutine
  1050. method, releases before Python 3.7 returned a <a class="reference internal" href="asyncio-future.html#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a>.
  1051. Since Python 3.7 this is an <code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code> method.</p>
  1052. </div>
  1053. </dd></dl>
  1054. <dl class="py method">
  1055. <dt class="sig sig-object py" id="asyncio.loop.sock_recv_into">
  1056. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">sock_recv_into</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sock</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buf</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.sock_recv_into" title="Link to this definition">¶</a></dt>
  1057. <dd><p>Receive data from <em>sock</em> into the <em>buf</em> buffer. Modeled after the blocking
  1058. <a class="reference internal" href="socket.html#socket.socket.recv_into" title="socket.socket.recv_into"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.recv_into()</span></code></a> method.</p>
  1059. <p>Return the number of bytes written to the buffer.</p>
  1060. <p><em>sock</em> must be a non-blocking socket.</p>
  1061. <div class="versionadded">
  1062. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1063. </div>
  1064. </dd></dl>
  1065. <dl class="py method">
  1066. <dt class="sig sig-object py" id="asyncio.loop.sock_recvfrom">
  1067. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">sock_recvfrom</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sock</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">bufsize</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.sock_recvfrom" title="Link to this definition">¶</a></dt>
  1068. <dd><p>Receive a datagram of up to <em>bufsize</em> from <em>sock</em>. Asynchronous version of
  1069. <a class="reference internal" href="socket.html#socket.socket.recvfrom" title="socket.socket.recvfrom"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.recvfrom()</span></code></a>.</p>
  1070. <p>Return a tuple of (received data, remote address).</p>
  1071. <p><em>sock</em> must be a non-blocking socket.</p>
  1072. <div class="versionadded">
  1073. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1074. </div>
  1075. </dd></dl>
  1076. <dl class="py method">
  1077. <dt class="sig sig-object py" id="asyncio.loop.sock_recvfrom_into">
  1078. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">sock_recvfrom_into</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sock</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buf</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">nbytes</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.sock_recvfrom_into" title="Link to this definition">¶</a></dt>
  1079. <dd><p>Receive a datagram of up to <em>nbytes</em> from <em>sock</em> into <em>buf</em>.
  1080. Asynchronous version of
  1081. <a class="reference internal" href="socket.html#socket.socket.recvfrom_into" title="socket.socket.recvfrom_into"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.recvfrom_into()</span></code></a>.</p>
  1082. <p>Return a tuple of (number of bytes received, remote address).</p>
  1083. <p><em>sock</em> must be a non-blocking socket.</p>
  1084. <div class="versionadded">
  1085. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1086. </div>
  1087. </dd></dl>
  1088. <dl class="py method">
  1089. <dt class="sig sig-object py" id="asyncio.loop.sock_sendall">
  1090. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">sock_sendall</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sock</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.sock_sendall" title="Link to this definition">¶</a></dt>
  1091. <dd><p>Send <em>data</em> to the <em>sock</em> socket. Asynchronous version of
  1092. <a class="reference internal" href="socket.html#socket.socket.sendall" title="socket.socket.sendall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.sendall()</span></code></a>.</p>
  1093. <p>This method continues to send to the socket until either all data
  1094. in <em>data</em> has been sent or an error occurs. <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned
  1095. on success. On error, an exception is raised. Additionally, there is no way
  1096. to determine how much data, if any, was successfully processed by the
  1097. receiving end of the connection.</p>
  1098. <p><em>sock</em> must be a non-blocking socket.</p>
  1099. <div class="versionchanged">
  1100. <p><span class="versionmodified changed">Changed in version 3.7: </span>Even though the method was always documented as a coroutine
  1101. method, before Python 3.7 it returned a <a class="reference internal" href="asyncio-future.html#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a>.
  1102. Since Python 3.7, this is an <code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code> method.</p>
  1103. </div>
  1104. </dd></dl>
  1105. <dl class="py method">
  1106. <dt class="sig sig-object py" id="asyncio.loop.sock_sendto">
  1107. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">sock_sendto</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sock</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">address</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.sock_sendto" title="Link to this definition">¶</a></dt>
  1108. <dd><p>Send a datagram from <em>sock</em> to <em>address</em>.
  1109. Asynchronous version of
  1110. <a class="reference internal" href="socket.html#socket.socket.sendto" title="socket.socket.sendto"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.sendto()</span></code></a>.</p>
  1111. <p>Return the number of bytes sent.</p>
  1112. <p><em>sock</em> must be a non-blocking socket.</p>
  1113. <div class="versionadded">
  1114. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1115. </div>
  1116. </dd></dl>
  1117. <dl class="py method">
  1118. <dt class="sig sig-object py" id="asyncio.loop.sock_connect">
  1119. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">sock_connect</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sock</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">address</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.sock_connect" title="Link to this definition">¶</a></dt>
  1120. <dd><p>Connect <em>sock</em> to a remote socket at <em>address</em>.</p>
  1121. <p>Asynchronous version of <a class="reference internal" href="socket.html#socket.socket.connect" title="socket.socket.connect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.connect()</span></code></a>.</p>
  1122. <p><em>sock</em> must be a non-blocking socket.</p>
  1123. <div class="versionchanged">
  1124. <p><span class="versionmodified changed">Changed in version 3.5.2: </span><code class="docutils literal notranslate"><span class="pre">address</span></code> no longer needs to be resolved. <code class="docutils literal notranslate"><span class="pre">sock_connect</span></code>
  1125. will try to check if the <em>address</em> is already resolved by calling
  1126. <a class="reference internal" href="socket.html#socket.inet_pton" title="socket.inet_pton"><code class="xref py py-func docutils literal notranslate"><span class="pre">socket.inet_pton()</span></code></a>. If not,
  1127. <a class="reference internal" href="#asyncio.loop.getaddrinfo" title="asyncio.loop.getaddrinfo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.getaddrinfo()</span></code></a> will be used to resolve the
  1128. <em>address</em>.</p>
  1129. </div>
  1130. <div class="admonition seealso">
  1131. <p class="admonition-title">See also</p>
  1132. <p><a class="reference internal" href="#asyncio.loop.create_connection" title="asyncio.loop.create_connection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_connection()</span></code></a>
  1133. and <a class="reference internal" href="asyncio-stream.html#asyncio.open_connection" title="asyncio.open_connection"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.open_connection()</span></code></a>.</p>
  1134. </div>
  1135. </dd></dl>
  1136. <dl class="py method">
  1137. <dt class="sig sig-object py" id="asyncio.loop.sock_accept">
  1138. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">sock_accept</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sock</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.sock_accept" title="Link to this definition">¶</a></dt>
  1139. <dd><p>Accept a connection. Modeled after the blocking
  1140. <a class="reference internal" href="socket.html#socket.socket.accept" title="socket.socket.accept"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.accept()</span></code></a> method.</p>
  1141. <p>The socket must be bound to an address and listening
  1142. for connections. The return value is a pair <code class="docutils literal notranslate"><span class="pre">(conn,</span> <span class="pre">address)</span></code> where <em>conn</em>
  1143. is a <em>new</em> socket object usable to send and receive data on the connection,
  1144. and <em>address</em> is the address bound to the socket on the other end of the
  1145. connection.</p>
  1146. <p><em>sock</em> must be a non-blocking socket.</p>
  1147. <div class="versionchanged">
  1148. <p><span class="versionmodified changed">Changed in version 3.7: </span>Even though the method was always documented as a coroutine
  1149. method, before Python 3.7 it returned a <a class="reference internal" href="asyncio-future.html#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a>.
  1150. Since Python 3.7, this is an <code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code> method.</p>
  1151. </div>
  1152. <div class="admonition seealso">
  1153. <p class="admonition-title">See also</p>
  1154. <p><a class="reference internal" href="#asyncio.loop.create_server" title="asyncio.loop.create_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_server()</span></code></a> and <a class="reference internal" href="asyncio-stream.html#asyncio.start_server" title="asyncio.start_server"><code class="xref py py-func docutils literal notranslate"><span class="pre">start_server()</span></code></a>.</p>
  1155. </div>
  1156. </dd></dl>
  1157. <dl class="py method">
  1158. <dt class="sig sig-object py" id="asyncio.loop.sock_sendfile">
  1159. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">sock_sendfile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sock</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">count</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></em>, <em class="sig-param"><span class="n"><span class="pre">fallback</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.sock_sendfile" title="Link to this definition">¶</a></dt>
  1160. <dd><p>Send a file using high-performance <a class="reference internal" href="os.html#os.sendfile" title="os.sendfile"><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.sendfile</span></code></a> if possible.
  1161. Return the total number of bytes sent.</p>
  1162. <p>Asynchronous version of <a class="reference internal" href="socket.html#socket.socket.sendfile" title="socket.socket.sendfile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.sendfile()</span></code></a>.</p>
  1163. <p><em>sock</em> must be a non-blocking <a class="reference internal" href="socket.html#socket.SOCK_STREAM" title="socket.SOCK_STREAM"><code class="xref py py-const docutils literal notranslate"><span class="pre">socket.SOCK_STREAM</span></code></a>
  1164. <a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal notranslate"><span class="pre">socket</span></code></a>.</p>
  1165. <p><em>file</em> must be a regular file object open in binary mode.</p>
  1166. <p><em>offset</em> tells from where to start reading the file. If specified,
  1167. <em>count</em> is the total number of bytes to transmit as opposed to
  1168. sending the file until EOF is reached. File position is always updated,
  1169. even when this method raises an error, and
  1170. <a class="reference internal" href="io.html#io.IOBase.tell" title="io.IOBase.tell"><code class="xref py py-meth docutils literal notranslate"><span class="pre">file.tell()</span></code></a> can be used to obtain the actual
  1171. number of bytes sent.</p>
  1172. <p><em>fallback</em>, when set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, makes asyncio manually read and send
  1173. the file when the platform does not support the sendfile syscall
  1174. (e.g. Windows or SSL socket on Unix).</p>
  1175. <p>Raise <a class="reference internal" href="asyncio-exceptions.html#asyncio.SendfileNotAvailableError" title="asyncio.SendfileNotAvailableError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SendfileNotAvailableError</span></code></a> if the system does not support
  1176. <em>sendfile</em> syscall and <em>fallback</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  1177. <p><em>sock</em> must be a non-blocking socket.</p>
  1178. <div class="versionadded">
  1179. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1180. </div>
  1181. </dd></dl>
  1182. </section>
  1183. <section id="dns">
  1184. <h3><a class="toc-backref" href="#id11" role="doc-backlink">DNS</a><a class="headerlink" href="#dns" title="Link to this heading">¶</a></h3>
  1185. <dl class="py method">
  1186. <dt class="sig sig-object py" id="asyncio.loop.getaddrinfo">
  1187. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">getaddrinfo</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">host</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">port</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">family</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">type</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">proto</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.getaddrinfo" title="Link to this definition">¶</a></dt>
  1188. <dd><p>Asynchronous version of <a class="reference internal" href="socket.html#socket.getaddrinfo" title="socket.getaddrinfo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.getaddrinfo()</span></code></a>.</p>
  1189. </dd></dl>
  1190. <dl class="py method">
  1191. <dt class="sig sig-object py" id="asyncio.loop.getnameinfo">
  1192. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">getnameinfo</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sockaddr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.getnameinfo" title="Link to this definition">¶</a></dt>
  1193. <dd><p>Asynchronous version of <a class="reference internal" href="socket.html#socket.getnameinfo" title="socket.getnameinfo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.getnameinfo()</span></code></a>.</p>
  1194. </dd></dl>
  1195. <div class="versionchanged">
  1196. <p><span class="versionmodified changed">Changed in version 3.7: </span>Both <em>getaddrinfo</em> and <em>getnameinfo</em> methods were always documented
  1197. to return a coroutine, but prior to Python 3.7 they were, in fact,
  1198. returning <a class="reference internal" href="asyncio-future.html#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Future</span></code></a> objects. Starting with Python 3.7
  1199. both methods are coroutines.</p>
  1200. </div>
  1201. </section>
  1202. <section id="working-with-pipes">
  1203. <h3><a class="toc-backref" href="#id12" role="doc-backlink">Working with pipes</a><a class="headerlink" href="#working-with-pipes" title="Link to this heading">¶</a></h3>
  1204. <dl class="py method">
  1205. <dt class="sig sig-object py" id="asyncio.loop.connect_read_pipe">
  1206. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">connect_read_pipe</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">protocol_factory</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pipe</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.connect_read_pipe" title="Link to this definition">¶</a></dt>
  1207. <dd><p>Register the read end of <em>pipe</em> in the event loop.</p>
  1208. <p><em>protocol_factory</em> must be a callable returning an
  1209. <a class="reference internal" href="asyncio-protocol.html#asyncio-protocol"><span class="std std-ref">asyncio protocol</span></a> implementation.</p>
  1210. <p><em>pipe</em> is a <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file-like object</span></a>.</p>
  1211. <p>Return pair <code class="docutils literal notranslate"><span class="pre">(transport,</span> <span class="pre">protocol)</span></code>, where <em>transport</em> supports
  1212. the <a class="reference internal" href="asyncio-protocol.html#asyncio.ReadTransport" title="asyncio.ReadTransport"><code class="xref py py-class docutils literal notranslate"><span class="pre">ReadTransport</span></code></a> interface and <em>protocol</em> is an object
  1213. instantiated by the <em>protocol_factory</em>.</p>
  1214. <p>With <a class="reference internal" href="#asyncio.SelectorEventLoop" title="asyncio.SelectorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectorEventLoop</span></code></a> event loop, the <em>pipe</em> is set to
  1215. non-blocking mode.</p>
  1216. </dd></dl>
  1217. <dl class="py method">
  1218. <dt class="sig sig-object py" id="asyncio.loop.connect_write_pipe">
  1219. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">connect_write_pipe</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">protocol_factory</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pipe</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.connect_write_pipe" title="Link to this definition">¶</a></dt>
  1220. <dd><p>Register the write end of <em>pipe</em> in the event loop.</p>
  1221. <p><em>protocol_factory</em> must be a callable returning an
  1222. <a class="reference internal" href="asyncio-protocol.html#asyncio-protocol"><span class="std std-ref">asyncio protocol</span></a> implementation.</p>
  1223. <p><em>pipe</em> is <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file-like object</span></a>.</p>
  1224. <p>Return pair <code class="docutils literal notranslate"><span class="pre">(transport,</span> <span class="pre">protocol)</span></code>, where <em>transport</em> supports
  1225. <a class="reference internal" href="asyncio-protocol.html#asyncio.WriteTransport" title="asyncio.WriteTransport"><code class="xref py py-class docutils literal notranslate"><span class="pre">WriteTransport</span></code></a> interface and <em>protocol</em> is an object
  1226. instantiated by the <em>protocol_factory</em>.</p>
  1227. <p>With <a class="reference internal" href="#asyncio.SelectorEventLoop" title="asyncio.SelectorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectorEventLoop</span></code></a> event loop, the <em>pipe</em> is set to
  1228. non-blocking mode.</p>
  1229. </dd></dl>
  1230. <div class="admonition note">
  1231. <p class="admonition-title">Note</p>
  1232. <p><a class="reference internal" href="#asyncio.SelectorEventLoop" title="asyncio.SelectorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectorEventLoop</span></code></a> does not support the above methods on
  1233. Windows. Use <a class="reference internal" href="#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProactorEventLoop</span></code></a> instead for Windows.</p>
  1234. </div>
  1235. <div class="admonition seealso">
  1236. <p class="admonition-title">See also</p>
  1237. <p>The <a class="reference internal" href="#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> and
  1238. <a class="reference internal" href="#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> methods.</p>
  1239. </div>
  1240. </section>
  1241. <section id="unix-signals">
  1242. <h3><a class="toc-backref" href="#id13" role="doc-backlink">Unix signals</a><a class="headerlink" href="#unix-signals" title="Link to this heading">¶</a></h3>
  1243. <dl class="py method" id="loop-add-signal-handler">
  1244. <dt class="sig sig-object py" id="asyncio.loop.add_signal_handler">
  1245. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">add_signal_handler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">signum</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">callback</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><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.add_signal_handler" title="Link to this definition">¶</a></dt>
  1246. <dd><p>Set <em>callback</em> as the handler for the <em>signum</em> signal.</p>
  1247. <p>The callback will be invoked by <em>loop</em>, along with other queued callbacks
  1248. and runnable coroutines of that event loop. Unlike signal handlers
  1249. registered using <a class="reference internal" href="signal.html#signal.signal" title="signal.signal"><code class="xref py py-func docutils literal notranslate"><span class="pre">signal.signal()</span></code></a>, a callback registered with this
  1250. function is allowed to interact with the event loop.</p>
  1251. <p>Raise <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if the signal number is invalid or uncatchable.
  1252. Raise <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> if there is a problem setting up the handler.</p>
  1253. <p>Use <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a> <a class="reference internal" href="#asyncio-pass-keywords"><span class="std std-ref">to pass keyword arguments</span></a> to <em>callback</em>.</p>
  1254. <p>Like <a class="reference internal" href="signal.html#signal.signal" title="signal.signal"><code class="xref py py-func docutils literal notranslate"><span class="pre">signal.signal()</span></code></a>, this function must be invoked in the main
  1255. thread.</p>
  1256. </dd></dl>
  1257. <dl class="py method">
  1258. <dt class="sig sig-object py" id="asyncio.loop.remove_signal_handler">
  1259. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">remove_signal_handler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sig</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.remove_signal_handler" title="Link to this definition">¶</a></dt>
  1260. <dd><p>Remove the handler for the <em>sig</em> signal.</p>
  1261. <p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the signal handler was removed, or <code class="docutils literal notranslate"><span class="pre">False</span></code> if
  1262. no handler was set for the given signal.</p>
  1263. <div class="availability docutils container">
  1264. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>
  1265. </div>
  1266. </dd></dl>
  1267. <div class="admonition seealso">
  1268. <p class="admonition-title">See also</p>
  1269. <p>The <a class="reference internal" href="signal.html#module-signal" title="signal: Set handlers for asynchronous events."><code class="xref py py-mod docutils literal notranslate"><span class="pre">signal</span></code></a> module.</p>
  1270. </div>
  1271. </section>
  1272. <section id="executing-code-in-thread-or-process-pools">
  1273. <h3><a class="toc-backref" href="#id14" role="doc-backlink">Executing code in thread or process pools</a><a class="headerlink" href="#executing-code-in-thread-or-process-pools" title="Link to this heading">¶</a></h3>
  1274. <dl class="py method">
  1275. <dt class="sig sig-object py" id="asyncio.loop.run_in_executor">
  1276. <em class="property"><span class="pre">awaitable</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">run_in_executor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">executor</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">func</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><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.run_in_executor" title="Link to this definition">¶</a></dt>
  1277. <dd><p>Arrange for <em>func</em> to be called in the specified executor.</p>
  1278. <p>The <em>executor</em> argument should be an <a class="reference internal" href="concurrent.futures.html#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal notranslate"><span class="pre">concurrent.futures.Executor</span></code></a>
  1279. instance. The default executor is used if <em>executor</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1280. <p>Example:</p>
  1281. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
  1282. <span class="kn">import</span> <span class="nn">concurrent.futures</span>
  1283. <span class="k">def</span> <span class="nf">blocking_io</span><span class="p">():</span>
  1284. <span class="c1"># File operations (such as logging) can block the</span>
  1285. <span class="c1"># event loop: run them in a thread pool.</span>
  1286. <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;/dev/urandom&#39;</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  1287. <span class="k">return</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span>
  1288. <span class="k">def</span> <span class="nf">cpu_bound</span><span class="p">():</span>
  1289. <span class="c1"># CPU-bound operations will block the event loop:</span>
  1290. <span class="c1"># in general it is preferable to run them in a</span>
  1291. <span class="c1"># process pool.</span>
  1292. <span class="k">return</span> <span class="nb">sum</span><span class="p">(</span><span class="n">i</span> <span class="o">*</span> <span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span> <span class="o">**</span> <span class="mi">7</span><span class="p">))</span>
  1293. <span class="k">async</span> <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
  1294. <span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_running_loop</span><span class="p">()</span>
  1295. <span class="c1">## Options:</span>
  1296. <span class="c1"># 1. Run in the default loop&#39;s executor:</span>
  1297. <span class="n">result</span> <span class="o">=</span> <span class="k">await</span> <span class="n">loop</span><span class="o">.</span><span class="n">run_in_executor</span><span class="p">(</span>
  1298. <span class="kc">None</span><span class="p">,</span> <span class="n">blocking_io</span><span class="p">)</span>
  1299. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;default thread pool&#39;</span><span class="p">,</span> <span class="n">result</span><span class="p">)</span>
  1300. <span class="c1"># 2. Run in a custom thread pool:</span>
  1301. <span class="k">with</span> <span class="n">concurrent</span><span class="o">.</span><span class="n">futures</span><span class="o">.</span><span class="n">ThreadPoolExecutor</span><span class="p">()</span> <span class="k">as</span> <span class="n">pool</span><span class="p">:</span>
  1302. <span class="n">result</span> <span class="o">=</span> <span class="k">await</span> <span class="n">loop</span><span class="o">.</span><span class="n">run_in_executor</span><span class="p">(</span>
  1303. <span class="n">pool</span><span class="p">,</span> <span class="n">blocking_io</span><span class="p">)</span>
  1304. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;custom thread pool&#39;</span><span class="p">,</span> <span class="n">result</span><span class="p">)</span>
  1305. <span class="c1"># 3. Run in a custom process pool:</span>
  1306. <span class="k">with</span> <span class="n">concurrent</span><span class="o">.</span><span class="n">futures</span><span class="o">.</span><span class="n">ProcessPoolExecutor</span><span class="p">()</span> <span class="k">as</span> <span class="n">pool</span><span class="p">:</span>
  1307. <span class="n">result</span> <span class="o">=</span> <span class="k">await</span> <span class="n">loop</span><span class="o">.</span><span class="n">run_in_executor</span><span class="p">(</span>
  1308. <span class="n">pool</span><span class="p">,</span> <span class="n">cpu_bound</span><span class="p">)</span>
  1309. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;custom process pool&#39;</span><span class="p">,</span> <span class="n">result</span><span class="p">)</span>
  1310. <span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
  1311. <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>
  1312. </pre></div>
  1313. </div>
  1314. <p>Note that the entry point guard (<code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">__name__</span> <span class="pre">==</span> <span class="pre">'__main__'</span></code>)
  1315. is required for option 3 due to the peculiarities of <a class="reference internal" href="multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a>,
  1316. which is used by <a class="reference internal" href="concurrent.futures.html#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a>.
  1317. See <a class="reference internal" href="multiprocessing.html#multiprocessing-safe-main-import"><span class="std std-ref">Safe importing of main module</span></a>.</p>
  1318. <p>This method returns a <a class="reference internal" href="asyncio-future.html#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Future</span></code></a> object.</p>
  1319. <p>Use <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a> <a class="reference internal" href="#asyncio-pass-keywords"><span class="std std-ref">to pass keyword arguments</span></a> to <em>func</em>.</p>
  1320. <div class="versionchanged">
  1321. <p><span class="versionmodified changed">Changed in version 3.5.3: </span><a class="reference internal" href="#asyncio.loop.run_in_executor" title="asyncio.loop.run_in_executor"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.run_in_executor()</span></code></a> no longer configures the
  1322. <code class="docutils literal notranslate"><span class="pre">max_workers</span></code> of the thread pool executor it creates, instead
  1323. leaving it up to the thread pool executor
  1324. (<a class="reference internal" href="concurrent.futures.html#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code></a>) to set the
  1325. default.</p>
  1326. </div>
  1327. </dd></dl>
  1328. <dl class="py method">
  1329. <dt class="sig sig-object py" id="asyncio.loop.set_default_executor">
  1330. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">set_default_executor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">executor</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.set_default_executor" title="Link to this definition">¶</a></dt>
  1331. <dd><p>Set <em>executor</em> as the default executor used by <a class="reference internal" href="#asyncio.loop.run_in_executor" title="asyncio.loop.run_in_executor"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run_in_executor()</span></code></a>.
  1332. <em>executor</em> must be an instance of
  1333. <a class="reference internal" href="concurrent.futures.html#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code></a>.</p>
  1334. <div class="versionchanged">
  1335. <p><span class="versionmodified changed">Changed in version 3.11: </span><em>executor</em> must be an instance of
  1336. <a class="reference internal" href="concurrent.futures.html#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code></a>.</p>
  1337. </div>
  1338. </dd></dl>
  1339. </section>
  1340. <section id="error-handling-api">
  1341. <h3><a class="toc-backref" href="#id15" role="doc-backlink">Error Handling API</a><a class="headerlink" href="#error-handling-api" title="Link to this heading">¶</a></h3>
  1342. <p>Allows customizing how exceptions are handled in the event loop.</p>
  1343. <dl class="py method">
  1344. <dt class="sig sig-object py" id="asyncio.loop.set_exception_handler">
  1345. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">set_exception_handler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">handler</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.set_exception_handler" title="Link to this definition">¶</a></dt>
  1346. <dd><p>Set <em>handler</em> as the new event loop exception handler.</p>
  1347. <p>If <em>handler</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, the default exception handler will
  1348. be set. Otherwise, <em>handler</em> must be a callable with the signature
  1349. matching <code class="docutils literal notranslate"><span class="pre">(loop,</span> <span class="pre">context)</span></code>, where <code class="docutils literal notranslate"><span class="pre">loop</span></code>
  1350. is a reference to the active event loop, and <code class="docutils literal notranslate"><span class="pre">context</span></code>
  1351. is a <code class="docutils literal notranslate"><span class="pre">dict</span></code> object containing the details of the exception
  1352. (see <a class="reference internal" href="#asyncio.loop.call_exception_handler" title="asyncio.loop.call_exception_handler"><code class="xref py py-meth docutils literal notranslate"><span class="pre">call_exception_handler()</span></code></a> documentation for details
  1353. about context).</p>
  1354. <p>If the handler is called on behalf of a <a class="reference internal" href="asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal notranslate"><span class="pre">Task</span></code></a> or
  1355. <a class="reference internal" href="#asyncio.Handle" title="asyncio.Handle"><code class="xref py py-class docutils literal notranslate"><span class="pre">Handle</span></code></a>, it is run in the
  1356. <a class="reference internal" href="contextvars.html#contextvars.Context" title="contextvars.Context"><code class="xref py py-class docutils literal notranslate"><span class="pre">contextvars.Context</span></code></a> of that task or callback handle.</p>
  1357. <div class="versionchanged">
  1358. <p><span class="versionmodified changed">Changed in version 3.12: </span>The handler may be called in the <a class="reference internal" href="contextvars.html#contextvars.Context" title="contextvars.Context"><code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code></a>
  1359. of the task or handle where the exception originated.</p>
  1360. </div>
  1361. </dd></dl>
  1362. <dl class="py method">
  1363. <dt class="sig sig-object py" id="asyncio.loop.get_exception_handler">
  1364. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">get_exception_handler</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.get_exception_handler" title="Link to this definition">¶</a></dt>
  1365. <dd><p>Return the current exception handler, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if no custom
  1366. exception handler was set.</p>
  1367. <div class="versionadded">
  1368. <p><span class="versionmodified added">New in version 3.5.2.</span></p>
  1369. </div>
  1370. </dd></dl>
  1371. <dl class="py method">
  1372. <dt class="sig sig-object py" id="asyncio.loop.default_exception_handler">
  1373. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">default_exception_handler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.default_exception_handler" title="Link to this definition">¶</a></dt>
  1374. <dd><p>Default exception handler.</p>
  1375. <p>This is called when an exception occurs and no exception
  1376. handler is set. This can be called by a custom exception
  1377. handler that wants to defer to the default handler behavior.</p>
  1378. <p><em>context</em> parameter has the same meaning as in
  1379. <a class="reference internal" href="#asyncio.loop.call_exception_handler" title="asyncio.loop.call_exception_handler"><code class="xref py py-meth docutils literal notranslate"><span class="pre">call_exception_handler()</span></code></a>.</p>
  1380. </dd></dl>
  1381. <dl class="py method">
  1382. <dt class="sig sig-object py" id="asyncio.loop.call_exception_handler">
  1383. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">call_exception_handler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.call_exception_handler" title="Link to this definition">¶</a></dt>
  1384. <dd><p>Call the current event loop exception handler.</p>
  1385. <p><em>context</em> is a <code class="docutils literal notranslate"><span class="pre">dict</span></code> object containing the following keys
  1386. (new keys may be introduced in future Python versions):</p>
  1387. <ul class="simple">
  1388. <li><p>‘message’: Error message;</p></li>
  1389. <li><p>‘exception’ (optional): Exception object;</p></li>
  1390. <li><p>‘future’ (optional): <a class="reference internal" href="asyncio-future.html#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Future</span></code></a> instance;</p></li>
  1391. <li><p>‘task’ (optional): <a class="reference internal" href="asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Task</span></code></a> instance;</p></li>
  1392. <li><p>‘handle’ (optional): <a class="reference internal" href="#asyncio.Handle" title="asyncio.Handle"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Handle</span></code></a> instance;</p></li>
  1393. <li><p>‘protocol’ (optional): <a class="reference internal" href="asyncio-protocol.html#asyncio-protocol"><span class="std std-ref">Protocol</span></a> instance;</p></li>
  1394. <li><p>‘transport’ (optional): <a class="reference internal" href="asyncio-protocol.html#asyncio-transport"><span class="std std-ref">Transport</span></a> instance;</p></li>
  1395. <li><p>‘socket’ (optional): <a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal notranslate"><span class="pre">socket.socket</span></code></a> instance;</p></li>
  1396. <li><dl class="simple">
  1397. <dt>‘asyncgen’ (optional): Asynchronous generator that caused</dt><dd><p>the exception.</p>
  1398. </dd>
  1399. </dl>
  1400. </li>
  1401. </ul>
  1402. <div class="admonition note">
  1403. <p class="admonition-title">Note</p>
  1404. <p>This method should not be overloaded in subclassed
  1405. event loops. For custom exception handling, use
  1406. the <a class="reference internal" href="#asyncio.loop.set_exception_handler" title="asyncio.loop.set_exception_handler"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_exception_handler()</span></code></a> method.</p>
  1407. </div>
  1408. </dd></dl>
  1409. </section>
  1410. <section id="enabling-debug-mode">
  1411. <h3><a class="toc-backref" href="#id16" role="doc-backlink">Enabling debug mode</a><a class="headerlink" href="#enabling-debug-mode" title="Link to this heading">¶</a></h3>
  1412. <dl class="py method">
  1413. <dt class="sig sig-object py" id="asyncio.loop.get_debug">
  1414. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">get_debug</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.get_debug" title="Link to this definition">¶</a></dt>
  1415. <dd><p>Get the debug mode (<a class="reference internal" href="functions.html#bool" title="bool"><code class="xref py py-class docutils literal notranslate"><span class="pre">bool</span></code></a>) of the event loop.</p>
  1416. <p>The default value is <code class="docutils literal notranslate"><span class="pre">True</span></code> if the environment variable
  1417. <span class="target" id="index-5"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONASYNCIODEBUG"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONASYNCIODEBUG</span></code></a> is set to a non-empty string, <code class="docutils literal notranslate"><span class="pre">False</span></code>
  1418. otherwise.</p>
  1419. </dd></dl>
  1420. <dl class="py method">
  1421. <dt class="sig sig-object py" id="asyncio.loop.set_debug">
  1422. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">set_debug</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">enabled</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference internal" href="functions.html#bool" title="bool"><span class="pre">bool</span></a></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.set_debug" title="Link to this definition">¶</a></dt>
  1423. <dd><p>Set the debug mode of the event loop.</p>
  1424. <div class="versionchanged">
  1425. <p><span class="versionmodified changed">Changed in version 3.7: </span>The new <a class="reference internal" href="devmode.html#devmode"><span class="std std-ref">Python Development Mode</span></a> can now also be used
  1426. to enable the debug mode.</p>
  1427. </div>
  1428. </dd></dl>
  1429. <dl class="py attribute">
  1430. <dt class="sig sig-object py" id="asyncio.loop.slow_callback_duration">
  1431. <span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">slow_callback_duration</span></span><a class="headerlink" href="#asyncio.loop.slow_callback_duration" title="Link to this definition">¶</a></dt>
  1432. <dd><p>This attribute can be used to set the
  1433. minimum execution duration in seconds that is considered “slow”.
  1434. When debug mode is enabled, “slow” callbacks are logged.</p>
  1435. <p>Default value is 100 milliseconds.</p>
  1436. </dd></dl>
  1437. <div class="admonition seealso">
  1438. <p class="admonition-title">See also</p>
  1439. <p>The <a class="reference internal" href="asyncio-dev.html#asyncio-debug-mode"><span class="std std-ref">debug mode of asyncio</span></a>.</p>
  1440. </div>
  1441. </section>
  1442. <section id="running-subprocesses">
  1443. <h3><a class="toc-backref" href="#id17" role="doc-backlink">Running Subprocesses</a><a class="headerlink" href="#running-subprocesses" title="Link to this heading">¶</a></h3>
  1444. <p>Methods described in this subsections are low-level. In regular
  1445. async/await code consider using the high-level
  1446. <a class="reference internal" href="asyncio-subprocess.html#asyncio.create_subprocess_shell" title="asyncio.create_subprocess_shell"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.create_subprocess_shell()</span></code></a> and
  1447. <a class="reference internal" href="asyncio-subprocess.html#asyncio.create_subprocess_exec" title="asyncio.create_subprocess_exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.create_subprocess_exec()</span></code></a> convenience functions instead.</p>
  1448. <div class="admonition note">
  1449. <p class="admonition-title">Note</p>
  1450. <p>On Windows, the default event loop <a class="reference internal" href="#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProactorEventLoop</span></code></a> supports
  1451. subprocesses, whereas <a class="reference internal" href="#asyncio.SelectorEventLoop" title="asyncio.SelectorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectorEventLoop</span></code></a> does not. See
  1452. <a class="reference internal" href="asyncio-platforms.html#asyncio-windows-subprocess"><span class="std std-ref">Subprocess Support on Windows</span></a> for
  1453. details.</p>
  1454. </div>
  1455. <dl class="py method" id="loop-subprocess-exec">
  1456. <dt class="sig sig-object py" id="asyncio.loop.subprocess_exec">
  1457. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">subprocess_exec</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">protocol_factory</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">subprocess.PIPE</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">subprocess.PIPE</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">subprocess.PIPE</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.subprocess_exec" title="Link to this definition">¶</a></dt>
  1458. <dd><p>Create a subprocess from one or more string arguments specified by
  1459. <em>args</em>.</p>
  1460. <p><em>args</em> must be a list of strings represented by:</p>
  1461. <ul class="simple">
  1462. <li><p><a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>;</p></li>
  1463. <li><p>or <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>, encoded to the
  1464. <a class="reference internal" href="os.html#filesystem-encoding"><span class="std std-ref">filesystem encoding</span></a>.</p></li>
  1465. </ul>
  1466. <p>The first string specifies the program executable,
  1467. and the remaining strings specify the arguments. Together, string
  1468. arguments form the <code class="docutils literal notranslate"><span class="pre">argv</span></code> of the program.</p>
  1469. <p>This is similar to the standard library <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>
  1470. class called with <code class="docutils literal notranslate"><span class="pre">shell=False</span></code> and the list of strings passed as
  1471. the first argument; however, where <a class="reference internal" href="subprocess.html#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code></a> takes
  1472. a single argument which is list of strings, <em>subprocess_exec</em>
  1473. takes multiple string arguments.</p>
  1474. <p>The <em>protocol_factory</em> must be a callable returning a subclass of the
  1475. <a class="reference internal" href="asyncio-protocol.html#asyncio.SubprocessProtocol" title="asyncio.SubprocessProtocol"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.SubprocessProtocol</span></code></a> class.</p>
  1476. <p>Other parameters:</p>
  1477. <ul>
  1478. <li><p><em>stdin</em> can be any of these:</p>
  1479. <ul class="simple">
  1480. <li><p>a file-like object</p></li>
  1481. <li><p>an existing file descriptor (a positive integer), for example those created with <a class="reference internal" href="os.html#os.pipe" title="os.pipe"><code class="xref py py-meth docutils literal notranslate"><span class="pre">os.pipe()</span></code></a></p></li>
  1482. <li><p>the <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> constant (default) which will create a new
  1483. pipe and connect it,</p></li>
  1484. <li><p>the value <code class="docutils literal notranslate"><span class="pre">None</span></code> which will make the subprocess inherit the file
  1485. descriptor from this process</p></li>
  1486. <li><p>the <a class="reference internal" href="subprocess.html#subprocess.DEVNULL" title="subprocess.DEVNULL"><code class="xref py py-const docutils literal notranslate"><span class="pre">subprocess.DEVNULL</span></code></a> constant which indicates that the
  1487. special <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> file will be used</p></li>
  1488. </ul>
  1489. </li>
  1490. <li><p><em>stdout</em> can be any of these:</p>
  1491. <ul class="simple">
  1492. <li><p>a file-like object</p></li>
  1493. <li><p>the <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> constant (default) which will create a new
  1494. pipe and connect it,</p></li>
  1495. <li><p>the value <code class="docutils literal notranslate"><span class="pre">None</span></code> which will make the subprocess inherit the file
  1496. descriptor from this process</p></li>
  1497. <li><p>the <a class="reference internal" href="subprocess.html#subprocess.DEVNULL" title="subprocess.DEVNULL"><code class="xref py py-const docutils literal notranslate"><span class="pre">subprocess.DEVNULL</span></code></a> constant which indicates that the
  1498. special <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> file will be used</p></li>
  1499. </ul>
  1500. </li>
  1501. <li><p><em>stderr</em> can be any of these:</p>
  1502. <ul class="simple">
  1503. <li><p>a file-like object</p></li>
  1504. <li><p>the <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> constant (default) which will create a new
  1505. pipe and connect it,</p></li>
  1506. <li><p>the value <code class="docutils literal notranslate"><span class="pre">None</span></code> which will make the subprocess inherit the file
  1507. descriptor from this process</p></li>
  1508. <li><p>the <a class="reference internal" href="subprocess.html#subprocess.DEVNULL" title="subprocess.DEVNULL"><code class="xref py py-const docutils literal notranslate"><span class="pre">subprocess.DEVNULL</span></code></a> constant which indicates that the
  1509. special <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> file will be used</p></li>
  1510. <li><p>the <a class="reference internal" href="subprocess.html#subprocess.STDOUT" title="subprocess.STDOUT"><code class="xref py py-const docutils literal notranslate"><span class="pre">subprocess.STDOUT</span></code></a> constant which will connect the standard
  1511. error stream to the process’ standard output stream</p></li>
  1512. </ul>
  1513. </li>
  1514. <li><p>All other keyword arguments are passed to <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>
  1515. without interpretation, except for <em>bufsize</em>, <em>universal_newlines</em>,
  1516. <em>shell</em>, <em>text</em>, <em>encoding</em> and <em>errors</em>, which should not be specified
  1517. at all.</p>
  1518. <p>The <code class="docutils literal notranslate"><span class="pre">asyncio</span></code> subprocess API does not support decoding the streams
  1519. as text. <a class="reference internal" href="stdtypes.html#bytes.decode" title="bytes.decode"><code class="xref py py-func docutils literal notranslate"><span class="pre">bytes.decode()</span></code></a> can be used to convert the bytes returned
  1520. from the stream to text.</p>
  1521. </li>
  1522. </ul>
  1523. <p>If a file-like object passed as <em>stdin</em>, <em>stdout</em> or <em>stderr</em> represents a
  1524. pipe, then the other side of this pipe should be registered with
  1525. <a class="reference internal" href="#asyncio.loop.connect_write_pipe" title="asyncio.loop.connect_write_pipe"><code class="xref py py-meth docutils literal notranslate"><span class="pre">connect_write_pipe()</span></code></a> or <a class="reference internal" href="#asyncio.loop.connect_read_pipe" title="asyncio.loop.connect_read_pipe"><code class="xref py py-meth docutils literal notranslate"><span class="pre">connect_read_pipe()</span></code></a> for use
  1526. with the event loop.</p>
  1527. <p>See the constructor of the <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
  1528. for documentation on other arguments.</p>
  1529. <p>Returns a pair of <code class="docutils literal notranslate"><span class="pre">(transport,</span> <span class="pre">protocol)</span></code>, where <em>transport</em>
  1530. conforms to the <a class="reference internal" href="asyncio-protocol.html#asyncio.SubprocessTransport" title="asyncio.SubprocessTransport"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.SubprocessTransport</span></code></a> base class and
  1531. <em>protocol</em> is an object instantiated by the <em>protocol_factory</em>.</p>
  1532. </dd></dl>
  1533. <dl class="py method">
  1534. <dt class="sig sig-object py" id="asyncio.loop.subprocess_shell">
  1535. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-prename descclassname"><span class="pre">loop.</span></span><span class="sig-name descname"><span class="pre">subprocess_shell</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">protocol_factory</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cmd</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</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">subprocess.PIPE</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">subprocess.PIPE</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">subprocess.PIPE</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.loop.subprocess_shell" title="Link to this definition">¶</a></dt>
  1536. <dd><p>Create a subprocess from <em>cmd</em>, which can be a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> or a
  1537. <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> string encoded to the
  1538. <a class="reference internal" href="os.html#filesystem-encoding"><span class="std std-ref">filesystem encoding</span></a>,
  1539. using the platform’s “shell” syntax.</p>
  1540. <p>This is similar to the standard library <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>
  1541. class called with <code class="docutils literal notranslate"><span class="pre">shell=True</span></code>.</p>
  1542. <p>The <em>protocol_factory</em> must be a callable returning a subclass of the
  1543. <a class="reference internal" href="asyncio-protocol.html#asyncio.SubprocessProtocol" title="asyncio.SubprocessProtocol"><code class="xref py py-class docutils literal notranslate"><span class="pre">SubprocessProtocol</span></code></a> class.</p>
  1544. <p>See <a class="reference internal" href="#asyncio.loop.subprocess_exec" title="asyncio.loop.subprocess_exec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">subprocess_exec()</span></code></a> for more details about
  1545. the remaining arguments.</p>
  1546. <p>Returns a pair of <code class="docutils literal notranslate"><span class="pre">(transport,</span> <span class="pre">protocol)</span></code>, where <em>transport</em>
  1547. conforms to the <a class="reference internal" href="asyncio-protocol.html#asyncio.SubprocessTransport" title="asyncio.SubprocessTransport"><code class="xref py py-class docutils literal notranslate"><span class="pre">SubprocessTransport</span></code></a> base class and
  1548. <em>protocol</em> is an object instantiated by the <em>protocol_factory</em>.</p>
  1549. </dd></dl>
  1550. <div class="admonition note">
  1551. <p class="admonition-title">Note</p>
  1552. <p>It is the application’s responsibility to ensure that all whitespace
  1553. and special characters are quoted appropriately to avoid <a class="reference external" href="https://en.wikipedia.org/wiki/Shell_injection#Shell_injection">shell injection</a>
  1554. vulnerabilities. The <a class="reference internal" href="shlex.html#shlex.quote" title="shlex.quote"><code class="xref py py-func docutils literal notranslate"><span class="pre">shlex.quote()</span></code></a> function can be used to
  1555. properly escape whitespace and special characters in strings that
  1556. are going to be used to construct shell commands.</p>
  1557. </div>
  1558. </section>
  1559. </section>
  1560. <section id="callback-handles">
  1561. <h2>Callback Handles<a class="headerlink" href="#callback-handles" title="Link to this heading">¶</a></h2>
  1562. <dl class="py class">
  1563. <dt class="sig sig-object py" id="asyncio.Handle">
  1564. <em class="property"><span class="pre">class</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">Handle</span></span><a class="headerlink" href="#asyncio.Handle" title="Link to this definition">¶</a></dt>
  1565. <dd><p>A callback wrapper object returned by <a class="reference internal" href="#asyncio.loop.call_soon" title="asyncio.loop.call_soon"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.call_soon()</span></code></a>,
  1566. <a class="reference internal" href="#asyncio.loop.call_soon_threadsafe" title="asyncio.loop.call_soon_threadsafe"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.call_soon_threadsafe()</span></code></a>.</p>
  1567. <dl class="py method">
  1568. <dt class="sig sig-object py" id="asyncio.Handle.get_context">
  1569. <span class="sig-name descname"><span class="pre">get_context</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Handle.get_context" title="Link to this definition">¶</a></dt>
  1570. <dd><p>Return the <a class="reference internal" href="contextvars.html#contextvars.Context" title="contextvars.Context"><code class="xref py py-class docutils literal notranslate"><span class="pre">contextvars.Context</span></code></a> object
  1571. associated with the handle.</p>
  1572. <div class="versionadded">
  1573. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1574. </div>
  1575. </dd></dl>
  1576. <dl class="py method">
  1577. <dt class="sig sig-object py" id="asyncio.Handle.cancel">
  1578. <span class="sig-name descname"><span class="pre">cancel</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Handle.cancel" title="Link to this definition">¶</a></dt>
  1579. <dd><p>Cancel the callback. If the callback has already been canceled
  1580. or executed, this method has no effect.</p>
  1581. </dd></dl>
  1582. <dl class="py method">
  1583. <dt class="sig sig-object py" id="asyncio.Handle.cancelled">
  1584. <span class="sig-name descname"><span class="pre">cancelled</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Handle.cancelled" title="Link to this definition">¶</a></dt>
  1585. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the callback was cancelled.</p>
  1586. <div class="versionadded">
  1587. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1588. </div>
  1589. </dd></dl>
  1590. </dd></dl>
  1591. <dl class="py class">
  1592. <dt class="sig sig-object py" id="asyncio.TimerHandle">
  1593. <em class="property"><span class="pre">class</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">TimerHandle</span></span><a class="headerlink" href="#asyncio.TimerHandle" title="Link to this definition">¶</a></dt>
  1594. <dd><p>A callback wrapper object returned by <a class="reference internal" href="#asyncio.loop.call_later" title="asyncio.loop.call_later"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.call_later()</span></code></a>,
  1595. and <a class="reference internal" href="#asyncio.loop.call_at" title="asyncio.loop.call_at"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.call_at()</span></code></a>.</p>
  1596. <p>This class is a subclass of <a class="reference internal" href="#asyncio.Handle" title="asyncio.Handle"><code class="xref py py-class docutils literal notranslate"><span class="pre">Handle</span></code></a>.</p>
  1597. <dl class="py method">
  1598. <dt class="sig sig-object py" id="asyncio.TimerHandle.when">
  1599. <span class="sig-name descname"><span class="pre">when</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.TimerHandle.when" title="Link to this definition">¶</a></dt>
  1600. <dd><p>Return a scheduled callback time as <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> seconds.</p>
  1601. <p>The time is an absolute timestamp, using the same time
  1602. reference as <a class="reference internal" href="#asyncio.loop.time" title="asyncio.loop.time"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.time()</span></code></a>.</p>
  1603. <div class="versionadded">
  1604. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1605. </div>
  1606. </dd></dl>
  1607. </dd></dl>
  1608. </section>
  1609. <section id="server-objects">
  1610. <h2>Server Objects<a class="headerlink" href="#server-objects" title="Link to this heading">¶</a></h2>
  1611. <p>Server objects are created by <a class="reference internal" href="#asyncio.loop.create_server" title="asyncio.loop.create_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_server()</span></code></a>,
  1612. <a class="reference internal" href="#asyncio.loop.create_unix_server" title="asyncio.loop.create_unix_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_unix_server()</span></code></a>, <a class="reference internal" href="asyncio-stream.html#asyncio.start_server" title="asyncio.start_server"><code class="xref py py-func docutils literal notranslate"><span class="pre">start_server()</span></code></a>,
  1613. and <a class="reference internal" href="asyncio-stream.html#asyncio.start_unix_server" title="asyncio.start_unix_server"><code class="xref py py-func docutils literal notranslate"><span class="pre">start_unix_server()</span></code></a> functions.</p>
  1614. <p>Do not instantiate the <a class="reference internal" href="#asyncio.Server" title="asyncio.Server"><code class="xref py py-class docutils literal notranslate"><span class="pre">Server</span></code></a> class directly.</p>
  1615. <dl class="py class">
  1616. <dt class="sig sig-object py" id="asyncio.Server">
  1617. <em class="property"><span class="pre">class</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">Server</span></span><a class="headerlink" href="#asyncio.Server" title="Link to this definition">¶</a></dt>
  1618. <dd><p><em>Server</em> objects are asynchronous context managers. When used in an
  1619. <code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">with</span></code> statement, it’s guaranteed that the Server object is
  1620. closed and not accepting new connections when the <code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">with</span></code>
  1621. statement is completed:</p>
  1622. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">srv</span> <span class="o">=</span> <span class="k">await</span> <span class="n">loop</span><span class="o">.</span><span class="n">create_server</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
  1623. <span class="k">async</span> <span class="k">with</span> <span class="n">srv</span><span class="p">:</span>
  1624. <span class="c1"># some code</span>
  1625. <span class="c1"># At this point, srv is closed and no longer accepts new connections.</span>
  1626. </pre></div>
  1627. </div>
  1628. <div class="versionchanged">
  1629. <p><span class="versionmodified changed">Changed in version 3.7: </span>Server object is an asynchronous context manager since Python 3.7.</p>
  1630. </div>
  1631. <div class="versionchanged">
  1632. <p><span class="versionmodified changed">Changed in version 3.11: </span>This class was exposed publicly as <code class="docutils literal notranslate"><span class="pre">asyncio.Server</span></code> in Python 3.9.11, 3.10.3 and 3.11.</p>
  1633. </div>
  1634. <dl class="py method">
  1635. <dt class="sig sig-object py" id="asyncio.Server.close">
  1636. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Server.close" title="Link to this definition">¶</a></dt>
  1637. <dd><p>Stop serving: close listening sockets and set the <a class="reference internal" href="#asyncio.Server.sockets" title="asyncio.Server.sockets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sockets</span></code></a>
  1638. attribute to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1639. <p>The sockets that represent existing incoming client connections
  1640. are left open.</p>
  1641. <p>The server is closed asynchronously; use the <a class="reference internal" href="#asyncio.Server.wait_closed" title="asyncio.Server.wait_closed"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait_closed()</span></code></a>
  1642. coroutine to wait until the server is closed (and no more
  1643. connections are active).</p>
  1644. </dd></dl>
  1645. <dl class="py method">
  1646. <dt class="sig sig-object py" id="asyncio.Server.get_loop">
  1647. <span class="sig-name descname"><span class="pre">get_loop</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Server.get_loop" title="Link to this definition">¶</a></dt>
  1648. <dd><p>Return the event loop associated with the server object.</p>
  1649. <div class="versionadded">
  1650. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1651. </div>
  1652. </dd></dl>
  1653. <dl class="py method">
  1654. <dt class="sig sig-object py" id="asyncio.Server.start_serving">
  1655. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-name descname"><span class="pre">start_serving</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Server.start_serving" title="Link to this definition">¶</a></dt>
  1656. <dd><p>Start accepting connections.</p>
  1657. <p>This method is idempotent, so it can be called when
  1658. the server is already serving.</p>
  1659. <p>The <em>start_serving</em> keyword-only parameter to
  1660. <a class="reference internal" href="#asyncio.loop.create_server" title="asyncio.loop.create_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_server()</span></code></a> and
  1661. <a class="reference internal" href="asyncio-stream.html#asyncio.start_server" title="asyncio.start_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">asyncio.start_server()</span></code></a> allows creating a Server object
  1662. that is not accepting connections initially. In this case
  1663. <code class="docutils literal notranslate"><span class="pre">Server.start_serving()</span></code>, or <a class="reference internal" href="#asyncio.Server.serve_forever" title="asyncio.Server.serve_forever"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Server.serve_forever()</span></code></a> can be used
  1664. to make the Server start accepting connections.</p>
  1665. <div class="versionadded">
  1666. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1667. </div>
  1668. </dd></dl>
  1669. <dl class="py method">
  1670. <dt class="sig sig-object py" id="asyncio.Server.serve_forever">
  1671. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-name descname"><span class="pre">serve_forever</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Server.serve_forever" title="Link to this definition">¶</a></dt>
  1672. <dd><p>Start accepting connections until the coroutine is cancelled.
  1673. Cancellation of <code class="docutils literal notranslate"><span class="pre">serve_forever</span></code> task causes the server
  1674. to be closed.</p>
  1675. <p>This method can be called if the server is already accepting
  1676. connections. Only one <code class="docutils literal notranslate"><span class="pre">serve_forever</span></code> task can exist per
  1677. one <em>Server</em> object.</p>
  1678. <p>Example:</p>
  1679. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">async</span> <span class="k">def</span> <span class="nf">client_connected</span><span class="p">(</span><span class="n">reader</span><span class="p">,</span> <span class="n">writer</span><span class="p">):</span>
  1680. <span class="c1"># Communicate with the client with</span>
  1681. <span class="c1"># reader/writer streams. For example:</span>
  1682. <span class="k">await</span> <span class="n">reader</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
  1683. <span class="k">async</span> <span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
  1684. <span class="n">srv</span> <span class="o">=</span> <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">start_server</span><span class="p">(</span>
  1685. <span class="n">client_connected</span><span class="p">,</span> <span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">)</span>
  1686. <span class="k">await</span> <span class="n">srv</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
  1687. <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><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
  1688. </pre></div>
  1689. </div>
  1690. <div class="versionadded">
  1691. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1692. </div>
  1693. </dd></dl>
  1694. <dl class="py method">
  1695. <dt class="sig sig-object py" id="asyncio.Server.is_serving">
  1696. <span class="sig-name descname"><span class="pre">is_serving</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Server.is_serving" title="Link to this definition">¶</a></dt>
  1697. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the server is accepting new connections.</p>
  1698. <div class="versionadded">
  1699. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1700. </div>
  1701. </dd></dl>
  1702. <dl class="py method">
  1703. <dt class="sig sig-object py" id="asyncio.Server.wait_closed">
  1704. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-name descname"><span class="pre">wait_closed</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Server.wait_closed" title="Link to this definition">¶</a></dt>
  1705. <dd><p>Wait until the <a class="reference internal" href="#asyncio.Server.close" title="asyncio.Server.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> method completes and all active
  1706. connections have finished.</p>
  1707. </dd></dl>
  1708. <dl class="py attribute">
  1709. <dt class="sig sig-object py" id="asyncio.Server.sockets">
  1710. <span class="sig-name descname"><span class="pre">sockets</span></span><a class="headerlink" href="#asyncio.Server.sockets" title="Link to this definition">¶</a></dt>
  1711. <dd><p>List of socket-like objects, <code class="docutils literal notranslate"><span class="pre">asyncio.trsock.TransportSocket</span></code>, which
  1712. the server is listening on.</p>
  1713. <div class="versionchanged">
  1714. <p><span class="versionmodified changed">Changed in version 3.7: </span>Prior to Python 3.7 <code class="docutils literal notranslate"><span class="pre">Server.sockets</span></code> used to return an
  1715. internal list of server sockets directly. In 3.7 a copy
  1716. of that list is returned.</p>
  1717. </div>
  1718. </dd></dl>
  1719. </dd></dl>
  1720. </section>
  1721. <section id="event-loop-implementations">
  1722. <span id="asyncio-event-loop-implementations"></span><span id="asyncio-event-loops"></span><h2>Event Loop Implementations<a class="headerlink" href="#event-loop-implementations" title="Link to this heading">¶</a></h2>
  1723. <p>asyncio ships with two different event loop implementations:
  1724. <a class="reference internal" href="#asyncio.SelectorEventLoop" title="asyncio.SelectorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectorEventLoop</span></code></a> and <a class="reference internal" href="#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProactorEventLoop</span></code></a>.</p>
  1725. <p>By default asyncio is configured to use <a class="reference internal" href="#asyncio.SelectorEventLoop" title="asyncio.SelectorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectorEventLoop</span></code></a>
  1726. on Unix and <a class="reference internal" href="#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProactorEventLoop</span></code></a> on Windows.</p>
  1727. <dl class="py class">
  1728. <dt class="sig sig-object py" id="asyncio.SelectorEventLoop">
  1729. <em class="property"><span class="pre">class</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">SelectorEventLoop</span></span><a class="headerlink" href="#asyncio.SelectorEventLoop" title="Link to this definition">¶</a></dt>
  1730. <dd><p>An event loop based on the <a class="reference internal" href="selectors.html#module-selectors" title="selectors: High-level I/O multiplexing."><code class="xref py py-mod docutils literal notranslate"><span class="pre">selectors</span></code></a> module.</p>
  1731. <p>Uses the most efficient <em>selector</em> available for the given
  1732. platform. It is also possible to manually configure the
  1733. exact selector implementation to be used:</p>
  1734. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
  1735. <span class="kn">import</span> <span class="nn">selectors</span>
  1736. <span class="k">class</span> <span class="nc">MyPolicy</span><span class="p">(</span><span class="n">asyncio</span><span class="o">.</span><span class="n">DefaultEventLoopPolicy</span><span class="p">):</span>
  1737. <span class="k">def</span> <span class="nf">new_event_loop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1738. <span class="n">selector</span> <span class="o">=</span> <span class="n">selectors</span><span class="o">.</span><span class="n">SelectSelector</span><span class="p">()</span>
  1739. <span class="k">return</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">SelectorEventLoop</span><span class="p">(</span><span class="n">selector</span><span class="p">)</span>
  1740. <span class="n">asyncio</span><span class="o">.</span><span class="n">set_event_loop_policy</span><span class="p">(</span><span class="n">MyPolicy</span><span class="p">())</span>
  1741. </pre></div>
  1742. </div>
  1743. <div class="availability docutils container">
  1744. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>
  1745. </div>
  1746. </dd></dl>
  1747. <dl class="py class">
  1748. <dt class="sig sig-object py" id="asyncio.ProactorEventLoop">
  1749. <em class="property"><span class="pre">class</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">ProactorEventLoop</span></span><a class="headerlink" href="#asyncio.ProactorEventLoop" title="Link to this definition">¶</a></dt>
  1750. <dd><p>An event loop for Windows that uses “I/O Completion Ports” (IOCP).</p>
  1751. <div class="availability docutils container">
  1752. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p>
  1753. </div>
  1754. <div class="admonition seealso">
  1755. <p class="admonition-title">See also</p>
  1756. <p><a class="reference external" href="https://docs.microsoft.com/en-ca/windows/desktop/FileIO/i-o-completion-ports">MSDN documentation on I/O Completion Ports</a>.</p>
  1757. </div>
  1758. </dd></dl>
  1759. <dl class="py class">
  1760. <dt class="sig sig-object py" id="asyncio.AbstractEventLoop">
  1761. <em class="property"><span class="pre">class</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">AbstractEventLoop</span></span><a class="headerlink" href="#asyncio.AbstractEventLoop" title="Link to this definition">¶</a></dt>
  1762. <dd><p>Abstract base class for asyncio-compliant event loops.</p>
  1763. <p>The <a class="reference internal" href="#asyncio-event-loop-methods"><span class="std std-ref">Event Loop Methods</span></a> section lists all
  1764. methods that an alternative implementation of <code class="docutils literal notranslate"><span class="pre">AbstractEventLoop</span></code>
  1765. should have defined.</p>
  1766. </dd></dl>
  1767. </section>
  1768. <section id="examples">
  1769. <h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
  1770. <p>Note that all examples in this section <strong>purposefully</strong> show how
  1771. to use the low-level event loop APIs, such as <a class="reference internal" href="#asyncio.loop.run_forever" title="asyncio.loop.run_forever"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.run_forever()</span></code></a>
  1772. and <a class="reference internal" href="#asyncio.loop.call_soon" title="asyncio.loop.call_soon"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.call_soon()</span></code></a>. Modern asyncio applications rarely
  1773. need to be written this way; consider using the high-level functions
  1774. like <a class="reference internal" href="asyncio-runner.html#asyncio.run" title="asyncio.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.run()</span></code></a>.</p>
  1775. <section id="hello-world-with-call-soon">
  1776. <span id="asyncio-example-lowlevel-helloworld"></span><h3>Hello World with call_soon()<a class="headerlink" href="#hello-world-with-call-soon" title="Link to this heading">¶</a></h3>
  1777. <p>An example using the <a class="reference internal" href="#asyncio.loop.call_soon" title="asyncio.loop.call_soon"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.call_soon()</span></code></a> method to schedule a
  1778. callback. The callback displays <code class="docutils literal notranslate"><span class="pre">&quot;Hello</span> <span class="pre">World&quot;</span></code> and then stops the
  1779. event loop:</p>
  1780. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
  1781. <span class="k">def</span> <span class="nf">hello_world</span><span class="p">(</span><span class="n">loop</span><span class="p">):</span>
  1782. <span class="w"> </span><span class="sd">&quot;&quot;&quot;A callback to print &#39;Hello World&#39; and stop the event loop&quot;&quot;&quot;</span>
  1783. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Hello World&#39;</span><span class="p">)</span>
  1784. <span class="n">loop</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  1785. <span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">new_event_loop</span><span class="p">()</span>
  1786. <span class="c1"># Schedule a call to hello_world()</span>
  1787. <span class="n">loop</span><span class="o">.</span><span class="n">call_soon</span><span class="p">(</span><span class="n">hello_world</span><span class="p">,</span> <span class="n">loop</span><span class="p">)</span>
  1788. <span class="c1"># Blocking call interrupted by loop.stop()</span>
  1789. <span class="k">try</span><span class="p">:</span>
  1790. <span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
  1791. <span class="k">finally</span><span class="p">:</span>
  1792. <span class="n">loop</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1793. </pre></div>
  1794. </div>
  1795. <div class="admonition seealso">
  1796. <p class="admonition-title">See also</p>
  1797. <p>A similar <a class="reference internal" href="asyncio-task.html#coroutine"><span class="std std-ref">Hello World</span></a>
  1798. example created with a coroutine and the <a class="reference internal" href="asyncio-runner.html#asyncio.run" title="asyncio.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a> function.</p>
  1799. </div>
  1800. </section>
  1801. <section id="display-the-current-date-with-call-later">
  1802. <span id="asyncio-example-call-later"></span><h3>Display the current date with call_later()<a class="headerlink" href="#display-the-current-date-with-call-later" title="Link to this heading">¶</a></h3>
  1803. <p>An example of a callback displaying the current date every second. The
  1804. callback uses the <a class="reference internal" href="#asyncio.loop.call_later" title="asyncio.loop.call_later"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.call_later()</span></code></a> method to reschedule itself
  1805. after 5 seconds, and then stops the event loop:</p>
  1806. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
  1807. <span class="kn">import</span> <span class="nn">datetime</span>
  1808. <span class="k">def</span> <span class="nf">display_date</span><span class="p">(</span><span class="n">end_time</span><span class="p">,</span> <span class="n">loop</span><span class="p">):</span>
  1809. <span class="nb">print</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">())</span>
  1810. <span class="k">if</span> <span class="p">(</span><span class="n">loop</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="mf">1.0</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">end_time</span><span class="p">:</span>
  1811. <span class="n">loop</span><span class="o">.</span><span class="n">call_later</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">display_date</span><span class="p">,</span> <span class="n">end_time</span><span class="p">,</span> <span class="n">loop</span><span class="p">)</span>
  1812. <span class="k">else</span><span class="p">:</span>
  1813. <span class="n">loop</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  1814. <span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">new_event_loop</span><span class="p">()</span>
  1815. <span class="c1"># Schedule the first call to display_date()</span>
  1816. <span class="n">end_time</span> <span class="o">=</span> <span class="n">loop</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="mf">5.0</span>
  1817. <span class="n">loop</span><span class="o">.</span><span class="n">call_soon</span><span class="p">(</span><span class="n">display_date</span><span class="p">,</span> <span class="n">end_time</span><span class="p">,</span> <span class="n">loop</span><span class="p">)</span>
  1818. <span class="c1"># Blocking call interrupted by loop.stop()</span>
  1819. <span class="k">try</span><span class="p">:</span>
  1820. <span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
  1821. <span class="k">finally</span><span class="p">:</span>
  1822. <span class="n">loop</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1823. </pre></div>
  1824. </div>
  1825. <div class="admonition seealso">
  1826. <p class="admonition-title">See also</p>
  1827. <p>A similar <a class="reference internal" href="asyncio-task.html#asyncio-example-sleep"><span class="std std-ref">current date</span></a> example
  1828. created with a coroutine and the <a class="reference internal" href="asyncio-runner.html#asyncio.run" title="asyncio.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a> function.</p>
  1829. </div>
  1830. </section>
  1831. <section id="watch-a-file-descriptor-for-read-events">
  1832. <span id="asyncio-example-watch-fd"></span><h3>Watch a file descriptor for read events<a class="headerlink" href="#watch-a-file-descriptor-for-read-events" title="Link to this heading">¶</a></h3>
  1833. <p>Wait until a file descriptor received some data using the
  1834. <a class="reference internal" href="#asyncio.loop.add_reader" title="asyncio.loop.add_reader"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.add_reader()</span></code></a> method and then close the event loop:</p>
  1835. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
  1836. <span class="kn">from</span> <span class="nn">socket</span> <span class="kn">import</span> <span class="n">socketpair</span>
  1837. <span class="c1"># Create a pair of connected file descriptors</span>
  1838. <span class="n">rsock</span><span class="p">,</span> <span class="n">wsock</span> <span class="o">=</span> <span class="n">socketpair</span><span class="p">()</span>
  1839. <span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">new_event_loop</span><span class="p">()</span>
  1840. <span class="k">def</span> <span class="nf">reader</span><span class="p">():</span>
  1841. <span class="n">data</span> <span class="o">=</span> <span class="n">rsock</span><span class="o">.</span><span class="n">recv</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span>
  1842. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Received:&quot;</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">())</span>
  1843. <span class="c1"># We are done: unregister the file descriptor</span>
  1844. <span class="n">loop</span><span class="o">.</span><span class="n">remove_reader</span><span class="p">(</span><span class="n">rsock</span><span class="p">)</span>
  1845. <span class="c1"># Stop the event loop</span>
  1846. <span class="n">loop</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  1847. <span class="c1"># Register the file descriptor for read event</span>
  1848. <span class="n">loop</span><span class="o">.</span><span class="n">add_reader</span><span class="p">(</span><span class="n">rsock</span><span class="p">,</span> <span class="n">reader</span><span class="p">)</span>
  1849. <span class="c1"># Simulate the reception of data from the network</span>
  1850. <span class="n">loop</span><span class="o">.</span><span class="n">call_soon</span><span class="p">(</span><span class="n">wsock</span><span class="o">.</span><span class="n">send</span><span class="p">,</span> <span class="s1">&#39;abc&#39;</span><span class="o">.</span><span class="n">encode</span><span class="p">())</span>
  1851. <span class="k">try</span><span class="p">:</span>
  1852. <span class="c1"># Run the event loop</span>
  1853. <span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
  1854. <span class="k">finally</span><span class="p">:</span>
  1855. <span class="c1"># We are done. Close sockets and the event loop.</span>
  1856. <span class="n">rsock</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1857. <span class="n">wsock</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1858. <span class="n">loop</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1859. </pre></div>
  1860. </div>
  1861. <div class="admonition seealso">
  1862. <p class="admonition-title">See also</p>
  1863. <ul class="simple">
  1864. <li><p>A similar <a class="reference internal" href="asyncio-protocol.html#asyncio-example-create-connection"><span class="std std-ref">example</span></a>
  1865. using transports, protocols, and the
  1866. <a class="reference internal" href="#asyncio.loop.create_connection" title="asyncio.loop.create_connection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_connection()</span></code></a> method.</p></li>
  1867. <li><p>Another similar <a class="reference internal" href="asyncio-stream.html#asyncio-example-create-connection-streams"><span class="std std-ref">example</span></a>
  1868. using the high-level <a class="reference internal" href="asyncio-stream.html#asyncio.open_connection" title="asyncio.open_connection"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.open_connection()</span></code></a> function
  1869. and streams.</p></li>
  1870. </ul>
  1871. </div>
  1872. </section>
  1873. <section id="set-signal-handlers-for-sigint-and-sigterm">
  1874. <span id="asyncio-example-unix-signals"></span><h3>Set signal handlers for SIGINT and SIGTERM<a class="headerlink" href="#set-signal-handlers-for-sigint-and-sigterm" title="Link to this heading">¶</a></h3>
  1875. <p>(This <code class="docutils literal notranslate"><span class="pre">signals</span></code> example only works on Unix.)</p>
  1876. <p>Register handlers for signals <a class="reference internal" href="signal.html#signal.SIGINT" title="signal.SIGINT"><code class="xref py py-const docutils literal notranslate"><span class="pre">SIGINT</span></code></a> and <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>
  1877. using the <a class="reference internal" href="#asyncio.loop.add_signal_handler" title="asyncio.loop.add_signal_handler"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.add_signal_handler()</span></code></a> method:</p>
  1878. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
  1879. <span class="kn">import</span> <span class="nn">functools</span>
  1880. <span class="kn">import</span> <span class="nn">os</span>
  1881. <span class="kn">import</span> <span class="nn">signal</span>
  1882. <span class="k">def</span> <span class="nf">ask_exit</span><span class="p">(</span><span class="n">signame</span><span class="p">,</span> <span class="n">loop</span><span class="p">):</span>
  1883. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;got signal </span><span class="si">%s</span><span class="s2">: exit&quot;</span> <span class="o">%</span> <span class="n">signame</span><span class="p">)</span>
  1884. <span class="n">loop</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  1885. <span class="k">async</span> <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
  1886. <span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_running_loop</span><span class="p">()</span>
  1887. <span class="k">for</span> <span class="n">signame</span> <span class="ow">in</span> <span class="p">{</span><span class="s1">&#39;SIGINT&#39;</span><span class="p">,</span> <span class="s1">&#39;SIGTERM&#39;</span><span class="p">}:</span>
  1888. <span class="n">loop</span><span class="o">.</span><span class="n">add_signal_handler</span><span class="p">(</span>
  1889. <span class="nb">getattr</span><span class="p">(</span><span class="n">signal</span><span class="p">,</span> <span class="n">signame</span><span class="p">),</span>
  1890. <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="n">ask_exit</span><span class="p">,</span> <span class="n">signame</span><span class="p">,</span> <span class="n">loop</span><span class="p">))</span>
  1891. <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">3600</span><span class="p">)</span>
  1892. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Event loop running for 1 hour, press Ctrl+C to interrupt.&quot;</span><span class="p">)</span>
  1893. <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;pid </span><span class="si">{</span><span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">()</span><span class="si">}</span><span class="s2">: send SIGINT or SIGTERM to exit.&quot;</span><span class="p">)</span>
  1894. <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>
  1895. </pre></div>
  1896. </div>
  1897. </section>
  1898. </section>
  1899. </section>
  1900. <div class="clearer"></div>
  1901. </div>
  1902. </div>
  1903. </div>
  1904. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1905. <div class="sphinxsidebarwrapper">
  1906. <div>
  1907. <h3><a href="../contents.html">Table of Contents</a></h3>
  1908. <ul>
  1909. <li><a class="reference internal" href="#">Event Loop</a><ul>
  1910. <li><a class="reference internal" href="#event-loop-methods">Event Loop Methods</a><ul>
  1911. <li><a class="reference internal" href="#running-and-stopping-the-loop">Running and stopping the loop</a></li>
  1912. <li><a class="reference internal" href="#scheduling-callbacks">Scheduling callbacks</a></li>
  1913. <li><a class="reference internal" href="#scheduling-delayed-callbacks">Scheduling delayed callbacks</a></li>
  1914. <li><a class="reference internal" href="#creating-futures-and-tasks">Creating Futures and Tasks</a></li>
  1915. <li><a class="reference internal" href="#opening-network-connections">Opening network connections</a></li>
  1916. <li><a class="reference internal" href="#creating-network-servers">Creating network servers</a></li>
  1917. <li><a class="reference internal" href="#transferring-files">Transferring files</a></li>
  1918. <li><a class="reference internal" href="#tls-upgrade">TLS Upgrade</a></li>
  1919. <li><a class="reference internal" href="#watching-file-descriptors">Watching file descriptors</a></li>
  1920. <li><a class="reference internal" href="#working-with-socket-objects-directly">Working with socket objects directly</a></li>
  1921. <li><a class="reference internal" href="#dns">DNS</a></li>
  1922. <li><a class="reference internal" href="#working-with-pipes">Working with pipes</a></li>
  1923. <li><a class="reference internal" href="#unix-signals">Unix signals</a></li>
  1924. <li><a class="reference internal" href="#executing-code-in-thread-or-process-pools">Executing code in thread or process pools</a></li>
  1925. <li><a class="reference internal" href="#error-handling-api">Error Handling API</a></li>
  1926. <li><a class="reference internal" href="#enabling-debug-mode">Enabling debug mode</a></li>
  1927. <li><a class="reference internal" href="#running-subprocesses">Running Subprocesses</a></li>
  1928. </ul>
  1929. </li>
  1930. <li><a class="reference internal" href="#callback-handles">Callback Handles</a></li>
  1931. <li><a class="reference internal" href="#server-objects">Server Objects</a></li>
  1932. <li><a class="reference internal" href="#event-loop-implementations">Event Loop Implementations</a></li>
  1933. <li><a class="reference internal" href="#examples">Examples</a><ul>
  1934. <li><a class="reference internal" href="#hello-world-with-call-soon">Hello World with call_soon()</a></li>
  1935. <li><a class="reference internal" href="#display-the-current-date-with-call-later">Display the current date with call_later()</a></li>
  1936. <li><a class="reference internal" href="#watch-a-file-descriptor-for-read-events">Watch a file descriptor for read events</a></li>
  1937. <li><a class="reference internal" href="#set-signal-handlers-for-sigint-and-sigterm">Set signal handlers for SIGINT and SIGTERM</a></li>
  1938. </ul>
  1939. </li>
  1940. </ul>
  1941. </li>
  1942. </ul>
  1943. </div>
  1944. <div>
  1945. <h4>Previous topic</h4>
  1946. <p class="topless"><a href="asyncio-exceptions.html"
  1947. title="previous chapter">Exceptions</a></p>
  1948. </div>
  1949. <div>
  1950. <h4>Next topic</h4>
  1951. <p class="topless"><a href="asyncio-future.html"
  1952. title="next chapter">Futures</a></p>
  1953. </div>
  1954. <div role="note" aria-label="source link">
  1955. <h3>This Page</h3>
  1956. <ul class="this-page-menu">
  1957. <li><a href="../bugs.html">Report a Bug</a></li>
  1958. <li>
  1959. <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio-eventloop.rst"
  1960. rel="nofollow">Show Source
  1961. </a>
  1962. </li>
  1963. </ul>
  1964. </div>
  1965. </div>
  1966. <div id="sidebarbutton" title="Collapse sidebar">
  1967. <span>«</span>
  1968. </div>
  1969. </div>
  1970. <div class="clearer"></div>
  1971. </div>
  1972. <div class="related" role="navigation" aria-label="related navigation">
  1973. <h3>Navigation</h3>
  1974. <ul>
  1975. <li class="right" style="margin-right: 10px">
  1976. <a href="../genindex.html" title="General Index"
  1977. >index</a></li>
  1978. <li class="right" >
  1979. <a href="../py-modindex.html" title="Python Module Index"
  1980. >modules</a> |</li>
  1981. <li class="right" >
  1982. <a href="asyncio-future.html" title="Futures"
  1983. >next</a> |</li>
  1984. <li class="right" >
  1985. <a href="asyncio-exceptions.html" title="Exceptions"
  1986. >previous</a> |</li>
  1987. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1988. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1989. <li class="switchers">
  1990. <div class="language_switcher_placeholder"></div>
  1991. <div class="version_switcher_placeholder"></div>
  1992. </li>
  1993. <li>
  1994. </li>
  1995. <li id="cpython-language-and-version">
  1996. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1997. </li>
  1998. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1999. <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
  2000. <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>
  2001. <li class="nav-item nav-item-this"><a href="">Event Loop</a></li>
  2002. <li class="right">
  2003. <div class="inline-search" role="search">
  2004. <form class="inline-search" action="../search.html" method="get">
  2005. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  2006. <input type="submit" value="Go" />
  2007. </form>
  2008. </div>
  2009. |
  2010. </li>
  2011. <li class="right">
  2012. <label class="theme-selector-label">
  2013. Theme
  2014. <select class="theme-selector" oninput="activateTheme(this.value)">
  2015. <option value="auto" selected>Auto</option>
  2016. <option value="light">Light</option>
  2017. <option value="dark">Dark</option>
  2018. </select>
  2019. </label> |</li>
  2020. </ul>
  2021. </div>
  2022. <div class="footer">
  2023. &copy;
  2024. <a href="../copyright.html">
  2025. Copyright
  2026. </a>
  2027. 2001-2024, Python Software Foundation.
  2028. <br />
  2029. This page is licensed under the Python Software Foundation License Version 2.
  2030. <br />
  2031. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  2032. <br />
  2033. See <a href="/license.html">History and License</a> for more information.<br />
  2034. <br />
  2035. The Python Software Foundation is a non-profit corporation.
  2036. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  2037. <br />
  2038. <br />
  2039. Last updated on Apr 09, 2024 (13:47 UTC).
  2040. <a href="/bugs.html">Found a bug</a>?
  2041. <br />
  2042. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  2043. </div>
  2044. </body>
  2045. </html>
上海开阖软件有限公司 沪ICP备12045867号-1