'Running django with ASGI?

when I run the app using:

gunicorn --log-level debug --workers 3 myapp.asgi:application --worker-class uvicorn.workers.UvicornWorker

I see the warning

ASGI 'lifespan' protocol appears unsupported.
  1. after reading here I understand that django do not support, but is this have any effect on my app? or where should the effect be?
  2. My app is using sync endpoints, for example:
class MyViewSet(viewsets.ModelViewSet):
    queryset = My.objects.all()
    serializer_class = MySerializer

is by running using ASGI the call to the database would be async?

  • I don't use any web sockets
  1. I can see online many version for the asgi.py file, with manny different middleware and the django.setup() keyword, where can I find a documentation about the use cases?


Solution 1:[1]

exec envdir .envdir gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --timeout 300 \
  --workers $NUM_WORKERS \
  --bind=unix:$SOCKFILE \
  --preload \
  -k uvicorn.workers.UvicornWorker

Solution 2:[2]

  1. I doesn't seem that the lifespan feature should be blocking any Django apps that are not using that feature.

  2. If you use sync code in your views, then switching to ASGI doesn't magically make your view asynchronously. You need async wrappers around it. Check out Django docs about the adapters sync_to_async() and async_to_sync().

  3. I have never seen the usage of django.setup(). I don't understand your question fully. This is how an asgi.py file should look like:

import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MyService.settings.local')
application = get_asgi_application()

Still, personally, after setting this all up, my views are still synchronously. I have opened this question about it: Uvicorn async workers are still working synchronously

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marin
Solution 2 physicalattraction