karnataka
karnataka

Reputation: 445

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

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

Upvotes: 11

Views: 10747

Answers (2)

Chukwujiobi Canon
Chukwujiobi Canon

Reputation: 4067

Q1.

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 becomes an ASGI request?

No, if you install Django Channels, every request doesn't automatically become ASGI. But if you install Daphne at the top of your settings.INSTALLED_APPS, Daphne will then replace the usual runserver command with its own command which spins up an ASGI server.[Django Channels] So if you want to keep using Django's runserver command, do not install Daphne at the top of your settings.INSTALLED_APPS.

This means that your method of spinning up an ASGI server will now default to: daphne myproject.asgi:application invoked from the root directory of your project (same directory as manage.py).


Q2.

What is the use of the python manage.py runworker command?

Keep in mind that it is not a built-in Django management command but is provided by Django Channels when installed in a project.

It runs background workers that process tasks off the Django Channels layer. This means that it can handle messages sent to specific channel types (e.g., WebSockets, background jobs). And it is commonly used in real-time applications like chat apps, live notifications, and task processing.

Upvotes: 0

pawelbylina
pawelbylina

Reputation: 1485

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

Upvotes: 3

Related Questions