Django 如何从使用者类外部发送channel_会话数据

Django 如何从使用者类外部发送channel_会话数据,django,websocket,django-rest-framework,django-channels,Django,Websocket,Django Rest Framework,Django Channels,我在从channel_会话向消费者类以外的用户发送数据时遇到问题 我的消费者喜欢:- class myconsumer(AsyncWebsocketConsumer): #all init connect function def subscribe(self): self.channel_Session = payload['data'] #some other functions 现在的问题是,我想通过一个称为线程的异步函数发送self.chan

我在从channel_会话向消费者类以外的用户发送数据时遇到问题

我的消费者喜欢:-

class myconsumer(AsyncWebsocketConsumer):
    #all init connect function

    def subscribe(self):
        self.channel_Session = payload['data']

    #some other functions
现在的问题是,我想通过一个称为线程的异步函数发送self.channel_会话

async def sendData():
    while True:
        #send data
        await asyio.sleep(5)

class thread(multiprocess.Process):
     try:
         loop = asyncio.bew_event_loop()
     except Exception as e:
          loop = asyncio.get_event_loop()
     loop.run_until_complete(sendData())
此线程在启动服务器时启动

问题: 我找不到发送该频道会话的方法


任何帮助都将不胜感激。

首先使用
get\u channel\u layer()
获取与redis通信的活动层,然后调用
group\u send
调用
type
参数指定的使用者方法

from channels.layers import get_channel_layer

async def sendData():
    channel_layer = get_channel_layer()
    while True:
        await channel_layer.group_send(
            << your_group_name_here >>,
            {
                'type': 'subscribe',
                'message': 'custom message'
            }
        )
        await asyncio.sleep(5)
从channels.layers导入获取通道层
异步def sendData():
通道层=获取通道层()
尽管如此:
等待通道\u层。组\u发送(
>,
{
“类型”:“订阅”,
“消息”:“自定义消息”
}
)
等待异步睡眠(5)