summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2Fasyncio-api-index.html
blob: b916c2b82f85a102f23b3d3342f21e0a15aee0fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 <h1>High-level API Index</h1> <p>This page lists all high-level async/await enabled asyncio APIs.</p> <section id="tasks"> <h2>Tasks</h2> <p>Utilities to run asyncio programs, create Tasks, and await on multiple things with timeouts.</p> <table class="colwidths-given full-width-table docutils align-default">   <tr>
<td><p><a class="reference internal" href="asyncio-runner#asyncio.run" title="asyncio.run"><code>run()</code></a></p></td> <td><p>Create event loop, run a coroutine, close the loop.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-runner#asyncio.Runner" title="asyncio.Runner"><code>Runner</code></a></p></td> <td><p>A context manager that simplifies multiple async function calls.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-task#asyncio.Task" title="asyncio.Task"><code>Task</code></a></p></td> <td><p>Task object.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-task#asyncio.TaskGroup" title="asyncio.TaskGroup"><code>TaskGroup</code></a></p></td> <td><p>A context manager that holds a group of tasks. Provides a convenient and reliable way to wait for all tasks in the group to finish.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-task#asyncio.create_task" title="asyncio.create_task"><code>create_task()</code></a></p></td> <td><p>Start an asyncio Task, then returns it.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-task#asyncio.current_task" title="asyncio.current_task"><code>current_task()</code></a></p></td> <td><p>Return the current Task.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-task#asyncio.all_tasks" title="asyncio.all_tasks"><code>all_tasks()</code></a></p></td> <td><p>Return all tasks that are not yet finished for an event loop.</p></td> </tr> <tr>
<td><p><code>await</code> <a class="reference internal" href="asyncio-task#asyncio.sleep" title="asyncio.sleep"><code>sleep()</code></a></p></td> <td><p>Sleep for a number of seconds.</p></td> </tr> <tr>
<td><p><code>await</code> <a class="reference internal" href="asyncio-task#asyncio.gather" title="asyncio.gather"><code>gather()</code></a></p></td> <td><p>Schedule and wait for things concurrently.</p></td> </tr> <tr>
<td><p><code>await</code> <a class="reference internal" href="asyncio-task#asyncio.wait_for" title="asyncio.wait_for"><code>wait_for()</code></a></p></td> <td><p>Run with a timeout.</p></td> </tr> <tr>
<td><p><code>await</code> <a class="reference internal" href="asyncio-task#asyncio.shield" title="asyncio.shield"><code>shield()</code></a></p></td> <td><p>Shield from cancellation.</p></td> </tr> <tr>
<td><p><code>await</code> <a class="reference internal" href="asyncio-task#asyncio.wait" title="asyncio.wait"><code>wait()</code></a></p></td> <td><p>Monitor for completion.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-task#asyncio.timeout" title="asyncio.timeout"><code>timeout()</code></a></p></td> <td><p>Run with a timeout. Useful in cases when <code>wait_for</code> is not suitable.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-task#asyncio.to_thread" title="asyncio.to_thread"><code>to_thread()</code></a></p></td> <td><p>Asynchronously run a function in a separate OS thread.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-task#asyncio.run_coroutine_threadsafe" title="asyncio.run_coroutine_threadsafe"><code>run_coroutine_threadsafe()</code></a></p></td> <td><p>Schedule a coroutine from another OS thread.</p></td> </tr> <tr>
<td><p><code>for in</code> <a class="reference internal" href="asyncio-task#asyncio.as_completed" title="asyncio.as_completed"><code>as_completed()</code></a></p></td> <td><p>Monitor for completion with a <code>for</code> loop.</p></td> </tr>  </table> <h4 class="rubric">Examples</h4> <ul class="simple"> <li>
<a class="reference internal" href="asyncio-task#asyncio-example-gather"><span class="std std-ref">Using asyncio.gather() to run things in parallel</span></a>.</li> <li>
<a class="reference internal" href="asyncio-task#asyncio-example-waitfor"><span class="std std-ref">Using asyncio.wait_for() to enforce a timeout</span></a>.</li> <li>
<a class="reference internal" href="asyncio-task#asyncio-example-task-cancel"><span class="std std-ref">Cancellation</span></a>.</li> <li>
<a class="reference internal" href="asyncio-task#asyncio-example-sleep"><span class="std std-ref">Using asyncio.sleep()</span></a>.</li> <li>See also the main <a class="reference internal" href="asyncio-task#coroutine"><span class="std std-ref">Tasks documentation page</span></a>.</li> </ul> </section> <section id="queues"> <h2>Queues</h2> <p>Queues should be used to distribute work amongst multiple asyncio Tasks, implement connection pools, and pub/sub patterns.</p> <table class="colwidths-given full-width-table docutils align-default">   <tr>
<td><p><a class="reference internal" href="asyncio-queue#asyncio.Queue" title="asyncio.Queue"><code>Queue</code></a></p></td> <td><p>A FIFO queue.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-queue#asyncio.PriorityQueue" title="asyncio.PriorityQueue"><code>PriorityQueue</code></a></p></td> <td><p>A priority queue.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-queue#asyncio.LifoQueue" title="asyncio.LifoQueue"><code>LifoQueue</code></a></p></td> <td><p>A LIFO queue.</p></td> </tr>  </table> <h4 class="rubric">Examples</h4> <ul class="simple"> <li>
<a class="reference internal" href="asyncio-queue#asyncio-example-queue-dist"><span class="std std-ref">Using asyncio.Queue to distribute workload between several Tasks</span></a>.</li> <li>See also the <a class="reference internal" href="asyncio-queue#asyncio-queues"><span class="std std-ref">Queues documentation page</span></a>.</li> </ul> </section> <section id="subprocesses"> <h2>Subprocesses</h2> <p>Utilities to spawn subprocesses and run shell commands.</p> <table class="colwidths-given full-width-table docutils align-default">   <tr>
<td><p><code>await</code> <a class="reference internal" href="asyncio-subprocess#asyncio.create_subprocess_exec" title="asyncio.create_subprocess_exec"><code>create_subprocess_exec()</code></a></p></td> <td><p>Create a subprocess.</p></td> </tr> <tr>
<td><p><code>await</code> <a class="reference internal" href="asyncio-subprocess#asyncio.create_subprocess_shell" title="asyncio.create_subprocess_shell"><code>create_subprocess_shell()</code></a></p></td> <td><p>Run a shell command.</p></td> </tr>  </table> <h4 class="rubric">Examples</h4> <ul class="simple"> <li>
<a class="reference internal" href="asyncio-subprocess#asyncio-example-subprocess-shell"><span class="std std-ref">Executing a shell command</span></a>.</li> <li>See also the <a class="reference internal" href="asyncio-subprocess#asyncio-subprocess"><span class="std std-ref">subprocess APIs</span></a> documentation.</li> </ul> </section> <section id="streams"> <h2>Streams</h2> <p>High-level APIs to work with network IO.</p> <table class="colwidths-given full-width-table docutils align-default">   <tr>
<td><p><code>await</code> <a class="reference internal" href="asyncio-stream#asyncio.open_connection" title="asyncio.open_connection"><code>open_connection()</code></a></p></td> <td><p>Establish a TCP connection.</p></td> </tr> <tr>
<td><p><code>await</code> <a class="reference internal" href="asyncio-stream#asyncio.open_unix_connection" title="asyncio.open_unix_connection"><code>open_unix_connection()</code></a></p></td> <td><p>Establish a Unix socket connection.</p></td> </tr> <tr>
<td><p><code>await</code> <a class="reference internal" href="asyncio-stream#asyncio.start_server" title="asyncio.start_server"><code>start_server()</code></a></p></td> <td><p>Start a TCP server.</p></td> </tr> <tr>
<td><p><code>await</code> <a class="reference internal" href="asyncio-stream#asyncio.start_unix_server" title="asyncio.start_unix_server"><code>start_unix_server()</code></a></p></td> <td><p>Start a Unix socket server.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-stream#asyncio.StreamReader" title="asyncio.StreamReader"><code>StreamReader</code></a></p></td> <td><p>High-level async/await object to receive network data.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-stream#asyncio.StreamWriter" title="asyncio.StreamWriter"><code>StreamWriter</code></a></p></td> <td><p>High-level async/await object to send network data.</p></td> </tr>  </table> <h4 class="rubric">Examples</h4> <ul class="simple"> <li>
<a class="reference internal" href="asyncio-stream#asyncio-example-stream"><span class="std std-ref">Example TCP client</span></a>.</li> <li>See also the <a class="reference internal" href="asyncio-stream#asyncio-streams"><span class="std std-ref">streams APIs</span></a> documentation.</li> </ul> </section> <section id="synchronization"> <h2>Synchronization</h2> <p>Threading-like synchronization primitives that can be used in Tasks.</p> <table class="colwidths-given full-width-table docutils align-default">   <tr>
<td><p><a class="reference internal" href="asyncio-sync#asyncio.Lock" title="asyncio.Lock"><code>Lock</code></a></p></td> <td><p>A mutex lock.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-sync#asyncio.Event" title="asyncio.Event"><code>Event</code></a></p></td> <td><p>An event object.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-sync#asyncio.Condition" title="asyncio.Condition"><code>Condition</code></a></p></td> <td><p>A condition object.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-sync#asyncio.Semaphore" title="asyncio.Semaphore"><code>Semaphore</code></a></p></td> <td><p>A semaphore.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-sync#asyncio.BoundedSemaphore" title="asyncio.BoundedSemaphore"><code>BoundedSemaphore</code></a></p></td> <td><p>A bounded semaphore.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-sync#asyncio.Barrier" title="asyncio.Barrier"><code>Barrier</code></a></p></td> <td><p>A barrier object.</p></td> </tr>  </table> <h4 class="rubric">Examples</h4> <ul class="simple"> <li>
<a class="reference internal" href="asyncio-sync#asyncio-example-sync-event"><span class="std std-ref">Using asyncio.Event</span></a>.</li> <li>
<a class="reference internal" href="asyncio-sync#asyncio-example-barrier"><span class="std std-ref">Using asyncio.Barrier</span></a>.</li> <li>See also the documentation of asyncio <a class="reference internal" href="asyncio-sync#asyncio-sync"><span class="std std-ref">synchronization primitives</span></a>.</li> </ul> </section> <section id="exceptions"> <h2>Exceptions</h2> <table class="colwidths-given full-width-table docutils align-default">   <tr>
<td><p><a class="reference internal" href="asyncio-exceptions#asyncio.CancelledError" title="asyncio.CancelledError"><code>asyncio.CancelledError</code></a></p></td> <td><p>Raised when a Task is cancelled. See also <a class="reference internal" href="asyncio-task#asyncio.Task.cancel" title="asyncio.Task.cancel"><code>Task.cancel()</code></a>.</p></td> </tr> <tr>
<td><p><a class="reference internal" href="asyncio-sync#asyncio.BrokenBarrierError" title="asyncio.BrokenBarrierError"><code>asyncio.BrokenBarrierError</code></a></p></td> <td><p>Raised when a Barrier is broken. See also <a class="reference internal" href="asyncio-sync#asyncio.Barrier.wait" title="asyncio.Barrier.wait"><code>Barrier.wait()</code></a>.</p></td> </tr>  </table> <h4 class="rubric">Examples</h4> <ul class="simple"> <li>
<a class="reference internal" href="asyncio-task#asyncio-example-task-cancel"><span class="std std-ref">Handling CancelledError to run code on cancellation request</span></a>.</li> <li>See also the full list of <a class="reference internal" href="asyncio-exceptions#asyncio-exceptions"><span class="std std-ref">asyncio-specific exceptions</span></a>.</li> </ul> </section> <div class="_attribution">
  <p class="_attribution-p">
    &copy; 2001&ndash;2023 Python Software Foundation<br>Licensed under the PSF License.<br>
    <a href="https://docs.python.org/3.12/library/asyncio-api-index.html" class="_attribution-link">https://docs.python.org/3.12/library/asyncio-api-index.html</a>
  </p>
</div>