4

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?
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
helpper
  • 2,058
  • 4
  • 13
  • 32
  • Better one question at one post... Lifespan is just a event so, like if the application start up, it will know, restart, then it will know only. – kyorilys Mar 24 '22 at 09:16

2 Answers2

0
  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

physicalattraction
  • 6,485
  • 10
  • 63
  • 122
-1
exec envdir .envdir gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --timeout 300 \
  --workers $NUM_WORKERS \
  --bind=unix:$SOCKFILE \
  --preload \
  -k uvicorn.workers.UvicornWorker
Marin
  • 1,098
  • 14
  • 33
  • 9
    How does it answer the questions? Can you maybe elaborate? – helpper Sep 28 '21 at 07:08
  • 3
    It helps more if you supply an explanation why this is the preferred solution and explain how it works. We want to educate, not just provide code. – the Tin Man Sep 28 '21 at 19:53