Skip to content

Commit

Permalink
Threading, Coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
gto76 committed Aug 20, 2024
1 parent d781e15 commit a0affa5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.**
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -1768,7 +1768,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
<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>
Expand Down Expand Up @@ -1909,9 +1909,9 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
&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

Expand Down Expand Up @@ -2932,7 +2932,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment


<footer>
<aside>August 19, 2024</aside>
<aside>August 20, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

Expand Down

0 comments on commit a0affa5

Please sign in to comment.