How to run wsgi along the side of the daphne ASGI for django channels
Asked Answered
H

1

11

I am using Django channels in my project using using official Django channels v2, my simple channels app is completed and working fine if run python manage.py runserver but I want to run Django channels on a different port so I am now using daphne. When I use daphne with run command my_project.asgi:application --port 8001 it works fine on 8001 port

INFO     Starting server at tcp:port=8001:interface=127.0.0.1
INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)

and if I also run python manage.py runserver in another terminal at the same time it works fine. Now both channels on port 8001 and Django on 8000 port works as expected but my runserver command runs my application as ASGI/Channels instead of wsgi development server,

Starting ASGI/Channels version 2.2.0 development server at http://127.0.0.1:8000/

instead of

Starting development server at http://127.0.0.1:8000/

settings.py

ASGI_APPLICATION = 'my_project.routing.application'

WSGI_APPLICATION = 'my_project.wsgi.application'

If I debug any function in views.py request, it is ASGI request instead of Django wsgi request

asgi.py
import os
import django
from channels.routing import get_default_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
django.setup()
application = get_default_application()

My questions are as follows:

  1. How can I get Django WSGI request instead of ASGI request in my normal function view request(like def index(request)) or if we install django channels every request become ASGI request?
  2. what is the use of the python mange.py runworker command
Hypomania answered 5/9, 2019 at 8:3 Comment(0)
C
3

Like you can read here: https://asgi.readthedocs.io/en/latest/

ASGI (Asynchronous Server Gateway Interface) is a spiritual successor to WSGI, intended to provide a standard interface between async-capable Python web servers, frameworks, and applications.

Where WSGI provided a standard for synchronous Python apps, ASGI provides one for both asynchronous and synchronous apps, with a WSGI backwards-compatibility implementation and multiple servers and application frameworks.

so answer for your question nr 1 is: Yes, all requests will be ASGI.

Question nr 2 - it's a command to run multiple workers to process your channel requests in asynchronous way https://channels.readthedocs.io/en/1.x/deploying.html#run-worker-servers

Centerboard answered 5/9, 2019 at 9:39 Comment(8)
for question 1 if answer is yes then how third party apps will behave they were expecting django request right so from now onwards if i send the ASGI request(or instance) can they break??Hypomania
or if yes then WSGI_APPLICATION is completely waste right?Hypomania
ASGI request has all attributes that normal django request has so this is not a problem, it should be transparent for you.WSGI_APPLICATION is necessary for runserver command https://mcmap.net/q/1098315/-why-do-we-have-to-provide-wsgi_application-variable-in-django-settingsCenterboard
yes boss and even if i comment out the code WSGI_APPLICATION in settings file my application and runserver working fineHypomania
@pako can you also see this my problem is also the similar one #59528305Pal
Same question, quite green to Django. Installed channels, and now I can't seem to start my Django development server. runserver starts channels ASGI server, How can I run Django development server like it ran previously when I was not using channels?Kendricks
@AdityaSingh provide more details. Why you cannot run development server?Centerboard
Thank you for your comment @pawelbylina, but I figured it out. There was an issue in my routing.py file. It's working fine now.Kendricks

© 2022 - 2024 — McMap. All rights reserved.