Slide 1

Slide 1 text

TAKING DJANGO'S ORM ANDREW GODWIN // @andrewgodwin ASYNC

Slide 2

Slide 2 text

Andrew Godwin / @andrewgodwin Hi, I’m Andrew Godwin • Django core developer • Worked on Migrations, Channels & Async • Not currently in Australia

Slide 3

Slide 3 text

Andrew Godwin / @andrewgodwin

Slide 4

Slide 4 text

Andrew Godwin / @andrewgodwin Async views are released! Find them in a Django 3.1 near you.

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Andrew Godwin / @andrewgodwin The ORM is the majority of Django And, of course, the most complex part.

Slide 8

Slide 8 text

Andrew Godwin / @andrewgodwin API Design is crucial It must be familiar, yet safe.

Slide 9

Slide 9 text

Andrew Godwin / @andrewgodwin Async is… a bit different You can't quite do everything you're used to

Slide 10

Slide 10 text

Andrew Godwin / @andrewgodwin Can't tell if a function returns a coroutine! There are standard hints, but no actual guaranteed way

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Andrew Godwin / @andrewgodwin You have to namespace async functions I really, really wish we didn't have to

Slide 13

Slide 13 text

Andrew Godwin / @andrewgodwin instance = MyModel.objects.get(id=3) instance = await MyModel.objects.async_get(id=3)

Slide 14

Slide 14 text

Andrew Godwin / @andrewgodwin Can't do asynchronous attribute access! Foreign keys will be a bit less magic.

Slide 15

Slide 15 text

Andrew Godwin / @andrewgodwin book = Book.objects.async_get(id=3) # Will error - needs to run a query print(book.author.name)

Slide 16

Slide 16 text

Andrew Godwin / @andrewgodwin book = Book.objects.get(id=3) .select_related("author") # Works fine - prefetched print(book.author.name)

Slide 17

Slide 17 text

Andrew Godwin / @andrewgodwin Some things do have nice analogues! They have async versions of the operations that call a different special method.

Slide 18

Slide 18 text

Andrew Godwin / @andrewgodwin async for book in Book.objects.filter(name="Django"): print(book)

Slide 19

Slide 19 text

Andrew Godwin / @andrewgodwin The Plan

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Andrew Godwin / @andrewgodwin Query After Phase Three QuerySet Managers Model Connection Compiler Database Library

Slide 24

Slide 24 text

Andrew Godwin / @andrewgodwin We may never get to Phase Three ...and that's alright.

Slide 25

Slide 25 text

Andrew Godwin / @andrewgodwin No sign of an async DBAPI... yet It might emerge once there's a need for it

Slide 26

Slide 26 text

Andrew Godwin / @andrewgodwin Databases via threads? Not the worst! Native async only matters for pure performance

Slide 27

Slide 27 text

Andrew Godwin / @andrewgodwin Transactions are tricky. They are very threadlocal, and that's not great with async

Slide 28

Slide 28 text

Andrew Godwin / @andrewgodwin Some things don't need async Migrations, introspection, most fields

Slide 29

Slide 29 text

Andrew Godwin / @andrewgodwin So what's first? (Apart from having to curse various non-PostgreSQL databases)

Slide 30

Slide 30 text

Andrew Godwin / @andrewgodwin Deciding on async API design Namespacing? async_ prefixes? Something else?

Slide 31

Slide 31 text

Andrew Godwin / @andrewgodwin Grappling with transactions Do we make them work across async/sync boundaries?

Slide 32

Slide 32 text

Andrew Godwin / @andrewgodwin Async connection management Namespacing? async_ prefixes? Something else?

Slide 33

Slide 33 text

Andrew Godwin / @andrewgodwin Asyncio only benefits IO-bound code Code that thrashes the CPU doesn't benefit at all

Slide 34

Slide 34 text

Andrew Godwin / @andrewgodwin There's lots to come! Hopefully starting in 3.2, but no guarantees...

Slide 35

Slide 35 text

Thanks. Andrew Godwin @andrewgodwin // aeracode.org