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

Taking Django's ORM Async

Andrew Godwin
September 04, 2020

Taking Django's ORM Async

A talk I gave at DjangoCon AU 2020 about the plans and sequencing for making Django's ORM support asynchronous call styles.

Andrew Godwin

September 04, 2020
Tweet

More Decks by Andrew Godwin

Other Decks in Programming

Transcript

  1. Andrew Godwin / @andrewgodwin Hi, I’m Andrew Godwin • Django

    core developer • Worked on Migrations, Channels & Async • Not currently in Australia
  2. Andrew Godwin / @andrewgodwin 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__ Implemented async request flow
  3. Andrew Godwin / @andrewgodwin Phase One: ASGI Support Allowing Django

    to be async at all Phase Two: Async Views Unlocking async use in normal apps Phase Three: The ORM High-level async use for the most common case
  4. Andrew Godwin / @andrewgodwin The ORM is the majority of

    Django And, of course, the most complex part.
  5. Andrew Godwin / @andrewgodwin Async is… a bit different You

    can't quite do everything you're used to
  6. Andrew Godwin / @andrewgodwin Can't tell if a function returns

    a coroutine! There are standard hints, but no actual guaranteed way
  7. Andrew Godwin / @andrewgodwin async def calculate(x): result = await

    coroutine(x) return result # These both return a coroutine def calculate(x): result = coroutine(x) return result
  8. Andrew Godwin / @andrewgodwin Some things do have nice analogues!

    They have async versions of the operations that call a different special method.
  9. Andrew Godwin / @andrewgodwin 1. Async Model API Querysets &

    model instances mostly 2. Async Query Internals Django is internally async through its stack 3. Async Database Adapters Async all the way down, no threads at all
  10. Andrew Godwin / @andrewgodwin Query Threaded if in async mode

    After Phase One QuerySet Managers Model Connection Compiler Database Library
  11. Andrew Godwin / @andrewgodwin Query Threaded if in async mode

    After Phase Two QuerySet Managers Model Connection Compiler Database Library
  12. Andrew Godwin / @andrewgodwin No sign of an async DBAPI...

    yet It might emerge once there's a need for it
  13. Andrew Godwin / @andrewgodwin Databases via threads? Not the worst!

    Native async only matters for pure performance
  14. Andrew Godwin / @andrewgodwin Transactions are tricky. They are very

    threadlocal, and that's not great with async
  15. Andrew Godwin / @andrewgodwin So what's first? (Apart from having

    to curse various non-PostgreSQL databases)
  16. Andrew Godwin / @andrewgodwin Asyncio only benefits IO-bound code Code

    that thrashes the CPU doesn't benefit at all