EuroPython 2018: Trio: A pythonic way to do async programming

Emmanuel Leblond is a python web developer from France.

Writing async code in 2000 was terrible. Then we invented futures. Now we've got asyncio, and everything's good, right?

asyncio

Weeeell. Error handling is still very unweildy. You have no real connection between the place an exception was thrown and … anything. Exceptions bubble up to the event loop, and then the event loop prints them to stdout for lack of better ideas. Error handling should involve cler and complete stacktraces across coroutines, exception propagation, and control of coroutine life spans.

In comes Trio

Trio was built by Nathaniel J. Smith.

In Trio, there are nurseries, which are async context managers for coroutines. Every coroutine belongs to a nursery, so there is a clear parent.

It gives you useful interfaces like move_on_after to cancel a coroutine after a set interval.

And that's it – Trio doesn't add callbacks, or promises/futures, or tasks, or transport protocols, or even a visible event loop – it's just nurseries and coroutines.

Other uses

  • I/O for networking, sleep, filesystem, signals
  • pytest and hypothesis helper modules
  • API for inter task communication (Event, Queeu)
  • Control-C works
  • Compatibility layer to use asyncio libraries on trio

Recommended reading