Open brylie opened 10 months ago
Hi @brylie, i would like to contribute. Can you tell me how you would like to approach this, because this is a big task and doing it gradually makes more sense.
Yes, this seems like an epic. I suppose a place to start would be to check for feasibility. E.g. check whether we have any synchronous middleware that would force Django to use a thread per request. If this task isn’t feasible, the HTMX task would likely have the most user benefit and could be broken into incremental sub-tasks. #88
Feature Description
Port existing views and database operations in the project to utilize Python/Django's asynchronous features. This enhancement involves refactoring current synchronous views and database interactions to asynchronous ones using the latest async capabilities provided by Django. The objective is to improve the overall performance and scalability of the application by enabling non-blocking database operations and handling concurrent requests more efficiently.
Motivation
As the application grows in complexity and user base, performance and scalability become increasingly important. Traditional synchronous processing can lead to inefficiencies, especially under heavy load. By adopting Django's async features, we can handle more requests simultaneously, reduce response times, and improve user experience, particularly in data-intensive operations. This shift is also in line with modern web development practices, ensuring our application remains robust and future-proof.
Possible Implementation
Alternatives Considered
The primary alternative to implementing Django's async features is to continue using synchronous views and database operations. This approach relies on the existing, well-understood synchronous programming model, ensuring stability and maintaining the current performance benchmarks. While it lacks the potential performance improvements of async processing, it avoids the complexities and potential challenges involved in refactoring to asynchronous code. This approach would be more about optimizing within the current synchronous framework, focusing on database query optimizations, efficient view logic, and potentially leveraging caching strategies to improve performance.
Additional Context
This transition to asynchronous processing is part of a strategic move towards adopting modern web development practices. It requires careful planning and testing to ensure seamless integration with the existing synchronous parts of the application. The latest version of Django's improved async support makes it a suitable time for this upgrade.
Documentation
For adding Django async support, the official Django documentation provides a comprehensive guide on implementing asynchronous support. Key aspects include:
async for
on all QuerySets.asave
andacreate
, which can be used for database operations.sync_to_async()
for calling sync functions from an async context.async_to_sync()
andsync_to_async()
from theasgiref.sync
module to adapt calling styles between sync and async code.asyncio.CancelledError
might be raised if a client disconnects before the view returns a response.The Django documentation covers these topics and more in-depth and is an essential resource for implementing async in Django. You can read more about asynchronous support in Django on the official documentation.
Additionally, the DEV Community provides an article discussing the comparison between WSGI and ASGI in the context of Django, highlighting the benefits of ASGI for async support, including scalability, concurrency, and real-time functionality. It also covers why ASGI is needed for async support in Django and provides a comparison of Uvicorn and Daphne, two popular ASGI server implementations. You can read more about this on the DEV Community article.