Django Channels Group.send 在 python 控制台中不工作?

Django Channels Group.send not working in python console?

我在 python 控制台中尝试了 Group(groupname).send,但它似乎不起作用。这是为什么?

这是我的consumers.py安排:

def ws_connect(message):
    message.reply_channel.send({"accept": True})
    Group(secure_group).add(message.reply_channel)


def ws_receive(message):
    # Nothing to do here
    Group(secure_group).send({
        "text": "Received {}".format(message.content['text'])
    })


def ws_disconnect(message):
    Group(secure_group).discard(message.reply_channel)

路由:

from channels.routing import route
from App.consumers import (
    ws_connect,
    ws_receive,
    ws_disconnect
)

channel_routing = [
    route("websocket.connect", ws_connect),
    route("websocket.receive", ws_receive),
    route("websocket.disconnect", ws_disconnect),
]

终端命令:

from channels import Group
#import secure_group here

Group(secure_group).send({ "text": "Tester" })

我所有的客户都没有收到短信。

CHANNEL_LAYERS 配置:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgiref.inmemory.ChannelLayer",
        "ROUTING": "App.routing.channel_routing",
    },
}

Inmemory 通道层不支持 cross-process communication。您不能在其他终端中执行群发。尝试使用 Redis 后端,您可以发送消息。

来自文档 <a href="https://channels.readthedocs.io/en/stable/backends.html#in-memory" rel="nofollow noreferrer">In-Memory</a>