gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

804 lines
57KB

  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="Synchronization Primitives" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/asyncio-sync.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/asyncio/locks.py asyncio synchronization primitives are designed to be similar to those of the threading module with two important caveats: asyncio primitives are not thread-safe, ..." />
  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/locks.py asyncio synchronization primitives are designed to be similar to those of the threading module with two important caveats: asyncio primitives are not thread-safe, ..." />
  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>Synchronization Primitives &#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="Subprocesses" href="asyncio-subprocess.html" />
  33. <link rel="prev" title="Streams" href="asyncio-stream.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/asyncio-sync.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="#">Synchronization Primitives</a><ul>
  86. <li><a class="reference internal" href="#lock">Lock</a></li>
  87. <li><a class="reference internal" href="#event">Event</a></li>
  88. <li><a class="reference internal" href="#condition">Condition</a></li>
  89. <li><a class="reference internal" href="#semaphore">Semaphore</a></li>
  90. <li><a class="reference internal" href="#boundedsemaphore">BoundedSemaphore</a></li>
  91. <li><a class="reference internal" href="#barrier">Barrier</a></li>
  92. </ul>
  93. </li>
  94. </ul>
  95. </div>
  96. <div>
  97. <h4>Previous topic</h4>
  98. <p class="topless"><a href="asyncio-stream.html"
  99. title="previous chapter">Streams</a></p>
  100. </div>
  101. <div>
  102. <h4>Next topic</h4>
  103. <p class="topless"><a href="asyncio-subprocess.html"
  104. title="next chapter">Subprocesses</a></p>
  105. </div>
  106. <div role="note" aria-label="source link">
  107. <h3>This Page</h3>
  108. <ul class="this-page-menu">
  109. <li><a href="../bugs.html">Report a Bug</a></li>
  110. <li>
  111. <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio-sync.rst"
  112. rel="nofollow">Show Source
  113. </a>
  114. </li>
  115. </ul>
  116. </div>
  117. </nav>
  118. </div>
  119. </div>
  120. <div class="related" role="navigation" aria-label="related navigation">
  121. <h3>Navigation</h3>
  122. <ul>
  123. <li class="right" style="margin-right: 10px">
  124. <a href="../genindex.html" title="General Index"
  125. accesskey="I">index</a></li>
  126. <li class="right" >
  127. <a href="../py-modindex.html" title="Python Module Index"
  128. >modules</a> |</li>
  129. <li class="right" >
  130. <a href="asyncio-subprocess.html" title="Subprocesses"
  131. accesskey="N">next</a> |</li>
  132. <li class="right" >
  133. <a href="asyncio-stream.html" title="Streams"
  134. accesskey="P">previous</a> |</li>
  135. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  136. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  137. <li class="switchers">
  138. <div class="language_switcher_placeholder"></div>
  139. <div class="version_switcher_placeholder"></div>
  140. </li>
  141. <li>
  142. </li>
  143. <li id="cpython-language-and-version">
  144. <a href="../index.html">3.12.3 Documentation</a> &#187;
  145. </li>
  146. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  147. <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
  148. <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>
  149. <li class="nav-item nav-item-this"><a href="">Synchronization Primitives</a></li>
  150. <li class="right">
  151. <div class="inline-search" role="search">
  152. <form class="inline-search" action="../search.html" method="get">
  153. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  154. <input type="submit" value="Go" />
  155. </form>
  156. </div>
  157. |
  158. </li>
  159. <li class="right">
  160. <label class="theme-selector-label">
  161. Theme
  162. <select class="theme-selector" oninput="activateTheme(this.value)">
  163. <option value="auto" selected>Auto</option>
  164. <option value="light">Light</option>
  165. <option value="dark">Dark</option>
  166. </select>
  167. </label> |</li>
  168. </ul>
  169. </div>
  170. <div class="document">
  171. <div class="documentwrapper">
  172. <div class="bodywrapper">
  173. <div class="body" role="main">
  174. <section id="synchronization-primitives">
  175. <span id="asyncio-sync"></span><h1>Synchronization Primitives<a class="headerlink" href="#synchronization-primitives" title="Link to this heading">¶</a></h1>
  176. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/asyncio/locks.py">Lib/asyncio/locks.py</a></p>
  177. <hr class="docutils" />
  178. <p>asyncio synchronization primitives are designed to be similar to
  179. those of the <a class="reference internal" href="threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module with two important caveats:</p>
  180. <ul class="simple">
  181. <li><p>asyncio primitives are not thread-safe, therefore they should not
  182. be used for OS thread synchronization (use <a class="reference internal" href="threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> for
  183. that);</p></li>
  184. <li><p>methods of these synchronization primitives do not accept the <em>timeout</em>
  185. argument; use the <a class="reference internal" href="asyncio-task.html#asyncio.wait_for" title="asyncio.wait_for"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.wait_for()</span></code></a> function to perform
  186. operations with timeouts.</p></li>
  187. </ul>
  188. <p>asyncio has the following basic synchronization primitives:</p>
  189. <ul class="simple">
  190. <li><p><a class="reference internal" href="#asyncio.Lock" title="asyncio.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">Lock</span></code></a></p></li>
  191. <li><p><a class="reference internal" href="#asyncio.Event" title="asyncio.Event"><code class="xref py py-class docutils literal notranslate"><span class="pre">Event</span></code></a></p></li>
  192. <li><p><a class="reference internal" href="#asyncio.Condition" title="asyncio.Condition"><code class="xref py py-class docutils literal notranslate"><span class="pre">Condition</span></code></a></p></li>
  193. <li><p><a class="reference internal" href="#asyncio.Semaphore" title="asyncio.Semaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">Semaphore</span></code></a></p></li>
  194. <li><p><a class="reference internal" href="#asyncio.BoundedSemaphore" title="asyncio.BoundedSemaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">BoundedSemaphore</span></code></a></p></li>
  195. <li><p><a class="reference internal" href="#asyncio.Barrier" title="asyncio.Barrier"><code class="xref py py-class docutils literal notranslate"><span class="pre">Barrier</span></code></a></p></li>
  196. </ul>
  197. <hr class="docutils" />
  198. <section id="lock">
  199. <h2>Lock<a class="headerlink" href="#lock" title="Link to this heading">¶</a></h2>
  200. <dl class="py class">
  201. <dt class="sig sig-object py" id="asyncio.Lock">
  202. <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">Lock</span></span><a class="headerlink" href="#asyncio.Lock" title="Link to this definition">¶</a></dt>
  203. <dd><p>Implements a mutex lock for asyncio tasks. Not thread-safe.</p>
  204. <p>An asyncio lock can be used to guarantee exclusive access to a
  205. shared resource.</p>
  206. <p>The preferred way to use a Lock is an <a class="reference internal" href="../reference/compound_stmts.html#async-with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">with</span></code></a>
  207. statement:</p>
  208. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">lock</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">Lock</span><span class="p">()</span>
  209. <span class="c1"># ... later</span>
  210. <span class="k">async</span> <span class="k">with</span> <span class="n">lock</span><span class="p">:</span>
  211. <span class="c1"># access shared state</span>
  212. </pre></div>
  213. </div>
  214. <p>which is equivalent to:</p>
  215. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">lock</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">Lock</span><span class="p">()</span>
  216. <span class="c1"># ... later</span>
  217. <span class="k">await</span> <span class="n">lock</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
  218. <span class="k">try</span><span class="p">:</span>
  219. <span class="c1"># access shared state</span>
  220. <span class="k">finally</span><span class="p">:</span>
  221. <span class="n">lock</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
  222. </pre></div>
  223. </div>
  224. <div class="versionchanged">
  225. <p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p>
  226. </div>
  227. <dl class="py method">
  228. <dt class="sig sig-object py" id="asyncio.Lock.acquire">
  229. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-name descname"><span class="pre">acquire</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Lock.acquire" title="Link to this definition">¶</a></dt>
  230. <dd><p>Acquire the lock.</p>
  231. <p>This method waits until the lock is <em>unlocked</em>, sets it to
  232. <em>locked</em> and returns <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  233. <p>When more than one coroutine is blocked in <a class="reference internal" href="#asyncio.Lock.acquire" title="asyncio.Lock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a>
  234. waiting for the lock to be unlocked, only one coroutine
  235. eventually proceeds.</p>
  236. <p>Acquiring a lock is <em>fair</em>: the coroutine that proceeds will be
  237. the first coroutine that started waiting on the lock.</p>
  238. </dd></dl>
  239. <dl class="py method">
  240. <dt class="sig sig-object py" id="asyncio.Lock.release">
  241. <span class="sig-name descname"><span class="pre">release</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Lock.release" title="Link to this definition">¶</a></dt>
  242. <dd><p>Release the lock.</p>
  243. <p>When the lock is <em>locked</em>, reset it to <em>unlocked</em> and return.</p>
  244. <p>If the lock is <em>unlocked</em>, 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> is raised.</p>
  245. </dd></dl>
  246. <dl class="py method">
  247. <dt class="sig sig-object py" id="asyncio.Lock.locked">
  248. <span class="sig-name descname"><span class="pre">locked</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Lock.locked" title="Link to this definition">¶</a></dt>
  249. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the lock is <em>locked</em>.</p>
  250. </dd></dl>
  251. </dd></dl>
  252. </section>
  253. <section id="event">
  254. <h2>Event<a class="headerlink" href="#event" title="Link to this heading">¶</a></h2>
  255. <dl class="py class">
  256. <dt class="sig sig-object py" id="asyncio.Event">
  257. <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">Event</span></span><a class="headerlink" href="#asyncio.Event" title="Link to this definition">¶</a></dt>
  258. <dd><p>An event object. Not thread-safe.</p>
  259. <p>An asyncio event can be used to notify multiple asyncio tasks
  260. that some event has happened.</p>
  261. <p>An Event object manages an internal flag that can be set to <em>true</em>
  262. with the <a class="reference internal" href="#asyncio.Event.set" title="asyncio.Event.set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code></a> method and reset to <em>false</em> with the
  263. <a class="reference internal" href="#asyncio.Event.clear" title="asyncio.Event.clear"><code class="xref py py-meth docutils literal notranslate"><span class="pre">clear()</span></code></a> method. The <a class="reference internal" href="#asyncio.Event.wait" title="asyncio.Event.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> method blocks until the
  264. flag is set to <em>true</em>. The flag is set to <em>false</em> initially.</p>
  265. <div class="versionchanged">
  266. <p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p>
  267. </div>
  268. <p id="asyncio-example-sync-event">Example:</p>
  269. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">async</span> <span class="k">def</span> <span class="nf">waiter</span><span class="p">(</span><span class="n">event</span><span class="p">):</span>
  270. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;waiting for it ...&#39;</span><span class="p">)</span>
  271. <span class="k">await</span> <span class="n">event</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
  272. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... got it!&#39;</span><span class="p">)</span>
  273. <span class="k">async</span> <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
  274. <span class="c1"># Create an Event object.</span>
  275. <span class="n">event</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">Event</span><span class="p">()</span>
  276. <span class="c1"># Spawn a Task to wait until &#39;event&#39; is set.</span>
  277. <span class="n">waiter_task</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">create_task</span><span class="p">(</span><span class="n">waiter</span><span class="p">(</span><span class="n">event</span><span class="p">))</span>
  278. <span class="c1"># Sleep for 1 second and set the event.</span>
  279. <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>
  280. <span class="n">event</span><span class="o">.</span><span class="n">set</span><span class="p">()</span>
  281. <span class="c1"># Wait until the waiter task is finished.</span>
  282. <span class="k">await</span> <span class="n">waiter_task</span>
  283. <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>
  284. </pre></div>
  285. </div>
  286. <dl class="py method">
  287. <dt class="sig sig-object py" id="asyncio.Event.wait">
  288. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-name descname"><span class="pre">wait</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Event.wait" title="Link to this definition">¶</a></dt>
  289. <dd><p>Wait until the event is set.</p>
  290. <p>If the event is set, return <code class="docutils literal notranslate"><span class="pre">True</span></code> immediately.
  291. Otherwise block until another task calls <a class="reference internal" href="#asyncio.Event.set" title="asyncio.Event.set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code></a>.</p>
  292. </dd></dl>
  293. <dl class="py method">
  294. <dt class="sig sig-object py" id="asyncio.Event.set">
  295. <span class="sig-name descname"><span class="pre">set</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Event.set" title="Link to this definition">¶</a></dt>
  296. <dd><p>Set the event.</p>
  297. <p>All tasks waiting for event to be set will be immediately
  298. awakened.</p>
  299. </dd></dl>
  300. <dl class="py method">
  301. <dt class="sig sig-object py" id="asyncio.Event.clear">
  302. <span class="sig-name descname"><span class="pre">clear</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Event.clear" title="Link to this definition">¶</a></dt>
  303. <dd><p>Clear (unset) the event.</p>
  304. <p>Tasks awaiting on <a class="reference internal" href="#asyncio.Event.wait" title="asyncio.Event.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> will now block until the
  305. <a class="reference internal" href="#asyncio.Event.set" title="asyncio.Event.set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code></a> method is called again.</p>
  306. </dd></dl>
  307. <dl class="py method">
  308. <dt class="sig sig-object py" id="asyncio.Event.is_set">
  309. <span class="sig-name descname"><span class="pre">is_set</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Event.is_set" title="Link to this definition">¶</a></dt>
  310. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the event is set.</p>
  311. </dd></dl>
  312. </dd></dl>
  313. </section>
  314. <section id="condition">
  315. <h2>Condition<a class="headerlink" href="#condition" title="Link to this heading">¶</a></h2>
  316. <dl class="py class">
  317. <dt class="sig sig-object py" id="asyncio.Condition">
  318. <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">Condition</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">lock</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.Condition" title="Link to this definition">¶</a></dt>
  319. <dd><p>A Condition object. Not thread-safe.</p>
  320. <p>An asyncio condition primitive can be used by a task to wait for
  321. some event to happen and then get exclusive access to a shared
  322. resource.</p>
  323. <p>In essence, a Condition object combines the functionality
  324. of an <a class="reference internal" href="#asyncio.Event" title="asyncio.Event"><code class="xref py py-class docutils literal notranslate"><span class="pre">Event</span></code></a> and a <a class="reference internal" href="#asyncio.Lock" title="asyncio.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">Lock</span></code></a>. It is possible to have
  325. multiple Condition objects share one Lock, which allows coordinating
  326. exclusive access to a shared resource between different tasks
  327. interested in particular states of that shared resource.</p>
  328. <p>The optional <em>lock</em> argument must be a <a class="reference internal" href="#asyncio.Lock" title="asyncio.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">Lock</span></code></a> object or
  329. <code class="docutils literal notranslate"><span class="pre">None</span></code>. In the latter case a new Lock object is created
  330. automatically.</p>
  331. <div class="versionchanged">
  332. <p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p>
  333. </div>
  334. <p>The preferred way to use a Condition is an <a class="reference internal" href="../reference/compound_stmts.html#async-with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">with</span></code></a>
  335. statement:</p>
  336. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">cond</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">Condition</span><span class="p">()</span>
  337. <span class="c1"># ... later</span>
  338. <span class="k">async</span> <span class="k">with</span> <span class="n">cond</span><span class="p">:</span>
  339. <span class="k">await</span> <span class="n">cond</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
  340. </pre></div>
  341. </div>
  342. <p>which is equivalent to:</p>
  343. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">cond</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">Condition</span><span class="p">()</span>
  344. <span class="c1"># ... later</span>
  345. <span class="k">await</span> <span class="n">cond</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
  346. <span class="k">try</span><span class="p">:</span>
  347. <span class="k">await</span> <span class="n">cond</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
  348. <span class="k">finally</span><span class="p">:</span>
  349. <span class="n">cond</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
  350. </pre></div>
  351. </div>
  352. <dl class="py method">
  353. <dt class="sig sig-object py" id="asyncio.Condition.acquire">
  354. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-name descname"><span class="pre">acquire</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Condition.acquire" title="Link to this definition">¶</a></dt>
  355. <dd><p>Acquire the underlying lock.</p>
  356. <p>This method waits until the underlying lock is <em>unlocked</em>,
  357. sets it to <em>locked</em> and returns <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  358. </dd></dl>
  359. <dl class="py method">
  360. <dt class="sig sig-object py" id="asyncio.Condition.notify">
  361. <span class="sig-name descname"><span class="pre">notify</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Condition.notify" title="Link to this definition">¶</a></dt>
  362. <dd><p>Wake up at most <em>n</em> tasks (1 by default) waiting on this
  363. condition. The method is no-op if no tasks are waiting.</p>
  364. <p>The lock must be acquired before this method is called and
  365. released shortly after. If called with an <em>unlocked</em> lock
  366. 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> error is raised.</p>
  367. </dd></dl>
  368. <dl class="py method">
  369. <dt class="sig sig-object py" id="asyncio.Condition.locked">
  370. <span class="sig-name descname"><span class="pre">locked</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Condition.locked" title="Link to this definition">¶</a></dt>
  371. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the underlying lock is acquired.</p>
  372. </dd></dl>
  373. <dl class="py method">
  374. <dt class="sig sig-object py" id="asyncio.Condition.notify_all">
  375. <span class="sig-name descname"><span class="pre">notify_all</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Condition.notify_all" title="Link to this definition">¶</a></dt>
  376. <dd><p>Wake up all tasks waiting on this condition.</p>
  377. <p>This method acts like <a class="reference internal" href="#asyncio.Condition.notify" title="asyncio.Condition.notify"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify()</span></code></a>, but wakes up all waiting
  378. tasks.</p>
  379. <p>The lock must be acquired before this method is called and
  380. released shortly after. If called with an <em>unlocked</em> lock
  381. 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> error is raised.</p>
  382. </dd></dl>
  383. <dl class="py method">
  384. <dt class="sig sig-object py" id="asyncio.Condition.release">
  385. <span class="sig-name descname"><span class="pre">release</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Condition.release" title="Link to this definition">¶</a></dt>
  386. <dd><p>Release the underlying lock.</p>
  387. <p>When invoked on an unlocked lock, 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> is
  388. raised.</p>
  389. </dd></dl>
  390. <dl class="py method">
  391. <dt class="sig sig-object py" id="asyncio.Condition.wait">
  392. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-name descname"><span class="pre">wait</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Condition.wait" title="Link to this definition">¶</a></dt>
  393. <dd><p>Wait until notified.</p>
  394. <p>If the calling task has not acquired the lock when this method is
  395. called, 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> is raised.</p>
  396. <p>This method releases the underlying lock, and then blocks until
  397. it is awakened by a <a class="reference internal" href="#asyncio.Condition.notify" title="asyncio.Condition.notify"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify()</span></code></a> or <a class="reference internal" href="#asyncio.Condition.notify_all" title="asyncio.Condition.notify_all"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify_all()</span></code></a> call.
  398. Once awakened, the Condition re-acquires its lock and this method
  399. returns <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  400. </dd></dl>
  401. <dl class="py method">
  402. <dt class="sig sig-object py" id="asyncio.Condition.wait_for">
  403. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-name descname"><span class="pre">wait_for</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">predicate</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Condition.wait_for" title="Link to this definition">¶</a></dt>
  404. <dd><p>Wait until a predicate becomes <em>true</em>.</p>
  405. <p>The predicate must be a callable which result will be
  406. interpreted as a boolean value. The final value is the
  407. return value.</p>
  408. </dd></dl>
  409. </dd></dl>
  410. </section>
  411. <section id="semaphore">
  412. <h2>Semaphore<a class="headerlink" href="#semaphore" title="Link to this heading">¶</a></h2>
  413. <dl class="py class">
  414. <dt class="sig sig-object py" id="asyncio.Semaphore">
  415. <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">Semaphore</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">value</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Semaphore" title="Link to this definition">¶</a></dt>
  416. <dd><p>A Semaphore object. Not thread-safe.</p>
  417. <p>A semaphore manages an internal counter which is decremented by each
  418. <a class="reference internal" href="#asyncio.Semaphore.acquire" title="asyncio.Semaphore.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> call and incremented by each <a class="reference internal" href="#asyncio.Semaphore.release" title="asyncio.Semaphore.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> call.
  419. The counter can never go below zero; when <a class="reference internal" href="#asyncio.Semaphore.acquire" title="asyncio.Semaphore.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> finds
  420. that it is zero, it blocks, waiting until some task calls
  421. <a class="reference internal" href="#asyncio.Semaphore.release" title="asyncio.Semaphore.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a>.</p>
  422. <p>The optional <em>value</em> argument gives the initial value for the
  423. internal counter (<code class="docutils literal notranslate"><span class="pre">1</span></code> by default). If the given value is
  424. less than <code class="docutils literal notranslate"><span class="pre">0</span></code> a <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> is raised.</p>
  425. <div class="versionchanged">
  426. <p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p>
  427. </div>
  428. <p>The preferred way to use a Semaphore is an <a class="reference internal" href="../reference/compound_stmts.html#async-with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">with</span></code></a>
  429. statement:</p>
  430. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">sem</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">Semaphore</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
  431. <span class="c1"># ... later</span>
  432. <span class="k">async</span> <span class="k">with</span> <span class="n">sem</span><span class="p">:</span>
  433. <span class="c1"># work with shared resource</span>
  434. </pre></div>
  435. </div>
  436. <p>which is equivalent to:</p>
  437. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">sem</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">Semaphore</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
  438. <span class="c1"># ... later</span>
  439. <span class="k">await</span> <span class="n">sem</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
  440. <span class="k">try</span><span class="p">:</span>
  441. <span class="c1"># work with shared resource</span>
  442. <span class="k">finally</span><span class="p">:</span>
  443. <span class="n">sem</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
  444. </pre></div>
  445. </div>
  446. <dl class="py method">
  447. <dt class="sig sig-object py" id="asyncio.Semaphore.acquire">
  448. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-name descname"><span class="pre">acquire</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Semaphore.acquire" title="Link to this definition">¶</a></dt>
  449. <dd><p>Acquire a semaphore.</p>
  450. <p>If the internal counter is greater than zero, decrement
  451. it by one and return <code class="docutils literal notranslate"><span class="pre">True</span></code> immediately. If it is zero, wait
  452. until a <a class="reference internal" href="#asyncio.Semaphore.release" title="asyncio.Semaphore.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> is called and return <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  453. </dd></dl>
  454. <dl class="py method">
  455. <dt class="sig sig-object py" id="asyncio.Semaphore.locked">
  456. <span class="sig-name descname"><span class="pre">locked</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Semaphore.locked" title="Link to this definition">¶</a></dt>
  457. <dd><p>Returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if semaphore can not be acquired immediately.</p>
  458. </dd></dl>
  459. <dl class="py method">
  460. <dt class="sig sig-object py" id="asyncio.Semaphore.release">
  461. <span class="sig-name descname"><span class="pre">release</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Semaphore.release" title="Link to this definition">¶</a></dt>
  462. <dd><p>Release a semaphore, incrementing the internal counter by one.
  463. Can wake up a task waiting to acquire the semaphore.</p>
  464. <p>Unlike <a class="reference internal" href="#asyncio.BoundedSemaphore" title="asyncio.BoundedSemaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">BoundedSemaphore</span></code></a>, <a class="reference internal" href="#asyncio.Semaphore" title="asyncio.Semaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">Semaphore</span></code></a> allows
  465. making more <code class="docutils literal notranslate"><span class="pre">release()</span></code> calls than <code class="docutils literal notranslate"><span class="pre">acquire()</span></code> calls.</p>
  466. </dd></dl>
  467. </dd></dl>
  468. </section>
  469. <section id="boundedsemaphore">
  470. <h2>BoundedSemaphore<a class="headerlink" href="#boundedsemaphore" title="Link to this heading">¶</a></h2>
  471. <dl class="py class">
  472. <dt class="sig sig-object py" id="asyncio.BoundedSemaphore">
  473. <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">BoundedSemaphore</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">value</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BoundedSemaphore" title="Link to this definition">¶</a></dt>
  474. <dd><p>A bounded semaphore object. Not thread-safe.</p>
  475. <p>Bounded Semaphore is a version of <a class="reference internal" href="#asyncio.Semaphore" title="asyncio.Semaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">Semaphore</span></code></a> that raises
  476. a <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> in <a class="reference internal" href="#asyncio.Semaphore.release" title="asyncio.Semaphore.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> if it
  477. increases the internal counter above the initial <em>value</em>.</p>
  478. <div class="versionchanged">
  479. <p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p>
  480. </div>
  481. </dd></dl>
  482. </section>
  483. <section id="barrier">
  484. <h2>Barrier<a class="headerlink" href="#barrier" title="Link to this heading">¶</a></h2>
  485. <dl class="py class">
  486. <dt class="sig sig-object py" id="asyncio.Barrier">
  487. <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">Barrier</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">parties</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Barrier" title="Link to this definition">¶</a></dt>
  488. <dd><p>A barrier object. Not thread-safe.</p>
  489. <p>A barrier is a simple synchronization primitive that allows to block until
  490. <em>parties</em> number of tasks are waiting on it.
  491. Tasks can wait on the <a class="reference internal" href="#asyncio.Barrier.wait" title="asyncio.Barrier.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> method and would be blocked until
  492. the specified number of tasks end up waiting on <a class="reference internal" href="#asyncio.Barrier.wait" title="asyncio.Barrier.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a>.
  493. At that point all of the waiting tasks would unblock simultaneously.</p>
  494. <p><a class="reference internal" href="../reference/compound_stmts.html#async-with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">with</span></code></a> can be used as an alternative to awaiting on
  495. <a class="reference internal" href="#asyncio.Barrier.wait" title="asyncio.Barrier.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a>.</p>
  496. <p>The barrier can be reused any number of times.</p>
  497. <p id="asyncio-example-barrier">Example:</p>
  498. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">async</span> <span class="k">def</span> <span class="nf">example_barrier</span><span class="p">():</span>
  499. <span class="c1"># barrier with 3 parties</span>
  500. <span class="n">b</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">Barrier</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
  501. <span class="c1"># create 2 new waiting tasks</span>
  502. <span class="n">asyncio</span><span class="o">.</span><span class="n">create_task</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">wait</span><span class="p">())</span>
  503. <span class="n">asyncio</span><span class="o">.</span><span class="n">create_task</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">wait</span><span class="p">())</span>
  504. <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">0</span><span class="p">)</span>
  505. <span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
  506. <span class="c1"># The third .wait() call passes the barrier</span>
  507. <span class="k">await</span> <span class="n">b</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
  508. <span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
  509. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;barrier passed&quot;</span><span class="p">)</span>
  510. <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">0</span><span class="p">)</span>
  511. <span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
  512. <span class="n">asyncio</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">example_barrier</span><span class="p">())</span>
  513. </pre></div>
  514. </div>
  515. <p>Result of this example is:</p>
  516. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;</span><span class="n">asyncio</span><span class="o">.</span><span class="n">locks</span><span class="o">.</span><span class="n">Barrier</span> <span class="nb">object</span> <span class="n">at</span> <span class="mi">0</span><span class="n">x</span><span class="o">...</span> <span class="p">[</span><span class="n">filling</span><span class="p">,</span> <span class="n">waiters</span><span class="p">:</span><span class="mi">2</span><span class="o">/</span><span class="mi">3</span><span class="p">]</span><span class="o">&gt;</span>
  517. <span class="o">&lt;</span><span class="n">asyncio</span><span class="o">.</span><span class="n">locks</span><span class="o">.</span><span class="n">Barrier</span> <span class="nb">object</span> <span class="n">at</span> <span class="mi">0</span><span class="n">x</span><span class="o">...</span> <span class="p">[</span><span class="n">draining</span><span class="p">,</span> <span class="n">waiters</span><span class="p">:</span><span class="mi">0</span><span class="o">/</span><span class="mi">3</span><span class="p">]</span><span class="o">&gt;</span>
  518. <span class="n">barrier</span> <span class="n">passed</span>
  519. <span class="o">&lt;</span><span class="n">asyncio</span><span class="o">.</span><span class="n">locks</span><span class="o">.</span><span class="n">Barrier</span> <span class="nb">object</span> <span class="n">at</span> <span class="mi">0</span><span class="n">x</span><span class="o">...</span> <span class="p">[</span><span class="n">filling</span><span class="p">,</span> <span class="n">waiters</span><span class="p">:</span><span class="mi">0</span><span class="o">/</span><span class="mi">3</span><span class="p">]</span><span class="o">&gt;</span>
  520. </pre></div>
  521. </div>
  522. <div class="versionadded">
  523. <p><span class="versionmodified added">New in version 3.11.</span></p>
  524. </div>
  525. <dl class="py method">
  526. <dt class="sig sig-object py" id="asyncio.Barrier.wait">
  527. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-name descname"><span class="pre">wait</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Barrier.wait" title="Link to this definition">¶</a></dt>
  528. <dd><p>Pass the barrier. When all the tasks party to the barrier have called
  529. this function, they are all unblocked simultaneously.</p>
  530. <p>When a waiting or blocked task in the barrier is cancelled,
  531. this task exits the barrier which stays in the same state.
  532. If the state of the barrier is “filling”, the number of waiting task
  533. decreases by 1.</p>
  534. <p>The return value is an integer in the range of 0 to <code class="docutils literal notranslate"><span class="pre">parties-1</span></code>, different
  535. for each task. This can be used to select a task to do some special
  536. housekeeping, e.g.:</p>
  537. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">...</span>
  538. <span class="k">async</span> <span class="k">with</span> <span class="n">barrier</span> <span class="k">as</span> <span class="n">position</span><span class="p">:</span>
  539. <span class="k">if</span> <span class="n">position</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
  540. <span class="c1"># Only one task prints this</span>
  541. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;End of *draining phase*&#39;</span><span class="p">)</span>
  542. </pre></div>
  543. </div>
  544. <p>This method may raise a <a class="reference internal" href="#asyncio.BrokenBarrierError" title="asyncio.BrokenBarrierError"><code class="xref py py-class docutils literal notranslate"><span class="pre">BrokenBarrierError</span></code></a> exception if the
  545. barrier is broken or reset while a task is waiting.
  546. It could raise a <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">CancelledError</span></code></a> if a task is cancelled.</p>
  547. </dd></dl>
  548. <dl class="py method">
  549. <dt class="sig sig-object py" id="asyncio.Barrier.reset">
  550. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-name descname"><span class="pre">reset</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Barrier.reset" title="Link to this definition">¶</a></dt>
  551. <dd><p>Return the barrier to the default, empty state. Any tasks waiting on it
  552. will receive the <a class="reference internal" href="#asyncio.BrokenBarrierError" title="asyncio.BrokenBarrierError"><code class="xref py py-class docutils literal notranslate"><span class="pre">BrokenBarrierError</span></code></a> exception.</p>
  553. <p>If a barrier is broken it may be better to just leave it and create a new one.</p>
  554. </dd></dl>
  555. <dl class="py method">
  556. <dt class="sig sig-object py" id="asyncio.Barrier.abort">
  557. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-name descname"><span class="pre">abort</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Barrier.abort" title="Link to this definition">¶</a></dt>
  558. <dd><p>Put the barrier into a broken state. This causes any active or future
  559. calls to <a class="reference internal" href="asyncio-task.html#asyncio.wait" title="asyncio.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> to fail with the <a class="reference internal" href="#asyncio.BrokenBarrierError" title="asyncio.BrokenBarrierError"><code class="xref py py-class docutils literal notranslate"><span class="pre">BrokenBarrierError</span></code></a>.
  560. Use this for example if one of the tasks needs to abort, to avoid infinite
  561. waiting tasks.</p>
  562. </dd></dl>
  563. <dl class="py attribute">
  564. <dt class="sig sig-object py" id="asyncio.Barrier.parties">
  565. <span class="sig-name descname"><span class="pre">parties</span></span><a class="headerlink" href="#asyncio.Barrier.parties" title="Link to this definition">¶</a></dt>
  566. <dd><p>The number of tasks required to pass the barrier.</p>
  567. </dd></dl>
  568. <dl class="py attribute">
  569. <dt class="sig sig-object py" id="asyncio.Barrier.n_waiting">
  570. <span class="sig-name descname"><span class="pre">n_waiting</span></span><a class="headerlink" href="#asyncio.Barrier.n_waiting" title="Link to this definition">¶</a></dt>
  571. <dd><p>The number of tasks currently waiting in the barrier while filling.</p>
  572. </dd></dl>
  573. <dl class="py attribute">
  574. <dt class="sig sig-object py" id="asyncio.Barrier.broken">
  575. <span class="sig-name descname"><span class="pre">broken</span></span><a class="headerlink" href="#asyncio.Barrier.broken" title="Link to this definition">¶</a></dt>
  576. <dd><p>A boolean that is <code class="docutils literal notranslate"><span class="pre">True</span></code> if the barrier is in the broken state.</p>
  577. </dd></dl>
  578. </dd></dl>
  579. <dl class="py exception">
  580. <dt class="sig sig-object py" id="asyncio.BrokenBarrierError">
  581. <em class="property"><span class="pre">exception</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">BrokenBarrierError</span></span><a class="headerlink" href="#asyncio.BrokenBarrierError" title="Link to this definition">¶</a></dt>
  582. <dd><p>This exception, a subclass of <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>, is raised when the
  583. <a class="reference internal" href="#asyncio.Barrier" title="asyncio.Barrier"><code class="xref py py-class docutils literal notranslate"><span class="pre">Barrier</span></code></a> object is reset or broken.</p>
  584. </dd></dl>
  585. <hr class="docutils" />
  586. <div class="versionchanged">
  587. <p><span class="versionmodified changed">Changed in version 3.9: </span>Acquiring a lock using <code class="docutils literal notranslate"><span class="pre">await</span> <span class="pre">lock</span></code> or <code class="docutils literal notranslate"><span class="pre">yield</span> <span class="pre">from</span> <span class="pre">lock</span></code> and/or
  588. <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> statement (<code class="docutils literal notranslate"><span class="pre">with</span> <span class="pre">await</span> <span class="pre">lock</span></code>, <code class="docutils literal notranslate"><span class="pre">with</span> <span class="pre">(yield</span> <span class="pre">from</span>
  589. <span class="pre">lock)</span></code>) was removed. Use <code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">with</span> <span class="pre">lock</span></code> instead.</p>
  590. </div>
  591. </section>
  592. </section>
  593. <div class="clearer"></div>
  594. </div>
  595. </div>
  596. </div>
  597. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  598. <div class="sphinxsidebarwrapper">
  599. <div>
  600. <h3><a href="../contents.html">Table of Contents</a></h3>
  601. <ul>
  602. <li><a class="reference internal" href="#">Synchronization Primitives</a><ul>
  603. <li><a class="reference internal" href="#lock">Lock</a></li>
  604. <li><a class="reference internal" href="#event">Event</a></li>
  605. <li><a class="reference internal" href="#condition">Condition</a></li>
  606. <li><a class="reference internal" href="#semaphore">Semaphore</a></li>
  607. <li><a class="reference internal" href="#boundedsemaphore">BoundedSemaphore</a></li>
  608. <li><a class="reference internal" href="#barrier">Barrier</a></li>
  609. </ul>
  610. </li>
  611. </ul>
  612. </div>
  613. <div>
  614. <h4>Previous topic</h4>
  615. <p class="topless"><a href="asyncio-stream.html"
  616. title="previous chapter">Streams</a></p>
  617. </div>
  618. <div>
  619. <h4>Next topic</h4>
  620. <p class="topless"><a href="asyncio-subprocess.html"
  621. title="next chapter">Subprocesses</a></p>
  622. </div>
  623. <div role="note" aria-label="source link">
  624. <h3>This Page</h3>
  625. <ul class="this-page-menu">
  626. <li><a href="../bugs.html">Report a Bug</a></li>
  627. <li>
  628. <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio-sync.rst"
  629. rel="nofollow">Show Source
  630. </a>
  631. </li>
  632. </ul>
  633. </div>
  634. </div>
  635. <div id="sidebarbutton" title="Collapse sidebar">
  636. <span>«</span>
  637. </div>
  638. </div>
  639. <div class="clearer"></div>
  640. </div>
  641. <div class="related" role="navigation" aria-label="related navigation">
  642. <h3>Navigation</h3>
  643. <ul>
  644. <li class="right" style="margin-right: 10px">
  645. <a href="../genindex.html" title="General Index"
  646. >index</a></li>
  647. <li class="right" >
  648. <a href="../py-modindex.html" title="Python Module Index"
  649. >modules</a> |</li>
  650. <li class="right" >
  651. <a href="asyncio-subprocess.html" title="Subprocesses"
  652. >next</a> |</li>
  653. <li class="right" >
  654. <a href="asyncio-stream.html" title="Streams"
  655. >previous</a> |</li>
  656. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  657. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  658. <li class="switchers">
  659. <div class="language_switcher_placeholder"></div>
  660. <div class="version_switcher_placeholder"></div>
  661. </li>
  662. <li>
  663. </li>
  664. <li id="cpython-language-and-version">
  665. <a href="../index.html">3.12.3 Documentation</a> &#187;
  666. </li>
  667. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  668. <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
  669. <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>
  670. <li class="nav-item nav-item-this"><a href="">Synchronization Primitives</a></li>
  671. <li class="right">
  672. <div class="inline-search" role="search">
  673. <form class="inline-search" action="../search.html" method="get">
  674. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  675. <input type="submit" value="Go" />
  676. </form>
  677. </div>
  678. |
  679. </li>
  680. <li class="right">
  681. <label class="theme-selector-label">
  682. Theme
  683. <select class="theme-selector" oninput="activateTheme(this.value)">
  684. <option value="auto" selected>Auto</option>
  685. <option value="light">Light</option>
  686. <option value="dark">Dark</option>
  687. </select>
  688. </label> |</li>
  689. </ul>
  690. </div>
  691. <div class="footer">
  692. &copy;
  693. <a href="../copyright.html">
  694. Copyright
  695. </a>
  696. 2001-2024, Python Software Foundation.
  697. <br />
  698. This page is licensed under the Python Software Foundation License Version 2.
  699. <br />
  700. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  701. <br />
  702. See <a href="/license.html">History and License</a> for more information.<br />
  703. <br />
  704. The Python Software Foundation is a non-profit corporation.
  705. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  706. <br />
  707. <br />
  708. Last updated on Apr 09, 2024 (13:47 UTC).
  709. <a href="/bugs.html">Found a bug</a>?
  710. <br />
  711. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  712. </div>
  713. </body>
  714. </html>
上海开阖软件有限公司 沪ICP备12045867号-1