Browse Source

Threading, Coroutines

pull/116/merge
Jure Šorn 2 months ago
parent
commit
a0affa5b40
2 changed files with 8 additions and 8 deletions
  1. 6
      README.md
  2. 10
      index.html

6
README.md

@ -2154,7 +2154,7 @@ with <lock>: # Enters the block by calling acq
<bool> = <Future>.done() # Checks if the thread has finished executing.
<obj> = <Future>.result(timeout=None) # Waits for thread to finish and returns result.
<bool> = <Future>.cancel() # Cancels or returns False if running/finished.
<iter> = as_completed(<coll_of_Futures>) # Next() waits for next completed Future.
<iter> = as_completed(<coll_of_Futures>) # `next(<iter>)` returns next completed Future.
```
* **Map() and as\_completed() also accept 'timeout'. It causes futures.TimeoutError when next() is called/blocking. Map() times from original call and as_completed() from first call to next(). As\_completed() fails if next() is called too late, even if thread finished on time.**
* **Exceptions that happen inside threads are raised when next() is called on map's iterator or when result() is called on a Future. Its exception() method returns exception or None.**
@ -2329,9 +2329,9 @@ import asyncio as aio
```
```python
<coro> = aio.gather(<coro/task>, ...) # Schedules coroutines. Returns results when awaited.
<coro> = aio.gather(<coro/task>, ...) # Schedules coros. Returns list of results on await.
<coro> = aio.wait(<tasks>, …) # `aio.ALL/FIRST_COMPLETED`. Returns (done, pending).
<iter> = aio.as_completed(<coros/tasks>) # Iter of coros. All return next result when awaited.
<iter> = aio.as_completed(<coros/tasks>) # Iterator of coros. All return next result on await.
```
#### Runs a terminal game where you control an asterisk that must avoid numbers:

10
index.html

@ -54,7 +54,7 @@
<body>
<header>
<aside>August 19, 2024</aside>
<aside>August 20, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -1768,7 +1768,7 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
<pre><code class="python language-python hljs">&lt;bool&gt; = &lt;Future&gt;.done() <span class="hljs-comment"># Checks if the thread has finished executing.</span>
&lt;obj&gt; = &lt;Future&gt;.result(timeout=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Waits for thread to finish and returns result.</span>
&lt;bool&gt; = &lt;Future&gt;.cancel() <span class="hljs-comment"># Cancels or returns False if running/finished.</span>
&lt;iter&gt; = as_completed(&lt;coll_of_Futures&gt;) <span class="hljs-comment"># Next() waits for next completed Future.</span>
&lt;iter&gt; = as_completed(&lt;coll_of_Futures&gt;) <span class="hljs-comment"># `next(&lt;iter&gt;)` returns next completed Future.</span>
</code></pre>
<ul>
<li><strong>Map() and as_completed() also accept 'timeout'. It causes futures.TimeoutError when next() is called/blocking. Map() times from original call and as_completed() from first call to next(). As_completed() fails if next() is called too late, even if thread finished on time.</strong></li>
@ -1909,9 +1909,9 @@ delattr(&lt;obj&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>)
&lt;task&gt; = aio.create_task(&lt;coroutine&gt;) <span class="hljs-comment"># Schedules the coroutine for execution.</span>
&lt;obj&gt; = <span class="hljs-keyword">await</span> &lt;task&gt; <span class="hljs-comment"># Returns result. Also &lt;task&gt;.cancel().</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;coro&gt; = aio.gather(&lt;coro/task&gt;, ...) <span class="hljs-comment"># Schedules coroutines. Returns results when awaited.</span>
<pre><code class="python language-python hljs">&lt;coro&gt; = aio.gather(&lt;coro/task&gt;, ...) <span class="hljs-comment"># Schedules coros. Returns list of results on await.</span>
&lt;coro&gt; = aio.wait(&lt;tasks&gt;, …) <span class="hljs-comment"># `aio.ALL/FIRST_COMPLETED`. Returns (done, pending).</span>
&lt;iter&gt; = aio.as_completed(&lt;coros/tasks&gt;) <span class="hljs-comment"># Iter of coros. All return next result when awaited.</span>
&lt;iter&gt; = aio.as_completed(&lt;coros/tasks&gt;) <span class="hljs-comment"># Iterator of coros. All return next result on await.</span>
</code></pre>
<div><h4 id="runsaterminalgamewhereyoucontrolanasteriskthatmustavoidnumbers">Runs a terminal game where you control an asterisk that must avoid numbers:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> asyncio, collections, curses, curses.textpad, enum, random
@ -2932,7 +2932,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the activ
<footer>
<aside>August 19, 2024</aside>
<aside>August 20, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

Loading…
Cancel
Save