Alternatives for Deploying Django with ASGI Servers
Django with Hypercorn
-
Install Hypercorn
Use pip to install Hypercorn:
pip install hypercorn
-
Configure Django ASGI application
Django projects use ASGI applications for handling asynchronous requests. Ensure your Django project has an
asgi.py
file at the project root. This file defines the ASGI application object (application
).# asgi.py import os from django.core.asgi import get_asgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<your_project_name>.settings') application = get_asgi_application()
Replace
<your_project_name>
with your actual project name. -
Run Django with Hypercorn
Navigate to your project directory in the terminal and run the following command:
hypercorn <your_project_name>.asgi:application
This command tells Hypercorn to use the ASGI application defined in
<your_project_name>.asgi:application
.
- Integrate Hypercorn with process managers like systemd for automatic startup and management in production.
- Consider using a production-ready WSGI server like Gunicorn in front of Hypercorn for additional features like process management and hot reloading.
asgi.py (Basic)
# asgi.py
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<your_project_name>.settings')
application = get_asgi_application()
This code defines the basic ASGI application for your Django project. It retrieves the settings module using os.environ.setdefault
and then uses get_asgi_application
to create the application object.
asgi.py (With Custom Settings)
# asgi.py
import os
def get_asgi_application():
""" Function to get the ASGI application with custom logic (optional)
You can add environment checks or custom configuration here before
creating the application.
"""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<your_project_name>.settings')
return get_asgi_application()
application = get_asgi_application()
This version allows for some customization before creating the application. You can perform checks or additional configuration within the get_asgi_application
function.
hypercorn.config.py (Basic Configuration)
# hypercorn.config.py
BIND = "0.0.0.0:8000" # Listen on all interfaces, port 8000
WORKERS = 3 # Adjust worker count based on your needs
This configuration file defines two basic settings for Hypercorn:
WORKERS
: Sets the number of worker processes to handle requests concurrently (adjust based on your system resources).BIND
: Specifies the address and port to listen on (here, all interfaces on port 8000).
Running Hypercorn with configuration
hypercorn hypercorn.config:application
This command uses the configuration file hypercorn.config.py
and specifies the application object to run.
Alternative ASGI Servers
- Daphne
Designed specifically for running Django Channels applications that require WebSocket support. Use Daphne if your Django project utilizes real-time features. - Uvicorn
This is another popular ASGI server known for its simplicity and speed. It's a good choice for development and low-traffic deployments. It's included by default in some Django project templates.
Traditional WSGI Servers with ASGI Adapter
- Gunicorn
A mature and production-ready WSGI server commonly used with Django. It offers features like process management, hot reloading, and worker configuration. You can use Gunicorn with an ASGI adapter likedaphne
for WebSocket support in combination with Django Channels.
Additional Options
- WSGI Servers (No ASGI Support)
Traditional WSGI servers like Nginx or Apache require additional configuration to handle Django applications. This approach might be more complex for modern Django deployments. - Django Development Server
This built-in server is suitable for local development but not recommended for production due to performance limitations.
Choosing the Right Option
The best alternative depends on your specific requirements:
- Production with High Traffic
Gunicorn with Hypercorn for maximum performance is a common choice. - Production with WebSockets
Consider Daphne with Gunicorn for process management. - Development
Uvicorn or the built-in Django development server are good options for local development.