Saúl Ibarra Corretgé: asyncio internals

published May 09, 2014

Saúl Ibarra Corretgé talks about asyncio internals, at PyGrunn.

See the PyGrunn website for more info about this one-day Python conference in Groningen, The Netherlands.

See the first talk introducing asyncio.

In asyncio there is no event class, no abstraction of what an event is. It runs callbacks which are put in a queue.

[Note: I am not going to type over the code examples, too easy to make typos.]

I/O handling APIs are in readyness style (tell us when you can start) or completion style (tell us when you are done). On Windows there are limits, so you may need to do it differently.

Polling: calculate a timeout, block for I/O, process I/O events (schedule callbacks), process timers (schedule callbacks), run pending callbacks.

Coroutines are generator functions, can also receive values. With the @asyncio.coroutine decorator I know I can use yield from on that function.

Futures represent a value which is not there yet. yield from can be used to wait for it. asyncio.wrap_future can be used to wrap a PEP-3148 Future into one of these.

f = Future()
f.set_result(...)  # or f.set_exception
yield from f  # wait until the result arrives

A Task is a unit on concurrent asynchronous work. It is actually a coroutine wrapped in a Future. It schedules callbacks using loop.call_soon. Use asyncio.async to turn a coroutine into a task.

Go read PEP-3156. Don't be afraid of looking under the hood in the source code.

@saghul on Twitter.