Are asyncio's EventLoop tasks created with loop.create_task a FIFO
<p>I can not find any documentation on this, but empirically it <em>seems</em> that it is.</p>

<p>In what order will the coroutines 1 and 2 run in the following three examples and is the order always guaranteed?</p>

<p><strong>A</strong></p>

<pre><code>loop.run_until_complete(coro1)
loop.run_until_complete(coro2)
loop.run_forever()
</code></pre>

<p><strong>B</strong></p>

<pre><code>loop.create_task(coro1)
loop.create_task(coro2)
loop.run_forever()
</code></pre>

<p><strong>C</strong></p>

<pre><code>loop.create_task(coro1)
loop.run_until_complete(coro2)
loop.run_forever()
</code></pre>

<p>etc.</p>

<p>In your first example, <code>coro1</code> will run until it is complete. Then <code>coro2</code> will run. This is essentially the same as if they were both synchronous functions. </p>

<p>In your second example, <code>coro1</code> will run until it's told to await. At that point control is yielded to <code>coro2</code>. <code>coro2</code> will run until it's told to await. At that point the loop will check to see if <code>coro1</code> is ready to resume. This will repeat until both are finished and then the loop will just wait. </p>

<p>In your final example, <code>coro2</code> starts first, following the same back and forth as the previous example, and then the process will stop once it, <code>coro2</code>, is done. Then <code>coro1</code> will resume until it's done and then the loop will just wait. </p>

<p>A fourth example is</p>

<pre><code>loop.run_until_complete(
    asyncio.gather(
        asyncio.ensure_future(coro1),
        asyncio.ensure_future(coro2),
    )
)
</code></pre>

<p>It will behave like the second example except it will stop once both are complete. </p>

python python-asyncio