Talk by Jakob Stasilowicz from Django Meetup Stockholm group on the current status on Async/Await, ASGI, the future of Django and why it is a big deal
Held in 2020-02-14
3. ‣ Python and WSGI/ASGI
‣ The ASGI ecosystem
‣ ASGI capable frameworks
‣ Django?
FRÖJD
4. ‣ Python and WSGI/ASGI
‣ The ASGI ecosystem
‣ ASGI capable frameworks
‣ Django?
FRÖJD
5. FRÖJD
Python 3.5 - a crossroads
• Introduced async/await
• …and thus enables a new, more efficient concurrency
model (non-blocking I/O)
• If you’ve worked with NodeJS, things should look & feel
familiar :)
6. FRÖJD
WSGI - Web Server Gateway
Interface
Introduced 2003 in PEP-333
“WSGI applications are a single, synchronous callable
that takes a request and returns a response”
7. FRÖJD
WSGI - drawbacks
• No async / await
• No web socket support as sockets are asynchronous by nature
(wsgi.websockets - unofficial workaround)
• No HTTP/2 (from end to end, possible to use NGINX as reverse proxy
running HTTP/2, though)
8. FRÖJD
Why asynchronous?
Performance!
The resource efficient switching of task-based concurrency means that thousands of
lightweight tasks can be executed alongside each other, without the high overhead
of thread-based switching.”
- https://www.encode.io/articles/hello-asgi
9. FRÖJD
Concurrency?
• Signifies requests/writes/general i/o that occurs
concurrently/simultaneously
• This can be achieved through, for example, multiple
servers (horizontal scaling), multiple processes,
threads or…
• Async/await :)
• Async/Await - an alternative to threading
• Simply put: throughput increases with concurrency
which increases/is simplified with asynchronous code
11. ‣ Python and WSGI/ASGI
‣ The ASGI ecosystem
‣ ASGI capable frameworks
‣ Django?
FRÖJD
12. FRÖJD
ASGI - Asynchronous Server
Gateway Interface
EXAMPLE CODE AVAILABLE HERE: HTTPS://GITHUB.COM/STASILO/PYTHON-ASGI-DJANGO-MEETUP
13. FRÖJD
ASGI - Asynchronous Server
Gateway Interface
• “The solution” to WSGI shortcomings:
concurrency (incl. background tasks), websockets,
http/2
• ASGI1 / ASGI2 / ASGI3
• ASGI3 arrived 2019 and is a well worked out spec
• ASGI is a broader abstraction, compared to WSGI
which only handles HTTP, which makes it more usable
(i.e. websockets)
18. FRÖJD
Starlette / FastAPI / Sanic - a
short example with
Websockets
CODE AVAILABLE HERE: HTTPS://GITHUB.COM/STASILO/PYTHON-ASGI-DJANGO-MEETUP
19. ‣ Python and WSGI/ASGI
‣ The ASGI ecosystem
‣ ASGI capable frameworks
‣ Django?
FRÖJD AGENDA
20. FRÖJD
Django - consequences?
• Django is currently not ASGI-ready
• A big rewrite of various parts is needed as…
• …async/await is incompatible with synchronous code
21. FRÖJD
Django - parts in need of a
rewrite
•ORM
•Views
•SMTP-clients
•Validation (API-validation, form-validation)
•Caching
•etc.
22. FRÖJD
Django - ORM
• Django ORM, SQLAlchemy & DBAPI are all synchronous libs
• There is no complete asynchronous ORM yet. Tom Christie is
working on something though :)
• All database-I/O will be/needs to be explicit:
• Entails a few design differences: no lazy-loading of relations,
explicit paging of querysets, etc.
• Benefit: reading code and more easily seeing where requests to
the db are made (await db_req())
23. FRÖJD
Django 3 - status
- Done
• ASGI base implementation
- Work in progress
• Async views
- Planned
• ORM
• Template-engine
• Cache handling
• Test-client etc.