【发布时间】:2022-01-19 22:15:59
【问题描述】:
我正在尝试发送 consumer.py 信息以显示在 consumer.py 之外的客户端上。
我已经引用了Send message using Django Channels from outside Consumer classthis previous question,但子进程.group_send或.group_add似乎不存在,所以我觉得我可能错过了一些非常容易的东西。
Consumers.py
from channels.generic.websocket import WebsocketConsumer
from asgiref.sync import async_to_sync
class WSConsumer(WebsocketConsumer):
def connect(self):
async_to_sync(self.channel_layer.group_add)("appDel", self.channel_name)
self.accept()
self.render()
appAlarm.py
def appFunc(csvUpload):
#csvUpload=pd.read_csv(request.FILES['filename'])
csvFile = pd.DataFrame(csvUpload)
colUsernames = csvFile.usernames
print(colUsernames)
channel_layer = get_channel_layer()
for user in colUsernames:
req = r.get('https://reqres.in/api/users/2')
print(req)
t = req.json()
data = t['data']['email']
print(user + " " + data)
message = user + " " + data
async_to_sync(channel_layer.group_send)(
'appDel',
{'type': 'render', 'message': message}
)
它抛出了这个错误:
async_to_sync(channel_layer.group_send)(
AttributeError: 'NoneType' object has no attribute 'group_send'
并且在将group_add 剥离更多以弄清楚发生了什么时,会为group_add 抛出相同的错误,但根据HERE 的文档,我觉得这应该可以工作。
【问题讨论】:
标签: python django django-channels