Upgrade to Pro — share decks privately, control downloads, hide ads and more …

async/await and why it's awesome

async/await and why it's awesome

An introduction to async/await and asyncio.

Yury Selivanov

July 21, 2016
Tweet

More Decks by Yury Selivanov

Other Decks in Programming

Transcript

  1. async/await and why it’s awesome Yury Selivanov EuroPython 2016 or

    an introduction to 
 async/await and asyncio
  2. Yury Selivanov – @1st1 About me • Co-founder of MagicStack

    (http://magic.io); check our company out! • Avid Python user since 2008. • CPython core developer since 2013.
  3. Yury Selivanov – @1st1 What I do for Python •

    Co-authored and implemented PEP 362:
 inspect.signature API. • Authored and implemented PEP 492:
 async/await syntax. • Co-maintainer of asyncio with Guido and Victor Stinner. • Created uvloop & asyncpg.
  4. Yury Selivanov – @1st1 One obvious way to do it

    • Callbacks and Deferreds; Twisted since 2002. • Stackless Python & greenlets. • Python 2.5: coroutines with yield. • Python 3.3: coroutines with yield from. • Python 3.5: async/await.
  5. Yury Selivanov – @1st1 • Dedicated syntax; concise and readable.

    • New builtin type for coroutines. • New concepts: async for and async with. • Generic, framework agnostic design. • Fast: only ~2x slower than a function call.
 10-100x faster than yield coroutines. Why async/await is the answer
  6. Yury Selivanov – @1st1 async/await • Subtype of generators; shares

    a lot of code. • await —> yield from —> YIELD_FROM opcode. • @types.coroutine. • __await__. • __aenter__, __aexit__, __aiter__, __anext__.
  7. Yury Selivanov – @1st1 asyncio • Developed and actively maintained

    by the BDFL. • A toolbox for frameworks and protocols. • Part of Standard Library. • Twisted (soon) and Tornado (now) can interoperate.
  8. Yury Selivanov – @1st1 asyncio: what’s inside? • Standardized pluggable

    event loop. • Interfaces for Protocols and Transports. • Factories for servers and connections; streams. • Futures and Tasks: callbacks + coroutines, timeouts, cancellation, etc. • Subprocess, queues, synchronisation primitives.
  9. Yury Selivanov – @1st1 asyncio is simple. • asyncio.get_event_loop() •

    loop.create_task(). • loop.run_until_complete() and loop.run_forever(). • asyncio.gather(). • loop.run_in_executor(). • loop.close().
  10. Yury Selivanov – @1st1 loop.create_task() Wraps coroutines in a “coroutine

    runner”: a mechanism for the 
 event loop to work with 
 async/await.
  11. Yury Selivanov – @1st1 loop.run_in_executor() Runs slow 
 CPU-intensive or

    blocking IO code in a thread
 or in a process pool.
  12. Yury Selivanov – @1st1 asyncio: debugging • PYTHONASYNCIODEBUG=1 python program.py


    or loop.set_debug().
 • Configure python logging to see errors. • Configure your test runner to print out warnings.
  13. Yury Selivanov – @1st1 github.com/magicstack/uvloop • 99.(9)% compatible asyncio event

    loop. • Written in Cython. • Uses libuv under the hood. • Fast Tasks and Futures = faster async/await. • Super fast IO.
  14. Yury Selivanov – @1st1 Language Summit 2015 • First time

    discussed the idea with Guido. • Two months before feature freeze. • He liked it!
  15. Yury Selivanov – @1st1 PEP & Implementation • 2-5 days

    for the first draft of the PEP. • 40 hours to prototype in CPython. • 5 iterations of the PEP. • ~500 emails on python-ideas and python-dev. • Nick Coghlan and Victor Stinner reviewed the patch.