Django频道与daphne和worker超时

Django频道与daphne和worker超时,django,worker,django-channels,daphne,Django,Worker,Django Channels,Daphne,我对django频道有问题。 我的Django应用程序与用于HTTP请求的WSGI一起完美运行。 我尝试迁移到通道以允许websocket请求,结果表明,在安装通道并运行ASGI(daphne)和worker之后,服务器会回答错误503,浏览器会显示错误504(超时),以显示以前工作的http请求(例如管理页面)。 我阅读了所有我能找到的教程,但我不知道问题出在哪里。此外,如果我使用“runserver”运行,它工作得很好 我在应用程序前面有一个Nginx(在一个单独的服务器上),充当代理和负载

我对django频道有问题。 我的Django应用程序与用于HTTP请求的WSGI一起完美运行。 我尝试迁移到通道以允许websocket请求,结果表明,在安装通道并运行ASGI(daphne)和worker之后,服务器会回答错误503,浏览器会显示错误504(超时),以显示以前工作的http请求(例如管理页面)。 我阅读了所有我能找到的教程,但我不知道问题出在哪里。此外,如果我使用“runserver”运行,它工作得很好

我在应用程序前面有一个Nginx(在一个单独的服务器上),充当代理和负载平衡器。 我将Django 1.9.5与asgi redis>=0.10.0、Channel>=0.17.0和daphne>=0.15.0一起使用。wsgi.py和asgi.py文件位于同一文件夹中。Redis正在工作

我以前在WSGI中使用的命令(如果切换回它,该命令仍然有效)是:
uwsgi--http:8000--master--enable threads--module Cats.wsgi

使用runserver工作的命令是:
python manage.py运行服务器0.0.0.0:8000

与其他两个命令一起工作的请求失败的命令有:
daphne-b0.0.0-p8000猫。asgi:通道层
python manage.py runworker

其他资料: 我在安装的应用程序中添加了“频道”(在settings.py中)

其他设置.py相关信息

CHANNEL_LAYERS = {
"default": {
    "BACKEND": "asgi_redis.RedisChannelLayer",
    "ROUTING": "Cats.routing.app_routing",
    "CONFIG": {
        "hosts": [(os.environ['REDIS_HOST'], 6379)],
    },
},
}

Cats/routing.py

from channels.routing import route, include
from main.routing import routing as main_routing

app_routing = [
    include(main_routing, path=r"^/ws/main"),
]
from channels.routing import route, include

http_routing = [
]

stream_routing = [
    route('websocket.receive', 'main.consumers.ws_echo'), #just for test once it will work
]

routing = [
    include(stream_routing),
    include(http_routing),
]
main/routing.py

from channels.routing import route, include
from main.routing import routing as main_routing

app_routing = [
    include(main_routing, path=r"^/ws/main"),
]
from channels.routing import route, include

http_routing = [
]

stream_routing = [
    route('websocket.receive', 'main.consumers.ws_echo'), #just for test once it will work
]

routing = [
    include(stream_routing),
    include(http_routing),
]
main/consumers.py

def ws_echo(message):
message.reply_channel.send({
    'text': message.content['text'],
})

#this consumer is just for test once it will work
你知道会出什么问题吗?非常感谢大家的帮助!泰

编辑: 我尝试了一个新东西:

python manage.py runserver 0.0.0.0:8000 --noworker
python manage.py runworker
当python manage.py runserver 0.0.0.0:8000运行时,这不起作用了


任何有帮助的想法?

频道将对未路由的请求使用默认视图。 假设您正确使用javascripts,我建议您只使用默认的Cats/routing.py文件,如下所示:

from channels.routing import route
from main.consumers import *


app_routing = [
    route('websocket.connect', ws_echo, path="/ws/main")
]
或者使用“反向”来帮助您选择路径

from django.urls import reverse

from channels.routing import route
from main.consumers import *


app_routing = [
    route('websocket.connect', ws_echo, path=reverse('main view name'))
]
我认为你的消费者也应该改变。当浏览器使用WebSocket进行连接时,服务器应首先处理添加消息回复频道的问题。比如:

def ws_echo(message):
    Group("notifications").add(message.reply_channel)
    Group("notifications").send({
        "text": json.dumps({'testkey':'testvalue'})
    })
发送功能可能会在不同的事件中调用,“通知”组可能会更改为具有专用于用户的通道。差不多

from channels.auth import channel_session_user_from_http

@channel_session_user_from_http
def ws_echo(message):
    Group("notify-private-%s" % message.user.id).add(message.reply_channel)
    Group("notify-private-%s" % message.user.id).send({
        "text": json.dumps({'testkey':'testvalue'})
    })

如果您使用的是heroku或dokku,请确保已正确设置“规模”以包括辅助进程。默认情况下,他们将只运行web实例,而不运行worker

希罗库

heroku ps:scale web=1:free worker=1:free
为dokku创建一个名为
dokku\u SCALE
的文件,并添加:

web=1
worker=1
见:


编辑了我的帖子,尝试将工作者与“runserver”分离。它失败了。你有没有检查过redis,daphne是否真的将请求放入队列?你是否同时解决了这个问题?我们在生产环境中运行通道,但当我尝试在我们的开发服务器上部署相同的项目时,我遇到了这个问题。工作者收到请求,但它停止在那里。daphne(runserver)返回一个503,在浏览器中我得到504。。。只是忘记了一个&:python manage.py runworker&python manage.py runserver 0.0.0:8000——现在指定给用户的worker要简单得多:
message.reply\u channel.send({'text':json.dumps({'test':'tetest'}))
除非您希望用户拥有多个都需要该消息的连接。的确,message.reply\u channel.send可以工作,但我认为多个选项卡是webapps的合法用法,因此需要多个连接