2025年使用daphne部署django channles websocket 项目

使用daphne部署django channles websocket 项目一 概述 在上一篇文章中 链接如下 https www cnblogs com xiao98733417 p 14361893 html 开发了一个 django channles websocket 项目 用的是 asgi 官方推荐使用 asgi 服务器 daphne 来处理 websocket 请求 daphne Daphne

大家好,我是讯享网,很高兴认识大家。

开发了一个django channles websocket 项目,用的是asgi。官方推荐使用asgi服务器daphne,来处理websocket请求

python -m pip install daphne

对于一个典型的 Django 项目,可以像下面这样来启动 Daphne

如果需要更改运行端口,使用以下命令:

-b 监听地址

-p 监控端口

已经开发好了,但是直接使用daphne运行,会遇到以下错误:

比如1:

django.core.exceptions.AppRegistryNotReady: Apps aren’t loaded yet.

复制代码
import os
import django
os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘websocket_demo.settings’)
django.setup()

Import other Channels classes and consumers here.

from channels.routing import ProtocolTypeRouter, URLRouter

from apps.websocket_app.urls import websocket_urlpatterns

from websocket_demo.urls import websocket_urlpatterns

application = get_asgi_application()

application = ProtocolTypeRouter({
# Explicitly set ‘http’ key using Django’s ASGI application.
“http”: get_asgi_application(),
‘websocket’: AuthMiddlewareStack(
URLRouter(
websocket_urlpatterns
)
),
})
复制代码
注意:django.setup()要置顶,不能在底部,否则使用daphne启动会报上面的错误。

运行项目

注意:要在manage.py同级目录下执行此命令

daphne websocket_demo.asgi:application -b 0.0.0.0 -p 8000

根据官方文档,推荐使用nginx+daphne+supervise

ip地址:192.168.31.165

生成配置文件


讯享网

echo_supervisord_conf > /etc/supervisord.conf

修改配置文件/etc/supervisord.conf,最后一行增加

[include]
files = supervisord.d/*.ini
表示配置文件读取supervisord.d目录下所有后缀为.ini的文件。

创建配置目录,并创建配置文件

mkdir /etc/supervisord.d/
vi /etc/supervisord.d/asgi.ini
内容如下:

TCP socket used by Nginx backend upstream

socket=tcp://localhost:8000

Directory where your site’s project files are located

directory=/tmp/internal_tools

Each process needs to have a separate socket file, so we use process_num

Make sure to update “mysite.asgi” to match your project name

command=/virtualenvs/venv1/bin/daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers internal_tools.asgi:application

Number of processes to startup, roughly the number of CPUs you have

numprocs=4

Give each process a unique name so they can be told apart

process_name=asgi%(process_num)d

Automatically start and recover processes

Choose where you want your log to go

stdout_logfile=/var/log/asgi.log
redirect_stderr=true
复制代码
注意:红色部分,请根据实际情况修改。

启动supervisord

supervisord -c /etc/supervisord.conf

查看asgi运行状态

supervisorctl

asgi:asgi0 RUNNING pid 17567, uptime 0:00:04
asgi:asgi1 RUNNING pid 17566, uptime 0:00:04
asgi:asgi2 RUNNING pid 17569, uptime 0:00:04
asgi:asgi3 RUNNING pid 17568, uptime 0:00:04
可以看到,有4个进程。如果状态不是RUNNING,请查看ini配置文件,是否正常。

yum install -y nginx

修改虚拟配置文件/etc/nginx/conf.d/asgi.conf

复制代码
upstream channels-backend {
server localhost:8000;
}
server {
listen 8093;
location / {
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
proxy_pass http://channels-backend;

 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Host $server_name; } 

讯享网

}
复制代码
注意红色部分,upstream 是asgi的监听端口。在server里面的8093是对外端口,也可以改成别的,根据实际情况而定。

最后加载nginx配置文件

nginx -s reload

Vue.prototype. a p i h o s t = " h t t p : / / 192.168.31.165 : 8093 " V u e . p r o t o t y p e . apihost = "http://192.168.31.165:8093" Vue.prototype. apihost="http://192.168.31.165:8093"Vue.prototype.websockethost = “ws://192.168.31.165:8093”
注意:daphne不光可以处理asgi,它也可以处理wsgi,没有必要部署uswgi来处理wsgi了。

总之:nginx+daphne+supervise就可以处理django的所有功能了。

本文参考链接:

https://stackoverflow.com/questions//django-apps-arent-loaded-yet-when-using-asgi

https://docs.djangoproject.com/zh-hans/3.1/howto/deployment/asgi/daphne/

https://blog.csdn.net/_/article/details/

https://www.cnblogs.com/chenjw-note/p/12516097.html

小讯
上一篇 2025-03-12 13:25
下一篇 2025-02-28 17:38

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/15054.html