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

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 KrakowFröjd Interactive
 
Agile architecture - for Sveriges Arkitekter #arkfrukost
Agile architecture - for Sveriges Arkitekter #arkfrukostAgile architecture - for Sveriges Arkitekter #arkfrukost
Agile architecture - for Sveriges Arkitekter #arkfrukostFröjd Interactive
 
Wordpress - Mythbusters. Benefits & challenges with WP
Wordpress - Mythbusters. Benefits & challenges with WPWordpress - Mythbusters. Benefits & challenges with WP
Wordpress - Mythbusters. Benefits & challenges with WPFröjd Interactive
 
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?Fröjd Interactive
 
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 projectFröjd Interactive
 
Exercise: My strength pitfall challenge & allergy
Exercise: My strength pitfall challenge & allergyExercise: My strength pitfall challenge & allergy
Exercise: My strength pitfall challenge & allergyFröjd Interactive
 
Digitala trender & strategi 2013
Digitala trender & strategi 2013Digitala trender & strategi 2013
Digitala trender & strategi 2013Frö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

SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 

Recently uploaded (20)

SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 

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.