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

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

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.