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

Friendly and Modern AsyncIO

Friendly and Modern AsyncIO

AsyncIO is a powerful mechanism for building high-performance services, but it has long been thought of as “too complicated”. But it’s easier than you might think, and with Python 3.7, it’s easier than ever to get started using just the standard library! We'll cover the basics of AsyncIO, take a look at the latest features in 3.7, build friendly and expressive APIs, analyze best practices for third-party libraries, and discuss integrating async code with synchronous libraries and codebases.

John Reese

May 02, 2019
Tweet

More Decks by John Reese

Other Decks in Programming

Transcript

  1. 1. What is AsyncIO 2. Why is it useful 3.

    High level concepts 4. Standard library basics 5. Building friendly, asynchronous APIs Topics
  2. github.com/jreese/pycon $ git clone https://github.com/jreese/pycon $ cd pycon/friendly-asyncio $ python3.7

    -m venv venv $ source venv/bin/activate $ pip install -Ur requirements.txt
  3. 156 runs of fibs in 1.0s: 6.42 msec per run

    151 runs of fibp in 1.0s: 6.65 msec per run
  4. 156 runs of fibs in 1.0s: 6.44 msec per run

    387 runs of fibp in 1.0s: 2.59 msec per run
  5. • Simple TCP server • Accepts an upper bound k

    • “Thinks about it” • Returns a random integer [0,k] Randint-as-a-service $ python3.7 server.py listening on 8080...
  6. $ echo 20 | nc ::1 8080 6 $ echo

    20 | nc ::1 8080 17 $ echo 20 | nc ::1 8080 11
  7. 2 runs of many_random in 1.1s: 0.558 sec per run

    1 runs of many_random in 1.4s: 1.351 sec per run 1 runs of many_random in 1.2s: 1.241 sec per run 0 0.001 0.002 0.003 0.004 32 64 128 256 512 1024
  8. 6 runs of async_random in 1.1s: 0.191 sec per run

    4 runs of async_random in 1.1s: 0.280 sec per run 3 runs of async_random in 1.4s: 0.467 sec per run 0 0.001 0.002 0.003 0.004 32 64 128 256 512 1024 128 256 512 1024 AsyncIO Threads
  9. • Object that is not ready, or done • “done”

    means success or exception • Add callbacks, or periodically check state • Result yields final value, or raise exception Back to the futures
  10. • Object that is not ready, or done • “done”

    means success or exception • Add callbacks, or periodically check state • Result yields final value, or raise exception Back to the futures
  11. • Like futures, without polling or callbacks • Object with

    __await__ method • Must be “awaited” via await keyword • Yields value when completed Awaitables
  12. • Asynchronous function • Dedicated async def syntax • Returns

    a coroutine object when called • Only executes when awaited Coroutines
  13. • Event loop controls execution of pending tasks • Tasks

    wrap and await futures automatically • Tasks are awaitable, yield their wrapped futures Event loops and tasks
  14. • Executes current task until done or blocked • Next

    task picked from queue • Tasks can “starve” other tasks Event loops and tasks
  15. coro: <coroutine object sleep at 0x1049e84c8> task: <Task pending coro=<sleep()

    running at ...> task: <Task finished coro=<sleep() done, …> result=None>
  16. coro: <coroutine object sleep at 0x1049e84c8> task: <Task pending coro=<sleep()

    running at ...> task: <Task finished coro=<sleep() done, …> result=None>
  17. coro: <coroutine object sleep at 0x1049e84c8> task: <Task pending coro=<sleep()

    running at ...> task: <Task finished coro=<sleep() done, …> result=None>
  18. 0: counting to 100000 0: ... 99999 ... 100000 1:

    counting to 100000 1: ... 99999 ... 100000 2: counting to 100000 2: ... 99999 ... 100000 3: counting to 100000 3: ... 99999 ... 100000
  19. 0: counting to 100000 1: counting to 100000 2: counting

    to 100000 3: counting to 100000 0: ... 99999 ... 100000 1: ... 99999 ... 100000 2: ... 99999 ... 100000 3: ... 99999 ... 100000
  20. $ curl http://0.0.0.0:8080/randint/25 15 $ curl http://0.0.0.0:8080/randint/2500 2318 $ python

    6-aiohttp-server.py ======== Running on http://0.0.0.0:8080 ======== (Press CTRL+C to quit)
  21. row #1 from Bob to Alice: I need your help.

    row #2 from Janet to Nina: What time is the developer sync? row #3 from Nina to Janet: I think it's after the design review.
  22. • Focus on common use cases • Make interfaces feel

    natural • Use the most obvious syntax features • Support multiple styles if possible Obvious and natural
  23. • For use in async for loops • Each iteration

    is an async coroutine • Useful for iteration before all data is ready • Iterate large, async datasets without blocking Async Iterables
  24. • Similar to normal iterables • Use __aiter__ to create

    an iterator • Use __anext__ to return the next value • Raise StopAsyncIteration when exhausted Async Iterables
  25. • Ephemeral actions, connections, transactions • Async coroutines at enter

    and exit • Ensure clean up on exit or exception Async Context Managers
  26. • Same pattern as regular context managers • __aenter__ called

    when entering • __aexit__ called at exit or exception Async Context Managers
  27. $ python3.7 10-contexts.py row #4 from Jack to Jill: I'm

    out of water. row #5 from Jack to Terry: Do you know where my pail is?
  28. ai o uvloop ai ess aioredis aiobot am aiofiles aiosqlite

    aiopg s aiounittest aioslack trio uvloop omysql aiohttp aiomultiprocess aioredis lack trio uvloop aioitertools aiostream aiofiles a ltiprocess aioredis aiobotocore aiodns aiounittest aiosl aiostream aiofiles aiosqlite aiopg aiomysql aiohttp aiomultipro e aiodns aiounittest aioslack trio uvloop aioitertools aiostream aiof iopg aiomysql aiohttp aiomultiprocess aioredis aiobotocore aiodns ack trio uvloop aioitertools aiostream aiofiles aiosqlite aiopg s aioredis aiobotocore aiodns aiounittest aioslack tr s aiosqlite aiopg aiomysql aiohttp aiomultipr slack trio uvloop aioitertools aiostrea iprocess aioredis aiobotocore stream aiofiles aiosqli aiounittest aio http aio a Don't reinvent the wheel
  29. ai o uvloop ai ess aioredis aiobot am aiofiles aiosqlite

    aiopg s aiounittest aioslack trio uvloop omysql aiohttp aiomultiprocess aioredis lack trio uvloop aioitertools aiostream aiofiles a ltiprocess aioredis aiobotocore aiodns aiounittest aiosl aiostream aiofiles aiosqlite aiopg aiomysql aiohttp aiomultipro e aiodns aiounittest aioslack trio uvloop aioitertools aiostream aiof iopg aiomysql aiohttp aiomultiprocess aioredis aiobotocore aiodns ack trio uvloop aioitertools aiostream aiofiles aiosqlite aiopg s aioredis aiobotocore aiodns aiounittest aioslack tr s aiosqlite aiopg aiomysql aiohttp aiomultipr slack trio uvloop aioitertools aiostrea iprocess aioredis aiobotocore stream aiofiles aiosqli aiounittest aio http aio a
  30. ai o uvloop ai ess aioredis aiobot am aiofiles aiosqlite

    aiopg s aiounittest aioslack trio uvloop omysql aiohttp aiomultiprocess aioredis lack trio uvloop aioitertools aiostream aiofiles a ltiprocess aioredis aiobotocore aiodns aiounittest aiosl aiostream aiofiles aiosqlite aiopg aiomysql aiohttp aiomultipro e aiodns aiounittest aioslack trio uvloop aioitertools aiostream aiof iopg aiomysql aiohttp aiomultiprocess aioredis aiobotocore aiodns ack trio uvloop aioitertools aiostream aiofiles aiosqlite aiopg s aioredis aiobotocore aiodns aiounittest aioslack tr s aiosqlite aiopg aiomysql aiohttp aiomultipr slack trio uvloop aioitertools aiostrea iprocess aioredis aiobotocore stream aiofiles aiosqli aiounittest aio http aio a
  31. ai o uvloop ai ess aioredis aiobot am aiofiles aiosqlite

    aiopg s aiounittest aioslack trio uvloop omysql aiohttp aiomultiprocess aioredis lack trio uvloop aioitertools aiostream aiofiles a ltiprocess aioredis aiobotocore aiodns aiounittest aiosl aiostream aiofiles aiosqlite aiopg aiomysql aiohttp aiomultipro e aiodns aiounittest aioslack trio uvloop aioitertools aiostream aiof iopg aiomysql aiohttp aiomultiprocess aioredis aiobotocore aiodns ack trio uvloop aioitertools aiostream aiofiles aiosqlite aiopg s aioredis aiobotocore aiodns aiounittest aioslack tr s aiosqlite aiopg aiomysql aiohttp aiomultipr slack trio uvloop aioitertools aiostrea iprocess aioredis aiobotocore stream aiofiles aiosqli aiounittest aio http aio a