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

Async, Python, and the Future

Async, Python, and the Future

A keynote I gave at Python Web Conference 2021.

Andrew Godwin

March 23, 2021
Tweet

More Decks by Andrew Godwin

Other Decks in Programming

Transcript

  1. Hi, I’m Andrew Godwin • Django core developer • Worked

    on Migrations, Channels & Async • Dabbled with Python async since 2009
  2. The Past What is all this async business anyway? The

    Present The long road and where we've got to The Future Is there a perfect solution to all this?
  3. The path was forged by other languages And continues to

    be - we're all one community in the end
  4. 1998 threading module, Stackless Python 2002 Twisted 2006 Greenlets (later

    gevent, eventlet) 2008 multiprocessing module 2012 Tulip, PEP 3156 2014 asyncio module 2005 Coroutine-friendly generators (PEP 342)
  5. 1998 threading module, Stackless Python 2002 Twisted 2006 Greenlets (later

    gevent, eventlet) 2008 multiprocessing module 2012 Tulip, PEP 3156 2014 asyncio module 2005 Coroutine-friendly generators (PEP 342)
  6. # Ready when a timer finishes await asyncio.sleep(1) # Ready

    when network packets return await client.get("http://example.com") # Ready when the coroutine exits await my_function("hello", 64.2)
  7. Can't tell if a function returns a coroutine! There are

    standard hints, but no actual guaranteed way
  8. async def calculate(x): result = await coroutine(x) return result #

    These both return a coroutine def calculate(x): result = coroutine(x) return result
  9. Can't have one function service both How we got here

    makes sense, but it's still annoying sometimes.
  10. WSGIHandler __call__ WSGI Server WSGIRequest URLs Middleware View __call__ ASGIHandler

    __call__ ASGI Server ASGIRequest Asynchronous request path BaseHandler get_response_async BaseHandler get_response URLs Middleware Async View __call__ Django's dual request flows
  11. But, in many ways, the future is here You can

    just write full async Python now, and it works pretty well.
  12. What does this mean for the Web? Our roles are

    changing along with our technology
  13. Parallel Queries After all, we're the experts in fetching data

    Notifications & Events Polling will absolutely wreck your servers Microservices / API aggregation It's a lot quicker to call those 10 things in parallel
  14. Think about your architecture Group things that all do I/O

    & requests together, for future parallelisation
  15. Be the change you want to see. There's a lot

    of work to be done, and never enough of us to do it.