<pre><codeclass="python language-python hljs"><coro> = <async_function>(<args>) <spanclass="hljs-comment"># Creates a coroutine by calling async def function.</span>
<obj> = <spanclass="hljs-keyword">await</span><coroutine><spanclass="hljs-comment"># Starts the coroutine and returns its result.</span>
<task> = aio.create_task(<coroutine>) <spanclass="hljs-comment"># Schedules the coroutine for execution.</span>
<obj> = <spanclass="hljs-keyword">await</span><task><spanclass="hljs-comment"># Returns coroutine's result. Also <task>.cancel().</span>
<pre><codeclass="python language-python hljs"><coro> = <async_function>(<args>) <spanclass="hljs-comment"># Creates a coroutine by calling async def function.</span>
<obj> = <spanclass="hljs-keyword">await</span><coroutine><spanclass="hljs-comment"># Starts the coroutine and returns its result.</span>
<task> = aio.create_task(<coroutine>) <spanclass="hljs-comment"># Schedules the coroutine for execution.</span>
<obj> = <spanclass="hljs-keyword">await</span><task><spanclass="hljs-comment"># Returns coroutine's result. Also <task>.cancel().</span>
</code></pre>
<pre><codeclass="python language-python hljs"><coro> = aio.gather(<coro/task>, ...) <spanclass="hljs-comment"># Schedules coros. Returns list of results on await.</span>
<iter> = aio.as_completed(<coros/tasks>) <spanclass="hljs-comment"># Iterator of coros. All return next result on await.</span>
<pre><codeclass="python language-python hljs"><coro> = aio.gather(<coro/task>, ...) <spanclass="hljs-comment"># Schedules coros. Returns list of results on await.</span>
<iter> = aio.as_completed(<coros/tasks>) <spanclass="hljs-comment"># Iter of coros that return next result on await.</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
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>