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
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?