Browse Source

Coroutines

pull/135/merge
Jure Šorn 3 weeks ago
parent
commit
4ac35a722d
3 changed files with 34 additions and 34 deletions
  1. 26
      README.md
  2. 30
      index.html
  3. 12
      parse.js

26
README.md

@ -2327,30 +2327,30 @@ import asyncio as aio
```
```python
<coro> = <async_function>(<args>) # Creates a coroutine by calling async def function.
<obj> = await <coroutine> # Starts the coroutine and returns its result.
<task> = aio.create_task(<coroutine>) # Schedules the coroutine for execution.
<obj> = await <task> # Returns coroutine's result. Also <task>.cancel().
<coro> = <async_function>(<args>) # Creates a coroutine by calling async def function.
<obj> = await <coroutine> # Starts the coroutine and returns its result.
<task> = aio.create_task(<coroutine>) # Schedules the coroutine for execution.
<obj> = await <task> # Returns coroutine's result. Also <task>.cancel().
```
```python
<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>) # Iterator of coros. All return next result on await.
<coro> = aio.gather(<coro/task>, ...) # Schedules coros. Returns list of results on await.
<coro> = aio.wait(<tasks>, return_when=…) # `'ALL/FIRST_COMPLETED'`. Returns (done, pending).
<iter> = aio.as_completed(<coros/tasks>) # Iter of coros that return next result on await.
```
#### Runs a terminal game where you control an asterisk that must avoid numbers:
```python
import asyncio, collections, curses, curses.textpad, enum, random
P = collections.namedtuple('P', 'x y') # Position
D = enum.Enum('D', 'n e s w') # Direction
W, H = 15, 7 # Width, Height
P = collections.namedtuple('P', 'x y') # Position
D = enum.Enum('D', 'n e s w') # Direction
W, H = 15, 7 # Width, Height
def main(screen):
curses.curs_set(0) # Makes cursor invisible.
screen.nodelay(True) # Makes getch() non-blocking.
asyncio.run(main_coroutine(screen)) # Starts running asyncio code.
curses.curs_set(0) # Makes cursor invisible.
screen.nodelay(True) # Makes getch() non-blocking.
asyncio.run(main_coroutine(screen)) # Starts running asyncio code.
async def main_coroutine(screen):
moves = asyncio.Queue()

30
index.html

@ -55,7 +55,7 @@
<body>
<header>
<aside>February 5, 2025</aside>
<aside>February 6, 2025</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -1902,25 +1902,25 @@ delattr(&lt;obj&gt;, <span class="hljs-string">'&lt;name&gt;'</span>)
</code></pre></div>
<pre><code class="python language-python hljs">&lt;coro&gt; = &lt;async_function&gt;(&lt;args&gt;) <span class="hljs-comment"># Creates a coroutine by calling async def function.</span>
&lt;obj&gt; = <span class="hljs-keyword">await</span> &lt;coroutine&gt; <span class="hljs-comment"># Starts the coroutine and returns its result.</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 coroutine's result. Also &lt;task&gt;.cancel().</span>
<pre><code class="python language-python hljs">&lt;coro&gt; = &lt;async_function&gt;(&lt;args&gt;) <span class="hljs-comment"># Creates a coroutine by calling async def function.</span>
&lt;obj&gt; = <span class="hljs-keyword">await</span> &lt;coroutine&gt; <span class="hljs-comment"># Starts the coroutine and returns its result.</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 coroutine's 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 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"># Iterator of coros. All return next result on await.</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;, return_when=…) <span class="hljs-comment"># `'ALL/FIRST_COMPLETED'`. Returns (done, pending).</span>
&lt;iter&gt; = aio.as_completed(&lt;coros/tasks&gt;) <span class="hljs-comment"># Iter of coros that 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
P = collections.namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Position</span>
D = enum.Enum(<span class="hljs-string">'D'</span>, <span class="hljs-string">'n e s w'</span>) <span class="hljs-comment"># Direction</span>
W, H = <span class="hljs-number">15</span>, <span class="hljs-number">7</span> <span class="hljs-comment"># Width, Height</span>
P = collections.namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Position</span>
D = enum.Enum(<span class="hljs-string">'D'</span>, <span class="hljs-string">'n e s w'</span>) <span class="hljs-comment"># Direction</span>
W, H = <span class="hljs-number">15</span>, <span class="hljs-number">7</span> <span class="hljs-comment"># Width, Height</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">(screen)</span>:</span>
curses.curs_set(<span class="hljs-number">0</span>) <span class="hljs-comment"># Makes cursor invisible.</span>
screen.nodelay(<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Makes getch() non-blocking.</span>
asyncio.run(main_coroutine(screen)) <span class="hljs-comment"># Starts running asyncio code.</span>
curses.curs_set(<span class="hljs-number">0</span>) <span class="hljs-comment"># Makes cursor invisible.</span>
screen.nodelay(<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Makes getch() non-blocking.</span>
asyncio.run(main_coroutine(screen)) <span class="hljs-comment"># Starts running asyncio code.</span>
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main_coroutine</span><span class="hljs-params">(screen)</span>:</span>
moves = asyncio.Queue()
@ -2931,7 +2931,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the active
<footer>
<aside>February 5, 2025</aside>
<aside>February 6, 2025</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

12
parse.js

@ -115,14 +115,14 @@ const MATCH_EXAMPLE =
const COROUTINES =
'<span class="hljs-keyword">import</span> asyncio, collections, curses, curses.textpad, enum, random\n' +
'\n' +
'P = collections.namedtuple(<span class="hljs-string">\'P\'</span>, <span class="hljs-string">\'x y\'</span>) <span class="hljs-comment"># Position</span>\n' +
'D = enum.Enum(<span class="hljs-string">\'D\'</span>, <span class="hljs-string">\'n e s w\'</span>) <span class="hljs-comment"># Direction</span>\n' +
'W, H = <span class="hljs-number">15</span>, <span class="hljs-number">7</span> <span class="hljs-comment"># Width, Height</span>\n' +
'P = collections.namedtuple(<span class="hljs-string">\'P\'</span>, <span class="hljs-string">\'x y\'</span>) <span class="hljs-comment"># Position</span>\n' +
'D = enum.Enum(<span class="hljs-string">\'D\'</span>, <span class="hljs-string">\'n e s w\'</span>) <span class="hljs-comment"># Direction</span>\n' +
'W, H = <span class="hljs-number">15</span>, <span class="hljs-number">7</span> <span class="hljs-comment"># Width, Height</span>\n' +
'\n' +
'<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">(screen)</span>:</span>\n' +
' curses.curs_set(<span class="hljs-number">0</span>) <span class="hljs-comment"># Makes cursor invisible.</span>\n' +
' screen.nodelay(<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Makes getch() non-blocking.</span>\n' +
' asyncio.run(main_coroutine(screen)) <span class="hljs-comment"># Starts running asyncio code.</span>\n' +
' curses.curs_set(<span class="hljs-number">0</span>) <span class="hljs-comment"># Makes cursor invisible.</span>\n' +
' screen.nodelay(<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Makes getch() non-blocking.</span>\n' +
' asyncio.run(main_coroutine(screen)) <span class="hljs-comment"># Starts running asyncio code.</span>\n' +
'\n' +
'<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main_coroutine</span><span class="hljs-params">(screen)</span>:</span>\n' +
' moves = asyncio.Queue()\n' +

Loading…
Cancel
Save