ASGI Ref
ASGI specification and utilities, version 3.11.1, released on a regular cadence. Provides ASGI specs, helper code, and adapters for Python applications.
Warnings
- breaking Import paths have changed in asgiref 3.11.1; ensure correct imports to avoid ImportError.
- gotcha Using AsyncToSync without proper context can lead to deadlocks; ensure it's used within an appropriate event loop.
Install
-
pip install asgiref
Imports
- ASGIApp
from asgiref.typing import ASGIApp
- AsyncToSync
from asgiref.sync import AsyncToSync
Quickstart
import os
from asgiref.sync import AsyncToSync
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from django.urls import path
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings')
application = ProtocolTypeRouter({
'http': get_asgi_application(),
'websocket': AuthMiddlewareStack(
URLRouter([
path('ws/some_path/', your_consumer.as_asgi()),
])
),
})