django channels redis 关闭时间过长被杀掉
django channels redis took too long to shut down and was killed
所以我试图用频道做一些基本的事情。
我在 html 中写了一个脚本给 return 一条消息
脚本
<script>
const chatsocket = new WebSocket(
'ws://'+window.location.host+'/ws/test/'
)
chatsocket.onmessage = function (e) {
const data = JSON.parse(e.data)
console.log(data)
chatsocket.close()
}
</script>
我的consumers.py
from channels.exceptions import StopConsumer
from channels.generic.websocket import AsyncWebsocketConsumer
import json
class NotificationConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.group_name = 'notificationgroup'
await self.channel_layer.group_add(
self.group_name,
self.channel_name
)
await self.accept()
await self.channel_layer.group_send(
self.group_name,
{
'type': 'tester_message',
'tester': 'hello world'
}
)
async def tester_message(self, event):
tester = event['tester']
await self.send(text_data=json.dumps({
'tester': tester
}))
async def disconnect(self, close_code):
await self.channel_layer.group_discard(
self.group_name,
self.channel_name
)
raise StopConsumer()
所以基本上我在控制台中得到了我想要的输出...当我有
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels.layers.InMemoryChannelLayer'
}
}
但是当我使用 aws redis 集群时(显然是在安装了 channels-redis 之后)
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('aws redis endpoint', 6379)],
},
},
}
我收到这个错误
WebSocket HANDSHAKING /ws/test/ [127.0.0.1:51551]
WebSocket DISCONNECT /ws/test/ [127.0.0.1:51551]
Application instance <Task pending name='Task-4' coro=<StaticFilesWrapper.__call__() running at E:\intes\sync\shero\venv\lib\site-packages\channels\staticfiles.py:44> wait_for=<Future pending cb=[BaseSelectorEventLoop._sock_write_done(1216)(), <TaskWakeupMethWrapper object at 0x000001F7D5226C70>()]>> for connection <WebSocketProtocol client=['127.0.0.1', 51551] path=b'/ws/test/'> took too long to shut down and was killed.
我做错了什么?
我做错了。显然,你不能让 aws-redis 在外面工作。您必须在同一 VPN 内的 EC2 上安装您的应用程序 运行。然后,它起作用了!
所以我试图用频道做一些基本的事情。 我在 html 中写了一个脚本给 return 一条消息
脚本
<script>
const chatsocket = new WebSocket(
'ws://'+window.location.host+'/ws/test/'
)
chatsocket.onmessage = function (e) {
const data = JSON.parse(e.data)
console.log(data)
chatsocket.close()
}
</script>
我的consumers.py
from channels.exceptions import StopConsumer
from channels.generic.websocket import AsyncWebsocketConsumer
import json
class NotificationConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.group_name = 'notificationgroup'
await self.channel_layer.group_add(
self.group_name,
self.channel_name
)
await self.accept()
await self.channel_layer.group_send(
self.group_name,
{
'type': 'tester_message',
'tester': 'hello world'
}
)
async def tester_message(self, event):
tester = event['tester']
await self.send(text_data=json.dumps({
'tester': tester
}))
async def disconnect(self, close_code):
await self.channel_layer.group_discard(
self.group_name,
self.channel_name
)
raise StopConsumer()
所以基本上我在控制台中得到了我想要的输出...当我有
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels.layers.InMemoryChannelLayer'
}
}
但是当我使用 aws redis 集群时(显然是在安装了 channels-redis 之后)
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('aws redis endpoint', 6379)],
},
},
}
我收到这个错误
WebSocket HANDSHAKING /ws/test/ [127.0.0.1:51551]
WebSocket DISCONNECT /ws/test/ [127.0.0.1:51551]
Application instance <Task pending name='Task-4' coro=<StaticFilesWrapper.__call__() running at E:\intes\sync\shero\venv\lib\site-packages\channels\staticfiles.py:44> wait_for=<Future pending cb=[BaseSelectorEventLoop._sock_write_done(1216)(), <TaskWakeupMethWrapper object at 0x000001F7D5226C70>()]>> for connection <WebSocketProtocol client=['127.0.0.1', 51551] path=b'/ws/test/'> took too long to shut down and was killed.
我做错了什么?
我做错了。显然,你不能让 aws-redis 在外面工作。您必须在同一 VPN 内的 EC2 上安装您的应用程序 运行。然后,它起作用了!