Team Ai
Apppublic

parthtamu/rag-code-assistant

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
asyncio-sync.html844 linesDownload Raw Back to docs
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4  <head>5    <meta charset="utf-8" />6    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="Synchronization Primitives" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/asyncio-sync.html" />10<meta property="og:site_name" content="Python documentation" />11<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, ..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_asyncio-sync_155db036.png" />15<meta property="og:image:alt" 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, ..." />16<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, ..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20    <title>Synchronization Primitives &#8212; Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21    22    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23    <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24    <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25    <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26    <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27    28    <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29    <script src="../_static/doctools.js?v=9bcbadda"></script>30    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31    <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32    33    <script src="../_static/sidebar.js"></script>34    35    <link rel="search" type="application/opensearchdescription+xml"36          title="Search within Python 3.15.0a6 documentation"37          href="../_static/opensearch.xml"/>38    <link rel="author" title="About these documents" href="../about.html" />39    <link rel="index" title="Index" href="../genindex.html" />40    <link rel="search" title="Search" href="../search.html" />41    <link rel="copyright" title="Copyright" href="../copyright.html" />42    <link rel="next" title="Subprocesses" href="asyncio-subprocess.html" />43    <link rel="prev" title="Streams" href="asyncio-stream.html" />44    45      46      <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47      48      <link rel="canonical" href="https://docs.python.org/3/library/asyncio-sync.html">49      50    51 52    53    <style>54      @media only screen {55        table.full-width-table {56            width: 100%;57        }58      }59    </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61    <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62            <script type="text/javascript" src="../_static/copybutton.js"></script>63            <script type="text/javascript" src="../_static/menu.js"></script>64            <script type="text/javascript" src="../_static/search-focus.js"></script>65            <script type="text/javascript" src="../_static/themetoggle.js"></script> 66            <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67            <meta name="readthedocs-addons-api-version" content="1">68 69  </head>70<body>71<div class="mobile-nav">72    <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73           aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74    <nav class="nav-content" role="navigation">75        <label for="menuToggler" class="toggler__label">76            <span></span>77        </label>78        <span class="nav-items-wrapper">79            <a href="https://www.python.org/" class="nav-logo">80                <img src="../_static/py.svg" alt="Python logo">81            </a>82            <span class="version_switcher_placeholder"></span>83            <form role="search" class="search" action="../search.html" method="get">84                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85                    <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86                </svg>87                <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88                <input type="submit" value="Go">89            </form>90        </span>91    </nav>92    <div class="menu-wrapper">93        <nav class="menu" role="navigation" aria-label="main navigation">94            <div class="language_switcher_placeholder"></div>95            96<label class="theme-selector-label">97    Theme98    <select class="theme-selector" oninput="activateTheme(this.value)">99        <option value="auto" selected>Auto</option>100        <option value="light">Light</option>101        <option value="dark">Dark</option>102    </select>103</label>104  <div>105    <h3><a href="../contents.html">Table of Contents</a></h3>106    <ul>107<li><a class="reference internal" href="#">Synchronization Primitives</a><ul>108<li><a class="reference internal" href="#lock">Lock</a></li>109<li><a class="reference internal" href="#event">Event</a></li>110<li><a class="reference internal" href="#condition">Condition</a></li>111<li><a class="reference internal" href="#semaphore">Semaphore</a></li>112<li><a class="reference internal" href="#boundedsemaphore">BoundedSemaphore</a></li>113<li><a class="reference internal" href="#barrier">Barrier</a></li>114</ul>115</li>116</ul>117 118  </div>119  <div>120    <h4>Previous topic</h4>121    <p class="topless"><a href="asyncio-stream.html"122                          title="previous chapter">Streams</a></p>123  </div>124  <div>125    <h4>Next topic</h4>126    <p class="topless"><a href="asyncio-subprocess.html"127                          title="next chapter">Subprocesses</a></p>128  </div>129  <script>130    document.addEventListener('DOMContentLoaded', () => {131        const title = document.querySelector('meta[property="og:title"]').content;132        const elements = document.querySelectorAll('.improvepage');133        const pageurl = window.location.href.split('?')[0];134        elements.forEach(element => {135            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));136            url.searchParams.set('pagetitle', title);137            url.searchParams.set('pageurl', pageurl);138            url.searchParams.set('pagesource', "library/asyncio-sync.rst");139            element.href = url.toString();140        });141    });142  </script>143  <div role="note" aria-label="source link">144    <h3>This page</h3>145    <ul class="this-page-menu">146      <li><a href="../bugs.html">Report a bug</a></li>147      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>148      <li>149        <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio-sync.rst?plain=1"150            rel="nofollow">Show source151        </a>152      </li>153      154    </ul>155  </div>156        </nav>157    </div>158</div>159 160  161    <div class="related" role="navigation" aria-label="Related">162      <h3>Navigation</h3>163      <ul>164        <li class="right" style="margin-right: 10px">165          <a href="../genindex.html" title="General Index"166             accesskey="I">index</a></li>167        <li class="right" >168          <a href="../py-modindex.html" title="Python Module Index"169             >modules</a> |</li>170        <li class="right" >171          <a href="asyncio-subprocess.html" title="Subprocesses"172             accesskey="N">next</a> |</li>173        <li class="right" >174          <a href="asyncio-stream.html" title="Streams"175             accesskey="P">previous</a> |</li>176 177          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>178          <li><a href="https://www.python.org/">Python</a> &#187;</li>179          <li class="switchers">180            <div class="language_switcher_placeholder"></div>181            <div class="version_switcher_placeholder"></div>182          </li>183          <li>184              185          </li>186    <li id="cpython-language-and-version">187      <a href="../index.html">3.15.0a6 Documentation</a> &#187;188    </li>189 190          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>191          <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>192          <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>193        <li class="nav-item nav-item-this"><a href="">Synchronization Primitives</a></li>194                <li class="right">195                    196 197    <div class="inline-search" role="search">198        <form class="inline-search" action="../search.html" method="get">199          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">200          <input type="submit" value="Go">201        </form>202    </div>203                     |204                </li>205            <li class="right">206<label class="theme-selector-label">207    Theme208    <select class="theme-selector" oninput="activateTheme(this.value)">209        <option value="auto" selected>Auto</option>210        <option value="light">Light</option>211        <option value="dark">Dark</option>212    </select>213</label> |</li>214            215      </ul>216    </div>    217 218    <div class="document">219      <div class="documentwrapper">220        <div class="bodywrapper">221          <div class="body" role="main">222            223  <section id="synchronization-primitives">224<span id="asyncio-sync"></span><h1>Synchronization Primitives<a class="headerlink" href="#synchronization-primitives" title="Link to this heading">¶</a></h1>225<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/asyncio/locks.py">Lib/asyncio/locks.py</a></p>226<hr class="docutils" />227<p>asyncio synchronization primitives are designed to be similar to228those 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>229<ul class="simple">230<li><p>asyncio primitives are not thread-safe, therefore they should not231be 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> for232that);</p></li>233<li><p>methods of these synchronization primitives do not accept the <em>timeout</em>234argument; 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 perform235operations with timeouts.</p></li>236</ul>237<p>asyncio has the following basic synchronization primitives:</p>238<ul class="simple">239<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>240<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>241<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>242<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>243<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>244<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>245</ul>246<hr class="docutils" />247<section id="lock">248<h2>Lock<a class="headerlink" href="#lock" title="Link to this heading">¶</a></h2>249<dl class="py class">250<dt class="sig sig-object py" id="asyncio.Lock">251<em class="property"><span class="k"><span class="pre">class</span></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>252<dd><p>Implements a mutex lock for asyncio tasks.  Not thread-safe.</p>253<p>An asyncio lock can be used to guarantee exclusive access to a254shared resource.</p>255<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>256statement:</p>257<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>258 259<span class="c1"># ... later</span>260<span class="k">async</span> <span class="k">with</span> <span class="n">lock</span><span class="p">:</span>261    <span class="c1"># access shared state</span>262</pre></div>263</div>264<p>which is equivalent to:</p>265<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>266 267<span class="c1"># ... later</span>268<span class="k">await</span> <span class="n">lock</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>269<span class="k">try</span><span class="p">:</span>270    <span class="c1"># access shared state</span>271<span class="k">finally</span><span class="p">:</span>272    <span class="n">lock</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>273</pre></div>274</div>275<div class="versionchanged">276<p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p>277</div>278<dl class="py method">279<dt class="sig sig-object py" id="asyncio.Lock.acquire">280<em class="property"><span class="k"><span class="pre">async</span></span><span class="w"> </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>281<dd><p>Acquire the lock.</p>282<p>This method waits until the lock is <em>unlocked</em>, sets it to283<em>locked</em> and returns <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>284<p>When more than one coroutine is blocked in <code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code>285waiting for the lock to be unlocked, only one coroutine286eventually proceeds.</p>287<p>Acquiring a lock is <em>fair</em>: the coroutine that proceeds will be288the first coroutine that started waiting on the lock.</p>289</dd></dl>290 291<dl class="py method">292<dt class="sig sig-object py" id="asyncio.Lock.release">293<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>294<dd><p>Release the lock.</p>295<p>When the lock is <em>locked</em>, reset it to <em>unlocked</em> and return.</p>296<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>297</dd></dl>298 299<dl class="py method">300<dt class="sig sig-object py" id="asyncio.Lock.locked">301<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>302<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the lock is <em>locked</em>.</p>303</dd></dl>304 305</dd></dl>306 307</section>308<section id="event">309<h2>Event<a class="headerlink" href="#event" title="Link to this heading">¶</a></h2>310<dl class="py class">311<dt class="sig sig-object py" id="asyncio.Event">312<em class="property"><span class="k"><span class="pre">class</span></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>313<dd><p>An event object.  Not thread-safe.</p>314<p>An asyncio event can be used to notify multiple asyncio tasks315that some event has happened.</p>316<p>An Event object manages an internal flag that can be set to <em>true</em>317with 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 the318<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 the319flag is set to <em>true</em>.  The flag is set to <em>false</em> initially.</p>320<div class="versionchanged">321<p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p>322</div>323<p id="asyncio-example-sync-event">Example:</p>324<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">waiter</span><span class="p">(</span><span class="n">event</span><span class="p">):</span>325    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;waiting for it ...&#39;</span><span class="p">)</span>326    <span class="k">await</span> <span class="n">event</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>327    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... got it!&#39;</span><span class="p">)</span>328 329<span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">main</span><span class="p">():</span>330    <span class="c1"># Create an Event object.</span>331    <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>332 333    <span class="c1"># Spawn a Task to wait until &#39;event&#39; is set.</span>334    <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>335 336    <span class="c1"># Sleep for 1 second and set the event.</span>337    <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>338    <span class="n">event</span><span class="o">.</span><span class="n">set</span><span class="p">()</span>339 340    <span class="c1"># Wait until the waiter task is finished.</span>341    <span class="k">await</span> <span class="n">waiter_task</span>342 343<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>344</pre></div>345</div>346<dl class="py method">347<dt class="sig sig-object py" id="asyncio.Event.wait">348<em class="property"><span class="k"><span class="pre">async</span></span><span class="w"> </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>349<dd><p>Wait until the event is set.</p>350<p>If the event is set, return <code class="docutils literal notranslate"><span class="pre">True</span></code> immediately.351Otherwise 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>352</dd></dl>353 354<dl class="py method">355<dt class="sig sig-object py" id="asyncio.Event.set">356<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>357<dd><p>Set the event.</p>358<p>All tasks waiting for event to be set will be immediately359awakened.</p>360</dd></dl>361 362<dl class="py method">363<dt class="sig sig-object py" id="asyncio.Event.clear">364<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>365<dd><p>Clear (unset) the event.</p>366<p>Subsequent 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 the367<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>368</dd></dl>369 370<dl class="py method">371<dt class="sig sig-object py" id="asyncio.Event.is_set">372<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>373<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the event is set.</p>374</dd></dl>375 376</dd></dl>377 378</section>379<section id="condition">380<h2>Condition<a class="headerlink" href="#condition" title="Link to this heading">¶</a></h2>381<dl class="py class">382<dt class="sig sig-object py" id="asyncio.Condition">383<em class="property"><span class="k"><span class="pre">class</span></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>384<dd><p>A Condition object.  Not thread-safe.</p>385<p>An asyncio condition primitive can be used by a task to wait for386some event to happen and then get exclusive access to a shared387resource.</p>388<p>In essence, a Condition object combines the functionality389of 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 have390multiple Condition objects share one Lock, which allows coordinating391exclusive access to a shared resource between different tasks392interested in particular states of that shared resource.</p>393<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 or394<code class="docutils literal notranslate"><span class="pre">None</span></code>.  In the latter case a new Lock object is created395automatically.</p>396<div class="versionchanged">397<p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p>398</div>399<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>400statement:</p>401<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>402 403<span class="c1"># ... later</span>404<span class="k">async</span> <span class="k">with</span> <span class="n">cond</span><span class="p">:</span>405    <span class="k">await</span> <span class="n">cond</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>406</pre></div>407</div>408<p>which is equivalent to:</p>409<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>410 411<span class="c1"># ... later</span>412<span class="k">await</span> <span class="n">cond</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>413<span class="k">try</span><span class="p">:</span>414    <span class="k">await</span> <span class="n">cond</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>415<span class="k">finally</span><span class="p">:</span>416    <span class="n">cond</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>417</pre></div>418</div>419<dl class="py method">420<dt class="sig sig-object py" id="asyncio.Condition.acquire">421<em class="property"><span class="k"><span class="pre">async</span></span><span class="w"> </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>422<dd><p>Acquire the underlying lock.</p>423<p>This method waits until the underlying lock is <em>unlocked</em>,424sets it to <em>locked</em> and returns <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>425</dd></dl>426 427<dl class="py method">428<dt class="sig sig-object py" id="asyncio.Condition.notify">429<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>430<dd><p>Wake up <em>n</em> tasks (1 by default) waiting on this431condition.  If fewer than <em>n</em> tasks are waiting they are all awakened.</p>432<p>The lock must be acquired before this method is called and433released shortly after.  If called with an <em>unlocked</em> lock434a <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>435</dd></dl>436 437<dl class="py method">438<dt class="sig sig-object py" id="asyncio.Condition.locked">439<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>440<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the underlying lock is acquired.</p>441</dd></dl>442 443<dl class="py method">444<dt class="sig sig-object py" id="asyncio.Condition.notify_all">445<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>446<dd><p>Wake up all tasks waiting on this condition.</p>447<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 waiting448tasks.</p>449<p>The lock must be acquired before this method is called and450released shortly after.  If called with an <em>unlocked</em> lock451a <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>452</dd></dl>453 454<dl class="py method">455<dt class="sig sig-object py" id="asyncio.Condition.release">456<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>457<dd><p>Release the underlying lock.</p>458<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> is459raised.</p>460</dd></dl>461 462<dl class="py method">463<dt class="sig sig-object py" id="asyncio.Condition.wait">464<em class="property"><span class="k"><span class="pre">async</span></span><span class="w"> </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>465<dd><p>Wait until notified.</p>466<p>If the calling task has not acquired the lock when this method is467called, 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>468<p>This method releases the underlying lock, and then blocks until469it 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.470Once awakened, the Condition re-acquires its lock and this method471returns <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>472<p>Note that a task <em>may</em> return from this call spuriously,473which is why the caller should always re-check the state474and be prepared to <code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code> again. For this reason, you may475prefer to use <a class="reference internal" href="#asyncio.Condition.wait_for" title="asyncio.Condition.wait_for"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait_for()</span></code></a> instead.</p>476</dd></dl>477 478<dl class="py method">479<dt class="sig sig-object py" id="asyncio.Condition.wait_for">480<em class="property"><span class="k"><span class="pre">async</span></span><span class="w"> </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>481<dd><p>Wait until a predicate becomes <em>true</em>.</p>482<p>The predicate must be a callable which result will be483interpreted as a boolean value.  The method will repeatedly484<a class="reference internal" href="#asyncio.Condition.wait" title="asyncio.Condition.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> until the predicate evaluates to <em>true</em>. The final value is the485return value.</p>486</dd></dl>487 488</dd></dl>489 490</section>491<section id="semaphore">492<h2>Semaphore<a class="headerlink" href="#semaphore" title="Link to this heading">¶</a></h2>493<dl class="py class">494<dt class="sig sig-object py" id="asyncio.Semaphore">495<em class="property"><span class="k"><span class="pre">class</span></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>496<dd><p>A Semaphore object.  Not thread-safe.</p>497<p>A semaphore manages an internal counter which is decremented by each498<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.499The counter can never go below zero; when <code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code> finds500that it is zero, it blocks, waiting until some task calls501<code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code>.</p>502<p>The optional <em>value</em> argument gives the initial value for the503internal counter (<code class="docutils literal notranslate"><span class="pre">1</span></code> by default). If the given value is504less 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>505<div class="versionchanged">506<p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p>507</div>508<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>509statement:</p>510<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>511 512<span class="c1"># ... later</span>513<span class="k">async</span> <span class="k">with</span> <span class="n">sem</span><span class="p">:</span>514    <span class="c1"># work with shared resource</span>515</pre></div>516</div>517<p>which is equivalent to:</p>518<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>519 520<span class="c1"># ... later</span>521<span class="k">await</span> <span class="n">sem</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>522<span class="k">try</span><span class="p">:</span>523    <span class="c1"># work with shared resource</span>524<span class="k">finally</span><span class="p">:</span>525    <span class="n">sem</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>526</pre></div>527</div>528<dl class="py method">529<dt class="sig sig-object py" id="asyncio.Semaphore.acquire">530<em class="property"><span class="k"><span class="pre">async</span></span><span class="w"> </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>531<dd><p>Acquire a semaphore.</p>532<p>If the internal counter is greater than zero, decrement533it by one and return <code class="docutils literal notranslate"><span class="pre">True</span></code> immediately.  If it is zero, wait534until 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>535</dd></dl>536 537<dl class="py method">538<dt class="sig sig-object py" id="asyncio.Semaphore.locked">539<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>540<dd><p>Returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if semaphore can not be acquired immediately.</p>541</dd></dl>542 543<dl class="py method">544<dt class="sig sig-object py" id="asyncio.Semaphore.release">545<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>546<dd><p>Release a semaphore, incrementing the internal counter by one.547Can wake up a task waiting to acquire the semaphore.</p>548<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>, <code class="xref py py-class docutils literal notranslate"><span class="pre">Semaphore</span></code> allows549making 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>550</dd></dl>551 552</dd></dl>553 554</section>555<section id="boundedsemaphore">556<h2>BoundedSemaphore<a class="headerlink" href="#boundedsemaphore" title="Link to this heading">¶</a></h2>557<dl class="py class">558<dt class="sig sig-object py" id="asyncio.BoundedSemaphore">559<em class="property"><span class="k"><span class="pre">class</span></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>560<dd><p>A bounded semaphore object.  Not thread-safe.</p>561<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 raises562a <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 it563increases the internal counter above the initial <em>value</em>.</p>564<div class="versionchanged">565<p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p>566</div>567</dd></dl>568 569</section>570<section id="barrier">571<h2>Barrier<a class="headerlink" href="#barrier" title="Link to this heading">¶</a></h2>572<dl class="py class">573<dt class="sig sig-object py" id="asyncio.Barrier">574<em class="property"><span class="k"><span class="pre">class</span></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>575<dd><p>A barrier object.  Not thread-safe.</p>576<p>A barrier is a simple synchronization primitive that allows to block until577<em>parties</em> number of tasks are waiting on it.578Tasks 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 until579the specified number of tasks end up waiting on <code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code>.580At that point all of the waiting tasks would unblock simultaneously.</p>581<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 on582<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>583<p>The barrier can be reused any number of times.</p>584<p id="asyncio-example-barrier">Example:</p>585<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">example_barrier</span><span class="p">():</span>586   <span class="c1"># barrier with 3 parties</span>587   <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>588 589   <span class="c1"># create 2 new waiting tasks</span>590   <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>591   <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>592 593   <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>594   <span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>595 596   <span class="c1"># The third .wait() call passes the barrier</span>597   <span class="k">await</span> <span class="n">b</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>598   <span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>599   <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;barrier passed&quot;</span><span class="p">)</span>600 601   <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>602   <span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>603 604<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>605</pre></div>606</div>607<p>Result of this example is:</p>608<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>609<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>610<span class="n">barrier</span> <span class="n">passed</span>611<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>612</pre></div>613</div>614<div class="versionadded">615<p><span class="versionmodified added">Added in version 3.11.</span></p>616</div>617<dl class="py method">618<dt class="sig sig-object py" id="asyncio.Barrier.wait">619<em class="property"><span class="k"><span class="pre">async</span></span><span class="w"> </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>620<dd><p>Pass the barrier. When all the tasks party to the barrier have called621this function, they are all unblocked simultaneously.</p>622<p>When a waiting or blocked task in the barrier is cancelled,623this task exits the barrier which stays in the same state.624If the state of the barrier is “filling”, the number of waiting task625decreases by 1.</p>626<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>, different627for each task. This can be used to select a task to do some special628housekeeping, e.g.:</p>629<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">...</span>630<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>631   <span class="k">if</span> <span class="n">position</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>632      <span class="c1"># Only one task prints this</span>633      <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;End of *draining phase*&#39;</span><span class="p">)</span>634</pre></div>635</div>636<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 the637barrier is broken or reset while a task is waiting.638It 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>639</dd></dl>640 641<dl class="py method">642<dt class="sig sig-object py" id="asyncio.Barrier.reset">643<em class="property"><span class="k"><span class="pre">async</span></span><span class="w"> </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>644<dd><p>Return the barrier to the default, empty state.  Any tasks waiting on it645will 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>646<p>If a barrier is broken it may be better to just leave it and create a new one.</p>647</dd></dl>648 649<dl class="py method">650<dt class="sig sig-object py" id="asyncio.Barrier.abort">651<em class="property"><span class="k"><span class="pre">async</span></span><span class="w"> </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>652<dd><p>Put the barrier into a broken state.  This causes any active or future653calls to <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> 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>.654Use this for example if one of the tasks needs to abort, to avoid infinite655waiting tasks.</p>656</dd></dl>657 658<dl class="py attribute">659<dt class="sig sig-object py" id="asyncio.Barrier.parties">660<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>661<dd><p>The number of tasks required to pass the barrier.</p>662</dd></dl>663 664<dl class="py attribute">665<dt class="sig sig-object py" id="asyncio.Barrier.n_waiting">666<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>667<dd><p>The number of tasks currently waiting in the barrier while filling.</p>668</dd></dl>669 670<dl class="py attribute">671<dt class="sig sig-object py" id="asyncio.Barrier.broken">672<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>673<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>674</dd></dl>675 676</dd></dl>677 678<dl class="py exception">679<dt class="sig sig-object py" id="asyncio.BrokenBarrierError">680<em class="property"><span class="k"><span class="pre">exception</span></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>681<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 the682<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>683</dd></dl>684 685<hr class="docutils" />686<div class="versionchanged">687<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/or688<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>689<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>690</div>691</section>692</section>693 694 695            <div class="clearer"></div>696          </div>697        </div>698      </div>699      <div class="sphinxsidebar" role="navigation" aria-label="Main">700        <div class="sphinxsidebarwrapper">701  <div>702    <h3><a href="../contents.html">Table of Contents</a></h3>703    <ul>704<li><a class="reference internal" href="#">Synchronization Primitives</a><ul>705<li><a class="reference internal" href="#lock">Lock</a></li>706<li><a class="reference internal" href="#event">Event</a></li>707<li><a class="reference internal" href="#condition">Condition</a></li>708<li><a class="reference internal" href="#semaphore">Semaphore</a></li>709<li><a class="reference internal" href="#boundedsemaphore">BoundedSemaphore</a></li>710<li><a class="reference internal" href="#barrier">Barrier</a></li>711</ul>712</li>713</ul>714 715  </div>716  <div>717    <h4>Previous topic</h4>718    <p class="topless"><a href="asyncio-stream.html"719                          title="previous chapter">Streams</a></p>720  </div>721  <div>722    <h4>Next topic</h4>723    <p class="topless"><a href="asyncio-subprocess.html"724                          title="next chapter">Subprocesses</a></p>725  </div>726  <script>727    document.addEventListener('DOMContentLoaded', () => {728        const title = document.querySelector('meta[property="og:title"]').content;729        const elements = document.querySelectorAll('.improvepage');730        const pageurl = window.location.href.split('?')[0];731        elements.forEach(element => {732            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));733            url.searchParams.set('pagetitle', title);734            url.searchParams.set('pageurl', pageurl);735            url.searchParams.set('pagesource', "library/asyncio-sync.rst");736            element.href = url.toString();737        });738    });739  </script>740  <div role="note" aria-label="source link">741    <h3>This page</h3>742    <ul class="this-page-menu">743      <li><a href="../bugs.html">Report a bug</a></li>744      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>745      <li>746        <a href="https://github.com/python/cpython/blob/main/Doc/library/asyncio-sync.rst?plain=1"747            rel="nofollow">Show source748        </a>749      </li>750      751    </ul>752  </div>753        </div>754<div id="sidebarbutton" title="Collapse sidebar">755<span>«</span>756</div>757 758      </div>759      <div class="clearer"></div>760    </div>  761    <div class="related" role="navigation" aria-label="Related">762      <h3>Navigation</h3>763      <ul>764        <li class="right" style="margin-right: 10px">765          <a href="../genindex.html" title="General Index"766             >index</a></li>767        <li class="right" >768          <a href="../py-modindex.html" title="Python Module Index"769             >modules</a> |</li>770        <li class="right" >771          <a href="asyncio-subprocess.html" title="Subprocesses"772             >next</a> |</li>773        <li class="right" >774          <a href="asyncio-stream.html" title="Streams"775             >previous</a> |</li>776 777          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>778          <li><a href="https://www.python.org/">Python</a> &#187;</li>779          <li class="switchers">780            <div class="language_switcher_placeholder"></div>781            <div class="version_switcher_placeholder"></div>782          </li>783          <li>784              785          </li>786    <li id="cpython-language-and-version">787      <a href="../index.html">3.15.0a6 Documentation</a> &#187;788    </li>789 790          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>791          <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>792          <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>793        <li class="nav-item nav-item-this"><a href="">Synchronization Primitives</a></li>794                <li class="right">795                    796 797    <div class="inline-search" role="search">798        <form class="inline-search" action="../search.html" method="get">799          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">800          <input type="submit" value="Go">801        </form>802    </div>803                     |804                </li>805            <li class="right">806<label class="theme-selector-label">807    Theme808    <select class="theme-selector" oninput="activateTheme(this.value)">809        <option value="auto" selected>Auto</option>810        <option value="light">Light</option>811        <option value="dark">Dark</option>812    </select>813</label> |</li>814            815      </ul>816    </div>  817    <div class="footer">818    &copy; <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.819    <br>820    This page is licensed under the Python Software Foundation License Version 2.821    <br>822    Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.823    <br>824    825      See <a href="/license.html">History and License</a> for more information.<br>826    827    828    <br>829 830    The Python Software Foundation is a non-profit corporation.831<a href="https://www.python.org/psf/donations/">Please donate.</a>832<br>833    <br>834      Last updated on Mar 10, 2026 (08:58 UTC).835    836      <a href="/bugs.html">Found a bug</a>?837    838    <br>839 840    Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.841    </div>842 843  </body>844</html>