gooderp18绿色标准版
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

482 líneas
32KB

  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="Runners" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/asyncio-runner.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/asyncio/runners.py This section outlines high-level asyncio primitives to run asyncio code. They are built on top of an event loop with the aim to simplify async code usage for com..." />
  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/runners.py This section outlines high-level asyncio primitives to run asyncio code. They are built on top of an event loop with the aim to simplify async code usage for com..." />
  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>Runners &#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="Coroutines and Tasks" href="asyncio-task.html" />
  33. <link rel="prev" title="asyncio — Asynchronous I/O" href="asyncio.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/asyncio-runner.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="#">Runners</a><ul>
  86. <li><a class="reference internal" href="#running-an-asyncio-program">Running an asyncio Program</a></li>
  87. <li><a class="reference internal" href="#runner-context-manager">Runner context manager</a></li>
  88. <li><a class="reference internal" href="#handling-keyboard-interruption">Handling Keyboard Interruption</a></li>
  89. </ul>
  90. </li>
  91. </ul>
  92. </div>
  93. <div>
  94. <h4>Previous topic</h4>
  95. <p class="topless"><a href="asyncio.html"
  96. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code> — Asynchronous I/O</a></p>
  97. </div>
  98. <div>
  99. <h4>Next topic</h4>
  100. <p class="topless"><a href="asyncio-task.html"
  101. title="next chapter">Coroutines and Tasks</a></p>
  102. </div>
  103. <div role="note" aria-label="source link">
  104. <h3>This Page</h3>
  105. <ul class="this-page-menu">
  106. <li><a href="../bugs.html">Report a Bug</a></li>
  107. <li>
  108. <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio-runner.rst"
  109. rel="nofollow">Show Source
  110. </a>
  111. </li>
  112. </ul>
  113. </div>
  114. </nav>
  115. </div>
  116. </div>
  117. <div class="related" role="navigation" aria-label="related navigation">
  118. <h3>Navigation</h3>
  119. <ul>
  120. <li class="right" style="margin-right: 10px">
  121. <a href="../genindex.html" title="General Index"
  122. accesskey="I">index</a></li>
  123. <li class="right" >
  124. <a href="../py-modindex.html" title="Python Module Index"
  125. >modules</a> |</li>
  126. <li class="right" >
  127. <a href="asyncio-task.html" title="Coroutines and Tasks"
  128. accesskey="N">next</a> |</li>
  129. <li class="right" >
  130. <a href="asyncio.html" title="asyncio — Asynchronous I/O"
  131. accesskey="P">previous</a> |</li>
  132. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  133. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  134. <li class="switchers">
  135. <div class="language_switcher_placeholder"></div>
  136. <div class="version_switcher_placeholder"></div>
  137. </li>
  138. <li>
  139. </li>
  140. <li id="cpython-language-and-version">
  141. <a href="../index.html">3.12.3 Documentation</a> &#187;
  142. </li>
  143. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  144. <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
  145. <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>
  146. <li class="nav-item nav-item-this"><a href="">Runners</a></li>
  147. <li class="right">
  148. <div class="inline-search" role="search">
  149. <form class="inline-search" action="../search.html" method="get">
  150. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  151. <input type="submit" value="Go" />
  152. </form>
  153. </div>
  154. |
  155. </li>
  156. <li class="right">
  157. <label class="theme-selector-label">
  158. Theme
  159. <select class="theme-selector" oninput="activateTheme(this.value)">
  160. <option value="auto" selected>Auto</option>
  161. <option value="light">Light</option>
  162. <option value="dark">Dark</option>
  163. </select>
  164. </label> |</li>
  165. </ul>
  166. </div>
  167. <div class="document">
  168. <div class="documentwrapper">
  169. <div class="bodywrapper">
  170. <div class="body" role="main">
  171. <section id="runners">
  172. <h1>Runners<a class="headerlink" href="#runners" title="Link to this heading">¶</a></h1>
  173. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/asyncio/runners.py">Lib/asyncio/runners.py</a></p>
  174. <p>This section outlines high-level asyncio primitives to run asyncio code.</p>
  175. <p>They are built on top of an <a class="reference internal" href="asyncio-eventloop.html#asyncio-event-loop"><span class="std std-ref">event loop</span></a> with the aim
  176. to simplify async code usage for common wide-spread scenarios.</p>
  177. <nav class="contents local" id="contents">
  178. <ul class="simple">
  179. <li><p><a class="reference internal" href="#running-an-asyncio-program" id="id1">Running an asyncio Program</a></p></li>
  180. <li><p><a class="reference internal" href="#runner-context-manager" id="id2">Runner context manager</a></p></li>
  181. <li><p><a class="reference internal" href="#handling-keyboard-interruption" id="id3">Handling Keyboard Interruption</a></p></li>
  182. </ul>
  183. </nav>
  184. <section id="running-an-asyncio-program">
  185. <h2><a class="toc-backref" href="#id1" role="doc-backlink">Running an asyncio Program</a><a class="headerlink" href="#running-an-asyncio-program" title="Link to this heading">¶</a></h2>
  186. <dl class="py function">
  187. <dt class="sig sig-object py" id="asyncio.run">
  188. <span class="sig-prename descclassname"><span class="pre">asyncio.</span></span><span class="sig-name descname"><span class="pre">run</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">debug</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">loop_factory</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.run" title="Link to this definition">¶</a></dt>
  189. <dd><p>Execute the <a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">coroutine</span></a> <em>coro</em> and return the result.</p>
  190. <p>This function runs the passed coroutine, taking care of
  191. managing the asyncio event loop, <em>finalizing asynchronous
  192. generators</em>, and closing the executor.</p>
  193. <p>This function cannot be called when another asyncio event loop is
  194. running in the same thread.</p>
  195. <p>If <em>debug</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the event loop will be run in debug mode. <code class="docutils literal notranslate"><span class="pre">False</span></code> disables
  196. debug mode explicitly. <code class="docutils literal notranslate"><span class="pre">None</span></code> is used to respect the global
  197. <a class="reference internal" href="asyncio-dev.html#asyncio-debug-mode"><span class="std std-ref">Debug Mode</span></a> settings.</p>
  198. <p>If <em>loop_factory</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, it is used to create a new event loop;
  199. otherwise <a class="reference internal" href="asyncio-eventloop.html#asyncio.new_event_loop" title="asyncio.new_event_loop"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.new_event_loop()</span></code></a> is used. The loop is closed at the end.
  200. This function should be used as a main entry point for asyncio programs,
  201. and should ideally only be called once. It is recommended to use
  202. <em>loop_factory</em> to configure the event loop instead of policies.</p>
  203. <p>The executor is given a timeout duration of 5 minutes to shutdown.
  204. If the executor hasn’t finished within that duration, a warning is
  205. emitted and the executor is closed.</p>
  206. <p>Example:</p>
  207. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">async</span> <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
  208. <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  209. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;hello&#39;</span><span class="p">)</span>
  210. <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>
  211. </pre></div>
  212. </div>
  213. <div class="versionadded">
  214. <p><span class="versionmodified added">New in version 3.7.</span></p>
  215. </div>
  216. <div class="versionchanged">
  217. <p><span class="versionmodified changed">Changed in version 3.9: </span>Updated to use <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.shutdown_default_executor" title="asyncio.loop.shutdown_default_executor"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.shutdown_default_executor()</span></code></a>.</p>
  218. </div>
  219. <div class="versionchanged">
  220. <p><span class="versionmodified changed">Changed in version 3.10: </span><em>debug</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> by default to respect the global debug mode settings.</p>
  221. </div>
  222. <div class="versionchanged">
  223. <p><span class="versionmodified changed">Changed in version 3.12: </span>Added <em>loop_factory</em> parameter.</p>
  224. </div>
  225. </dd></dl>
  226. </section>
  227. <section id="runner-context-manager">
  228. <h2><a class="toc-backref" href="#id2" role="doc-backlink">Runner context manager</a><a class="headerlink" href="#runner-context-manager" title="Link to this heading">¶</a></h2>
  229. <dl class="py class">
  230. <dt class="sig sig-object py" id="asyncio.Runner">
  231. <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">Runner</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">debug</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">loop_factory</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.Runner" title="Link to this definition">¶</a></dt>
  232. <dd><p>A context manager that simplifies <em>multiple</em> async function calls in the same
  233. context.</p>
  234. <p>Sometimes several top-level async functions should be called in the same <a class="reference internal" href="asyncio-eventloop.html#asyncio-event-loop"><span class="std std-ref">event
  235. loop</span></a> and <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>.</p>
  236. <p>If <em>debug</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the event loop will be run in debug mode. <code class="docutils literal notranslate"><span class="pre">False</span></code> disables
  237. debug mode explicitly. <code class="docutils literal notranslate"><span class="pre">None</span></code> is used to respect the global
  238. <a class="reference internal" href="asyncio-dev.html#asyncio-debug-mode"><span class="std std-ref">Debug Mode</span></a> settings.</p>
  239. <p><em>loop_factory</em> could be used for overriding the loop creation.
  240. It is the responsibility of the <em>loop_factory</em> to set the created loop as the
  241. current one. By default <a class="reference internal" href="asyncio-eventloop.html#asyncio.new_event_loop" title="asyncio.new_event_loop"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.new_event_loop()</span></code></a> is used and set as
  242. current event loop with <a class="reference internal" href="asyncio-eventloop.html#asyncio.set_event_loop" title="asyncio.set_event_loop"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.set_event_loop()</span></code></a> if <em>loop_factory</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  243. <p>Basically, <a class="reference internal" href="#asyncio.run" title="asyncio.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.run()</span></code></a> example can be rewritten with the runner usage:</p>
  244. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">async</span> <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
  245. <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  246. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;hello&#39;</span><span class="p">)</span>
  247. <span class="k">with</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">Runner</span><span class="p">()</span> <span class="k">as</span> <span class="n">runner</span><span class="p">:</span>
  248. <span class="n">runner</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">main</span><span class="p">())</span>
  249. </pre></div>
  250. </div>
  251. <div class="versionadded">
  252. <p><span class="versionmodified added">New in version 3.11.</span></p>
  253. </div>
  254. <dl class="py method">
  255. <dt class="sig sig-object py" id="asyncio.Runner.run">
  256. <span class="sig-name descname"><span class="pre">run</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">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.Runner.run" title="Link to this definition">¶</a></dt>
  257. <dd><p>Run a <a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">coroutine</span></a> <em>coro</em> in the embedded loop.</p>
  258. <p>Return the coroutine’s result or raise its exception.</p>
  259. <p>An optional keyword-only <em>context</em> argument allows specifying a
  260. 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.
  261. The runner’s default context is used if <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  262. <p>This function cannot be called when another asyncio event loop is
  263. running in the same thread.</p>
  264. </dd></dl>
  265. <dl class="py method">
  266. <dt class="sig sig-object py" id="asyncio.Runner.close">
  267. <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.Runner.close" title="Link to this definition">¶</a></dt>
  268. <dd><p>Close the runner.</p>
  269. <p>Finalize asynchronous generators, shutdown default executor, close the event loop
  270. and release embedded <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>.</p>
  271. </dd></dl>
  272. <dl class="py method">
  273. <dt class="sig sig-object py" id="asyncio.Runner.get_loop">
  274. <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.Runner.get_loop" title="Link to this definition">¶</a></dt>
  275. <dd><p>Return the event loop associated with the runner instance.</p>
  276. </dd></dl>
  277. <div class="admonition note">
  278. <p class="admonition-title">Note</p>
  279. <p><a class="reference internal" href="#asyncio.Runner" title="asyncio.Runner"><code class="xref py py-class docutils literal notranslate"><span class="pre">Runner</span></code></a> uses the lazy initialization strategy, its constructor doesn’t
  280. initialize underlying low-level structures.</p>
  281. <p>Embedded <em>loop</em> and <em>context</em> are created at the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> body entering
  282. or the first call of <a class="reference internal" href="#asyncio.run" title="asyncio.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> or <a class="reference internal" href="#asyncio.Runner.get_loop" title="asyncio.Runner.get_loop"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_loop()</span></code></a>.</p>
  283. </div>
  284. </dd></dl>
  285. </section>
  286. <section id="handling-keyboard-interruption">
  287. <h2><a class="toc-backref" href="#id3" role="doc-backlink">Handling Keyboard Interruption</a><a class="headerlink" href="#handling-keyboard-interruption" title="Link to this heading">¶</a></h2>
  288. <div class="versionadded">
  289. <p><span class="versionmodified added">New in version 3.11.</span></p>
  290. </div>
  291. <p>When <a class="reference internal" href="signal.html#signal.SIGINT" title="signal.SIGINT"><code class="xref py py-const docutils literal notranslate"><span class="pre">signal.SIGINT</span></code></a> is raised by <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Ctrl</kbd>-<kbd class="kbd docutils literal notranslate">C</kbd></kbd>, <a class="reference internal" href="exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a>
  292. exception is raised in the main thread by default. However this doesn’t work with
  293. <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> because it can interrupt asyncio internals and can hang the program from
  294. exiting.</p>
  295. <p>To mitigate this issue, <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> handles <a class="reference internal" href="signal.html#signal.SIGINT" title="signal.SIGINT"><code class="xref py py-const docutils literal notranslate"><span class="pre">signal.SIGINT</span></code></a> as follows:</p>
  296. <ol class="arabic simple">
  297. <li><p><a class="reference internal" href="#asyncio.Runner.run" title="asyncio.Runner.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">asyncio.Runner.run()</span></code></a> installs a custom <a class="reference internal" href="signal.html#signal.SIGINT" title="signal.SIGINT"><code class="xref py py-const docutils literal notranslate"><span class="pre">signal.SIGINT</span></code></a> handler before
  298. any user code is executed and removes it when exiting from the function.</p></li>
  299. <li><p>The <a class="reference internal" href="#asyncio.Runner" title="asyncio.Runner"><code class="xref py py-class docutils literal notranslate"><span class="pre">Runner</span></code></a> creates the main task for the passed coroutine for its
  300. execution.</p></li>
  301. <li><p>When <a class="reference internal" href="signal.html#signal.SIGINT" title="signal.SIGINT"><code class="xref py py-const docutils literal notranslate"><span class="pre">signal.SIGINT</span></code></a> is raised by <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Ctrl</kbd>-<kbd class="kbd docutils literal notranslate">C</kbd></kbd>, the custom signal handler
  302. cancels the main task by calling <a class="reference internal" href="asyncio-task.html#asyncio.Task.cancel" title="asyncio.Task.cancel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">asyncio.Task.cancel()</span></code></a> which raises
  303. <a class="reference internal" href="asyncio-exceptions.html#asyncio.CancelledError" title="asyncio.CancelledError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">asyncio.CancelledError</span></code></a> inside the main task. This causes the Python stack
  304. to unwind, <code class="docutils literal notranslate"><span class="pre">try/except</span></code> and <code class="docutils literal notranslate"><span class="pre">try/finally</span></code> blocks can be used for resource
  305. cleanup. After the main task is cancelled, <a class="reference internal" href="#asyncio.Runner.run" title="asyncio.Runner.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">asyncio.Runner.run()</span></code></a> raises
  306. <a class="reference internal" href="exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a>.</p></li>
  307. <li><p>A user could write a tight loop which cannot be interrupted by
  308. <a class="reference internal" href="asyncio-task.html#asyncio.Task.cancel" title="asyncio.Task.cancel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">asyncio.Task.cancel()</span></code></a>, in which case the second following <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Ctrl</kbd>-<kbd class="kbd docutils literal notranslate">C</kbd></kbd>
  309. immediately raises the <a class="reference internal" href="exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a> without cancelling the main task.</p></li>
  310. </ol>
  311. </section>
  312. </section>
  313. <div class="clearer"></div>
  314. </div>
  315. </div>
  316. </div>
  317. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  318. <div class="sphinxsidebarwrapper">
  319. <div>
  320. <h3><a href="../contents.html">Table of Contents</a></h3>
  321. <ul>
  322. <li><a class="reference internal" href="#">Runners</a><ul>
  323. <li><a class="reference internal" href="#running-an-asyncio-program">Running an asyncio Program</a></li>
  324. <li><a class="reference internal" href="#runner-context-manager">Runner context manager</a></li>
  325. <li><a class="reference internal" href="#handling-keyboard-interruption">Handling Keyboard Interruption</a></li>
  326. </ul>
  327. </li>
  328. </ul>
  329. </div>
  330. <div>
  331. <h4>Previous topic</h4>
  332. <p class="topless"><a href="asyncio.html"
  333. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code> — Asynchronous I/O</a></p>
  334. </div>
  335. <div>
  336. <h4>Next topic</h4>
  337. <p class="topless"><a href="asyncio-task.html"
  338. title="next chapter">Coroutines and Tasks</a></p>
  339. </div>
  340. <div role="note" aria-label="source link">
  341. <h3>This Page</h3>
  342. <ul class="this-page-menu">
  343. <li><a href="../bugs.html">Report a Bug</a></li>
  344. <li>
  345. <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio-runner.rst"
  346. rel="nofollow">Show Source
  347. </a>
  348. </li>
  349. </ul>
  350. </div>
  351. </div>
  352. <div id="sidebarbutton" title="Collapse sidebar">
  353. <span>«</span>
  354. </div>
  355. </div>
  356. <div class="clearer"></div>
  357. </div>
  358. <div class="related" role="navigation" aria-label="related navigation">
  359. <h3>Navigation</h3>
  360. <ul>
  361. <li class="right" style="margin-right: 10px">
  362. <a href="../genindex.html" title="General Index"
  363. >index</a></li>
  364. <li class="right" >
  365. <a href="../py-modindex.html" title="Python Module Index"
  366. >modules</a> |</li>
  367. <li class="right" >
  368. <a href="asyncio-task.html" title="Coroutines and Tasks"
  369. >next</a> |</li>
  370. <li class="right" >
  371. <a href="asyncio.html" title="asyncio — Asynchronous I/O"
  372. >previous</a> |</li>
  373. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  374. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  375. <li class="switchers">
  376. <div class="language_switcher_placeholder"></div>
  377. <div class="version_switcher_placeholder"></div>
  378. </li>
  379. <li>
  380. </li>
  381. <li id="cpython-language-and-version">
  382. <a href="../index.html">3.12.3 Documentation</a> &#187;
  383. </li>
  384. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  385. <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
  386. <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>
  387. <li class="nav-item nav-item-this"><a href="">Runners</a></li>
  388. <li class="right">
  389. <div class="inline-search" role="search">
  390. <form class="inline-search" action="../search.html" method="get">
  391. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  392. <input type="submit" value="Go" />
  393. </form>
  394. </div>
  395. |
  396. </li>
  397. <li class="right">
  398. <label class="theme-selector-label">
  399. Theme
  400. <select class="theme-selector" oninput="activateTheme(this.value)">
  401. <option value="auto" selected>Auto</option>
  402. <option value="light">Light</option>
  403. <option value="dark">Dark</option>
  404. </select>
  405. </label> |</li>
  406. </ul>
  407. </div>
  408. <div class="footer">
  409. &copy;
  410. <a href="../copyright.html">
  411. Copyright
  412. </a>
  413. 2001-2024, Python Software Foundation.
  414. <br />
  415. This page is licensed under the Python Software Foundation License Version 2.
  416. <br />
  417. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  418. <br />
  419. See <a href="/license.html">History and License</a> for more information.<br />
  420. <br />
  421. The Python Software Foundation is a non-profit corporation.
  422. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  423. <br />
  424. <br />
  425. Last updated on Apr 09, 2024 (13:47 UTC).
  426. <a href="/bugs.html">Found a bug</a>?
  427. <br />
  428. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  429. </div>
  430. </body>
  431. </html>
上海开阖软件有限公司 沪ICP备12045867号-1