SlideShare a Scribd company logo
1 of 24
Download to read offline
FRÖJD
Asynchronous Python &
Django - a status report
Jakob Stasilowicz
‣ Python and WSGI/ASGI
‣ The ASGI ecosystem
‣ ASGI capable frameworks
‣ Django?
FRÖJD
‣ Python and WSGI/ASGI
‣ The ASGI ecosystem
‣ ASGI capable frameworks
‣ Django?
FRÖJD
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 :)

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”
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) 

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
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
FRÖJD
Why asynchronous?
Källa: https://youtu.be/u8GSFEg5lnU?t=845
‣ Python and WSGI/ASGI
‣ The ASGI ecosystem
‣ ASGI capable frameworks
‣ Django?
FRÖJD
FRÖJD
ASGI - Asynchronous Server
Gateway Interface
EXAMPLE CODE AVAILABLE HERE: HTTPS://GITHUB.COM/STASILO/PYTHON-ASGI-DJANGO-MEETUP
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)

FRÖJD
ASGI - servers
‣ Python and WSGI/ASGI
‣ The ASGI ecosystem
‣ ASGI capable frameworks
‣ Django?
FRÖJD
FRÖJD
ASGI capable frameworks
• Starlette / FastAPI

• Sanic

• Quart

• Django Channels (merely an ASGI-”front”)

FRÖJD
Starlette
• “Seriously impressive performance” :)
• Websockets
• In-process background tasks
• GraphQL
• …and more
FRÖJD
Starlette / FastAPI / Sanic - a
short example with
Websockets
CODE AVAILABLE HERE: HTTPS://GITHUB.COM/STASILO/PYTHON-ASGI-DJANGO-MEETUP
‣ Python and WSGI/ASGI
‣ The ASGI ecosystem
‣ ASGI capable frameworks
‣ Django?
FRÖJD AGENDA
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

FRÖJD
Django - parts in need of a
rewrite
•ORM
•Views
•SMTP-clients

•Validation (API-validation, form-validation) 

•Caching

•etc.
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())
FRÖJD
Django 3 - status
- Done
• ASGI base implementation 

- Work in progress
• Async views 

- Planned
• ORM
• Template-engine
• Cache handling
• Test-client etc.

FRÖJD
Thank you!

More Related Content

More from Fröjd Interactive

More from Fröjd Interactive (9)

Sweden.se case presentation from Wordcamp in Krakow
Sweden.se case presentation from Wordcamp in KrakowSweden.se case presentation from Wordcamp in Krakow
Sweden.se case presentation from Wordcamp in Krakow
 
Agile architecture - for Sveriges Arkitekter #arkfrukost
Agile architecture - for Sveriges Arkitekter #arkfrukostAgile architecture - for Sveriges Arkitekter #arkfrukost
Agile architecture - for Sveriges Arkitekter #arkfrukost
 
Wordpress - Mythbusters. Benefits & challenges with WP
Wordpress - Mythbusters. Benefits & challenges with WPWordpress - Mythbusters. Benefits & challenges with WP
Wordpress - Mythbusters. Benefits & challenges with WP
 
The Perfect Web Project - does it exist?
The Perfect Web Project - does it exist?The Perfect Web Project - does it exist?
The Perfect Web Project - does it exist?
 
Workshop: How to make a risk analysis of a web project
Workshop: How to make a risk analysis of a web projectWorkshop: How to make a risk analysis of a web project
Workshop: How to make a risk analysis of a web project
 
Exercise: My strength pitfall challenge & allergy
Exercise: My strength pitfall challenge & allergyExercise: My strength pitfall challenge & allergy
Exercise: My strength pitfall challenge & allergy
 
14 Web tech trends 2014
14 Web tech trends 201414 Web tech trends 2014
14 Web tech trends 2014
 
Digitala trender & strategi 2013
Digitala trender & strategi 2013Digitala trender & strategi 2013
Digitala trender & strategi 2013
 
Guide mobile project
Guide mobile projectGuide mobile project
Guide mobile project
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

Asynchronous Python & Django – A Status Report

  • 1.
  • 2. FRÖJD Asynchronous Python & Django - a status report Jakob Stasilowicz
  • 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)

  • 15. ‣ Python and WSGI/ASGI ‣ The ASGI ecosystem ‣ ASGI capable frameworks ‣ Django? FRÖJD
  • 16. FRÖJD ASGI capable frameworks • Starlette / FastAPI
 • Sanic
 • Quart
 • Django Channels (merely an ASGI-”front”)

  • 17. FRÖJD Starlette • “Seriously impressive performance” :) • Websockets • In-process background tasks • GraphQL • …and more
  • 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.