Skip to content

Instantly share code, notes, and snippets.



Last active Jul 12, 2019
What would you like to do?
How on earth does Asyncio work

What are coroutines?

Coroutines are functions that can be paused. We define them using the async syntax:

async def say_something(what):

But coroutines can't be run directly:

>>> say_something("hello")
<coroutine object say_something at 0x1069837c8>

Calling a coroutine won't execute it—coroutines need to be scheduled to be executed.

Event Loops

An event loop is just that—a continuously running loop that checks if an async function is done being paused. In Python (unlike JavaScript) we create the event loop explicitly:

import asyncio

loop = asyncio.get_event_loop()

get_event_loop() will get or create the event loop in the current thread. We must schedule our say_something coroutine on the event loop if we wish to run it.

import asyncio

async def say_something(what):

loop = asyncio.get_event_loop()
task = loop.create_task(say_something("hello world"))

# hello world

A much easier way to run coroutines was added in Python 3.7—using The following is equivalent to the above example:

import asyncio

async def say_something(what):
    print(what)"hello world")) will create the event loop, run our coroutine, and kill the loop once done.



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment