Files
hustoj/web/python-docs/library/asyncio-sync.html
2025-03-09 17:10:56 +08:00

571 lines
41 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>同步原语 &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="子进程集" href="asyncio-subprocess.html" />
<link rel="prev" title="流" href="asyncio-stream.html" />
<link rel="canonical" href="https://docs.python.org/3/library/asyncio-sync.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<nav class="nav-content" role="navigation">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<div class="version_switcher_placeholder"></div>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero"
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" fill="#444"></path>
</svg>
<input type="text" name="q" aria-label="快速搜索"/>
<input type="submit" value="转向"/>
</form>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">同步原语</a><ul>
<li><a class="reference internal" href="#lock">Lock</a></li>
<li><a class="reference internal" href="#event">Event</a></li>
<li><a class="reference internal" href="#condition">Condition</a></li>
<li><a class="reference internal" href="#semaphore">Semaphore</a></li>
<li><a class="reference internal" href="#boundedsemaphore">BoundedSemaphore</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="asyncio-stream.html"
title="上一章"></a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="asyncio-subprocess.html"
title="下一章">子进程集</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/asyncio-sync.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
这个文档所针对的是一个已不再受支持的 Python 旧版本。
你应当升级版本,并阅读
<a href="/3/library/asyncio-sync.html"> Python 当前稳定版本的文档</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="asyncio-subprocess.html" title="子进程集"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="asyncio-stream.html" title="流"
accesskey="P">上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="ipc.html" >网络和进程间通信</a> &#187;</li>
<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> --- 异步 I/O</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="synchronization-primitives">
<span id="asyncio-sync"></span><h1>同步原语<a class="headerlink" href="#synchronization-primitives" title="永久链接至标题"></a></h1>
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/asyncio/locks.py">Lib/asyncio/locks.py</a></p>
<hr class="docutils" />
<p>asyncio 同步原语被设计为与 <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> 模块的类似,但有两个关键注意事项:</p>
<ul class="simple">
<li><p>asyncio 原语不是线程安全的,因此它们不应被用于 OS 线程同步 (而应当使用 <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>)</p></li>
<li><p>这些同步原语的方法不接受 <em>timeout</em> 参数;请使用 <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> 函数来执行带有超时的操作。</p></li>
</ul>
<p>asyncio 具有下列基本同步原语:</p>
<ul class="simple">
<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>
<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>
<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>
<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>
<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>
</ul>
<hr class="docutils" />
<section id="lock">
<h2>Lock<a class="headerlink" href="#lock" title="永久链接至标题"></a></h2>
<dl class="class">
<dt id="asyncio.Lock">
<em class="property">class </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">Lock</code><span class="sig-paren">(</span><em class="sig-param">*</em>, <em class="sig-param">loop=None</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Lock" title="永久链接至目标"></a></dt>
<dd><p>实现一个用于 asyncio 任务的互斥锁。 非线程安全。</p>
<p>asyncio 锁可被用来保证对共享资源的独占访问。</p>
<p>使用 Lock 的推荐方式是通过 <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> 语句:</p>
<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>
<span class="c1"># ... later</span>
<span class="k">async</span> <span class="k">with</span> <span class="n">lock</span><span class="p">:</span>
<span class="c1"># access shared state</span>
</pre></div>
</div>
<p>这等价于:</p>
<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>
<span class="c1"># ... later</span>
<span class="k">await</span> <span class="n">lock</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="c1"># access shared state</span>
<span class="k">finally</span><span class="p">:</span>
<span class="n">lock</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
</pre></div>
</div>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.8, will be removed in version 3.10: </span><em>loop</em> 形参。</p>
</div>
<dl class="method">
<dt id="asyncio.Lock.acquire">
<em class="property">coroutine </em><code class="sig-name descname">acquire</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Lock.acquire" title="永久链接至目标"></a></dt>
<dd><p>获取锁。</p>
<p>此方法会等待直至锁为 <em>unlocked</em>,将其设为 <em>locked</em> 并返回 <code class="docutils literal notranslate"><span class="pre">True</span></code></p>
<p>当有一个以上的协程在 <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> 中被阻塞则会等待解锁,最终只有一个协程会被执行。</p>
<p>锁的获取是 <em>公平的</em>: 被执行的协程将是第一个开始等待锁的协程。</p>
</dd></dl>
<dl class="method">
<dt id="asyncio.Lock.release">
<code class="sig-name descname">release</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Lock.release" title="永久链接至目标"></a></dt>
<dd><p>释放锁。</p>
<p>当锁为 <em>locked</em> 时,将其设为 <em>unlocked</em> 并返回。</p>
<p>如果锁为 <em>unlocked</em>,则会引发 <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a></p>
</dd></dl>
<dl class="method">
<dt id="asyncio.Lock.locked">
<code class="sig-name descname">locked</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Lock.locked" title="永久链接至目标"></a></dt>
<dd><p>如果锁为 <em>locked</em> 则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code></p>
</dd></dl>
</dd></dl>
</section>
<section id="event">
<h2>Event<a class="headerlink" href="#event" title="永久链接至标题"></a></h2>
<dl class="class">
<dt id="asyncio.Event">
<em class="property">class </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">Event</code><span class="sig-paren">(</span><em class="sig-param">*</em>, <em class="sig-param">loop=None</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Event" title="永久链接至目标"></a></dt>
<dd><p>事件对象。 该对象不是线程安全的。</p>
<p>asyncio 事件可被用来通知多个 asyncio 任务已经有事件发生。</p>
<p>Event 对象会管理一个内部旗标,可通过 <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> 方法将其设为 <em>true</em> 并通过 <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> 方法将其重设为 <em>false</em><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> 方法会阻塞直至该旗标被设为 <em>true</em>。 该旗标初始时会被设为 <em>false</em></p>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.8, will be removed in version 3.10: </span><em>loop</em> 形参。</p>
</div>
<p id="asyncio-example-sync-event">示例:</p>
<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>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;waiting for it ...&#39;</span><span class="p">)</span>
<span class="k">await</span> <span class="n">event</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;... got it!&#39;</span><span class="p">)</span>
<span class="k">async</span> <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
<span class="c1"># Create an Event object.</span>
<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>
<span class="c1"># Spawn a Task to wait until &#39;event&#39; is set.</span>
<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>
<span class="c1"># Sleep for 1 second and set the event.</span>
<span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">event</span><span class="o">.</span><span class="n">set</span><span class="p">()</span>
<span class="c1"># Wait until the waiter task is finished.</span>
<span class="k">await</span> <span class="n">waiter_task</span>
<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>
</pre></div>
</div>
<dl class="method">
<dt id="asyncio.Event.wait">
<em class="property">coroutine </em><code class="sig-name descname">wait</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Event.wait" title="永久链接至目标"></a></dt>
<dd><p>等待直至事件被设置。</p>
<p>如果事件已被设置,则立即返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。 否则将阻塞直至另一个任务调用 <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>
</dd></dl>
<dl class="method">
<dt id="asyncio.Event.set">
<code class="sig-name descname">set</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Event.set" title="永久链接至目标"></a></dt>
<dd><p>设置事件。</p>
<p>所有等待事件被设置的任务将被立即唤醒。</p>
</dd></dl>
<dl class="method">
<dt id="asyncio.Event.clear">
<code class="sig-name descname">clear</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Event.clear" title="永久链接至目标"></a></dt>
<dd><p>清空(取消设置)事件.</p>
<p>通过 <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> 进行等待的任务现在将会阻塞直至 <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>
</dd></dl>
<dl class="method">
<dt id="asyncio.Event.is_set">
<code class="sig-name descname">is_set</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Event.is_set" title="永久链接至目标"></a></dt>
<dd><p>如果事件已被设置则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code></p>
</dd></dl>
</dd></dl>
</section>
<section id="condition">
<h2>Condition<a class="headerlink" href="#condition" title="永久链接至标题"></a></h2>
<dl class="class">
<dt id="asyncio.Condition">
<em class="property">class </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">Condition</code><span class="sig-paren">(</span><em class="sig-param">lock=None</em>, <em class="sig-param">*</em>, <em class="sig-param">loop=None</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Condition" title="永久链接至目标"></a></dt>
<dd><p>条件对象。 该对象不是线程安全的。</p>
<p>asyncio 条件原语可被任务用于等待某个事件发生,然后获取对共享资源的独占访问。</p>
<p>在本质上Condition 对象合并了 <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><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> 的功能。 多个 Condition 对象有可能共享一个 Lock这允许关注于共享资源的特定状态的不同任务实现对共享资源的协同独占访问。</p>
<p>可选的 <em>lock</em> 参数必须为 <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> 对象或 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 在后一种情况下会自动创建一个新的 Lock 对象。</p>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.8, will be removed in version 3.10: </span><em>loop</em> 形参。</p>
</div>
<p>使用 Condition 的推荐方式是通过 <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> 语句:</p>
<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>
<span class="c1"># ... later</span>
<span class="k">async</span> <span class="k">with</span> <span class="n">cond</span><span class="p">:</span>
<span class="k">await</span> <span class="n">cond</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
</pre></div>
</div>
<p>这等价于:</p>
<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>
<span class="c1"># ... later</span>
<span class="k">await</span> <span class="n">cond</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">await</span> <span class="n">cond</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
<span class="k">finally</span><span class="p">:</span>
<span class="n">cond</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
</pre></div>
</div>
<dl class="method">
<dt id="asyncio.Condition.acquire">
<em class="property">coroutine </em><code class="sig-name descname">acquire</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Condition.acquire" title="永久链接至目标"></a></dt>
<dd><p>获取下层的锁。</p>
<p>此方法会等待直至下层的锁为 <em>unlocked</em>,将其设为 <em>locked</em> 并返回 returns <code class="docutils literal notranslate"><span class="pre">True</span></code></p>
</dd></dl>
<dl class="method">
<dt id="asyncio.Condition.notify">
<code class="sig-name descname">notify</code><span class="sig-paren">(</span><em class="sig-param">n=1</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Condition.notify" title="永久链接至目标"></a></dt>
<dd><p>唤醒最多 <em>n</em> 个正在等待此条件的任务(默认为 1 个)。 如果没有任务正在等待则此方法为空操作。</p>
<p>锁必须在此方法被调用前被获取并在随后被快速释放。 如果通过一个 <em>unlocked</em> 锁调用则会引发 <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a></p>
</dd></dl>
<dl class="method">
<dt id="asyncio.Condition.locked">
<code class="sig-name descname">locked</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Condition.locked" title="永久链接至目标"></a></dt>
<dd><p>如果下层的锁已被获取则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code></p>
</dd></dl>
<dl class="method">
<dt id="asyncio.Condition.notify_all">
<code class="sig-name descname">notify_all</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Condition.notify_all" title="永久链接至目标"></a></dt>
<dd><p>唤醒所有正在等待此条件的任务。</p>
<p>此方法的行为类似于 <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>,但会唤醒所有正在等待的任务。</p>
<p>锁必须在此方法被调用前被获取并在随后被快速释放。 如果通过一个 <em>unlocked</em> 锁调用则会引发 <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a></p>
</dd></dl>
<dl class="method">
<dt id="asyncio.Condition.release">
<code class="sig-name descname">release</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Condition.release" title="永久链接至目标"></a></dt>
<dd><p>释放下层的锁。</p>
<p>在未锁定的锁调用时,会引发 <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> 异常。</p>
</dd></dl>
<dl class="method">
<dt id="asyncio.Condition.wait">
<em class="property">coroutine </em><code class="sig-name descname">wait</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Condition.wait" title="永久链接至目标"></a></dt>
<dd><p>等待直至收到通知。</p>
<p>当此方法被调用时如果调用方任务未获得锁,则会引发 <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a></p>
<p>这个方法会释放下层的锁,然后保持阻塞直到被 <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><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> 调用所唤醒。 一旦被唤醒Condition 会重新获取它的锁并且此方法将返回 <code class="docutils literal notranslate"><span class="pre">True</span></code></p>
</dd></dl>
<dl class="method">
<dt id="asyncio.Condition.wait_for">
<em class="property">coroutine </em><code class="sig-name descname">wait_for</code><span class="sig-paren">(</span><em class="sig-param">predicate</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Condition.wait_for" title="永久链接至目标"></a></dt>
<dd><p>等待直到目标值变为 <em>true</em></p>
<p>目标必须为一个可调用对象,其结果将被解读为一个布尔值。 最终的值将为返回值。</p>
</dd></dl>
</dd></dl>
</section>
<section id="semaphore">
<h2>Semaphore<a class="headerlink" href="#semaphore" title="永久链接至标题"></a></h2>
<dl class="class">
<dt id="asyncio.Semaphore">
<em class="property">class </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">Semaphore</code><span class="sig-paren">(</span><em class="sig-param">value=1</em>, <em class="sig-param">*</em>, <em class="sig-param">loop=None</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Semaphore" title="永久链接至目标"></a></dt>
<dd><p>信号量对象。 该对象不是线程安全的。</p>
<p>信号量会管理一个内部计数器,该计数器会随每次 <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> 调用递减并随每次 <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> 调用递增。 计数器的值永远不会降到零以下;当 <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> 发现其值为零时,它将保持阻塞直到有某个任务调用了 <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>
<p>可选的 <em>value</em> 参数用来为内部计数器赋初始值 (默认值为 <code class="docutils literal notranslate"><span class="pre">1</span></code>)。 如果给定的值小于 <code class="docutils literal notranslate"><span class="pre">0</span></code> 则会引发 <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></p>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.8, will be removed in version 3.10: </span><em>loop</em> 形参。</p>
</div>
<p>使用 Semaphore 的推荐方式是通过 <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> 语句。:</p>
<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>
<span class="c1"># ... later</span>
<span class="k">async</span> <span class="k">with</span> <span class="n">sem</span><span class="p">:</span>
<span class="c1"># work with shared resource</span>
</pre></div>
</div>
<p>这等价于:</p>
<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>
<span class="c1"># ... later</span>
<span class="k">await</span> <span class="n">sem</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="c1"># work with shared resource</span>
<span class="k">finally</span><span class="p">:</span>
<span class="n">sem</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
</pre></div>
</div>
<dl class="method">
<dt id="asyncio.Semaphore.acquire">
<em class="property">coroutine </em><code class="sig-name descname">acquire</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Semaphore.acquire" title="永久链接至目标"></a></dt>
<dd><p>获取一个信号量。</p>
<p>如果内部计数器的值大于零,则将其减一并立即返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。 如果其值为零,则会等待直到 <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> 并调用并返回 <code class="docutils literal notranslate"><span class="pre">True</span></code></p>
</dd></dl>
<dl class="method">
<dt id="asyncio.Semaphore.locked">
<code class="sig-name descname">locked</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Semaphore.locked" title="永久链接至目标"></a></dt>
<dd><p>如果信号量对象无法被立即获取则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code></p>
</dd></dl>
<dl class="method">
<dt id="asyncio.Semaphore.release">
<code class="sig-name descname">release</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Semaphore.release" title="永久链接至目标"></a></dt>
<dd><p>释放一个信号量对象,将内部计数器的值加一。 可以唤醒一个正在等待获取信号量对象的任务。</p>
<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><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> 允许执行的 <code class="docutils literal notranslate"><span class="pre">release()</span></code> 调用多于 <code class="docutils literal notranslate"><span class="pre">acquire()</span></code> 调用。</p>
</dd></dl>
</dd></dl>
</section>
<section id="boundedsemaphore">
<h2>BoundedSemaphore<a class="headerlink" href="#boundedsemaphore" title="永久链接至标题"></a></h2>
<dl class="class">
<dt id="asyncio.BoundedSemaphore">
<em class="property">class </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">BoundedSemaphore</code><span class="sig-paren">(</span><em class="sig-param">value=1</em>, <em class="sig-param">*</em>, <em class="sig-param">loop=None</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BoundedSemaphore" title="永久链接至目标"></a></dt>
<dd><p>绑定的信号量对象。 该对象不是线程安全的。</p>
<p>BoundedSemaphore 是特殊版本的 <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>,如果在 <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> 中内部计数器值增加到初始 <em>value</em> 以上它将引发一个 <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></p>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.8, will be removed in version 3.10: </span><em>loop</em> 形参。</p>
</div>
</dd></dl>
<hr class="docutils" />
<div class="deprecated">
<p><span class="versionmodified deprecated">3.7 版后已移除: </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
<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>
<span class="pre">lock)</span></code>) is deprecated. Use <code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">with</span> <span class="pre">lock</span></code> instead.</p>
</div>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">同步原语</a><ul>
<li><a class="reference internal" href="#lock">Lock</a></li>
<li><a class="reference internal" href="#event">Event</a></li>
<li><a class="reference internal" href="#condition">Condition</a></li>
<li><a class="reference internal" href="#semaphore">Semaphore</a></li>
<li><a class="reference internal" href="#boundedsemaphore">BoundedSemaphore</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="asyncio-stream.html"
title="上一章"></a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="asyncio-subprocess.html"
title="下一章">子进程集</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/asyncio-sync.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="asyncio-subprocess.html" title="子进程集"
>下一页</a> |</li>
<li class="right" >
<a href="asyncio-stream.html" title="流"
>上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="ipc.html" >网络和进程间通信</a> &#187;</li>
<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> --- 异步 I/O</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
最后更新于 12月 09, 2024.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</div>
</body>
</html>