* **Coroutines have a lot in common with threads, but unlike threads, they only give up control when they call another coroutine and they don’t use as much memory.**
* **Coroutine definition starts with `'async'` and its call with `'await'`.**
* **`'asyncio.run(<coroutine>)'` is the main entry point for asynchronous programs.**
* **Functions wait(), gather() and as_completed() start multiple coroutines at the same time.**
* **Asyncio module also provides its own [Queue](#queue), [Event](#semaphore-event-barrier), [Lock](#lock) and [Semaphore](#semaphore-event-barrier) classes.**
#### Runs a terminal game where you control an asterisk that must avoid numbers:
```python
import asyncio as aio
```
```python
<coro> = <async_func>(<args>) # Creates a coroutine.
<obj> = await <coroutine> # Starts the coroutine and returns result.
<task> = aio.create_task(<coroutine>) # Schedules coroutine for execution.
<li><strong>Coroutines have a lot in common with threads, but unlike threads, they only give up control when they call another coroutine and they don’t use as much memory.</strong></li>
<li><strong>Coroutine definition starts with <codeclass="python hljs"><spanclass="hljs-string">'async'</span></code> and its call with <codeclass="python hljs"><spanclass="hljs-string">'await'</span></code>.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'asyncio.run(<coroutine>)'</span></code> is the main entry point for asynchronous programs.</strong></li>
<li><strong>Functions wait(), gather() and as_completed() start multiple coroutines at the same time.</strong></li>
<li><strong>Asyncio module also provides its own <ahref="#queue">Queue</a>, <ahref="#semaphoreeventbarrier">Event</a>, <ahref="#lock">Lock</a> and <ahref="#semaphoreeventbarrier">Semaphore</a> classes.</strong></li>
</ul><div><h4id="runsaterminalgamewhereyoucontrolanasteriskthatmustavoidnumbers">Runs a terminal game where you control an asterisk that must avoid numbers:</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span> asyncio, collections, curses, curses.textpad, enum, random, time
<iter> = aio.as_completed(<coros/tasks>) <spanclass="hljs-comment"># Iter of coros. All return next result when awaited.</span>
</code></pre>
<div><h4id="runsaterminalgamewhereyoucontrolanasteriskthatmustavoidnumbers">Runs a terminal game where you control an asterisk that must avoid numbers:</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span> asyncio, collections, curses, curses.textpad, enum, random, time
P = collections.namedtuple(<spanclass="hljs-string">'P'</span>, <spanclass="hljs-string">'x y'</span>) <spanclass="hljs-comment"># Position</span>
D = enum.Enum(<spanclass="hljs-string">'D'</span>, <spanclass="hljs-string">'n e s w'</span>) <spanclass="hljs-comment"># Direction</span>
W, H = <spanclass="hljs-number">15</span>, <spanclass="hljs-number">7</span><spanclass="hljs-comment"># Width, Height</span>
P = collections.namedtuple(<spanclass="hljs-string">'P'</span>, <spanclass="hljs-string">'x y'</span>) <spanclass="hljs-comment"># Position</span>
D = enum.Enum(<spanclass="hljs-string">'D'</span>, <spanclass="hljs-string">'n e s w'</span>) <spanclass="hljs-comment"># Direction</span>
W, H = <spanclass="hljs-number">15</span>, <spanclass="hljs-number">7</span><spanclass="hljs-comment"># Width, Height</span>