我在使用Django Channels制作通知系统时遇到了问题。它在本地工作正常。在生产中(在Webfaction上),它将在几分钟内正常工作,然后停止处理以下错误消息:
ERROR - server - Exception inside application:
File "/home/client/.virtualenvs/project/lib/python3.6/site-packages/channels/sessions.py", line 175, in __call__
return await self.inner(receive, self.send)
File "/home/client/.virtualenvs/project/lib/python3.6/site-packages/channels/middleware.py", line 41, in coroutine_call
await inner_instance(receive, send)
File "/home/client/.virtualenvs/project/lib/python3.6/site-packages/channels/consumer.py", line 54, in __call__
await await_many_dispatch([receive, self.channel_receive], self.dispatch)
File "/home/client/.virtualenvs/project/lib/python3.6/site-packages/channels/utils.py", line 57, in await_many_dispatch
await task
File "/home/client/.virtualenvs/project/lib/python3.6/site-packages/channels_redis/core.py", line 400, in receive
assert not self.receive_lock.locked()
我正在使用:
这都是使用django的开发服务器。
我的消费者看起来像这样:
class NotificationConsumer (AsyncJsonWebsocketConsumer):
slight_ordering = True
async def connect (self):
self.user = self.scope["user"]
await self.accept()
group_name = "notifications_{}".format(self.user.employee.pk)
await self.channel_layer.group_add(group_name, self.channel_name)
async def disconnect (self, code):
self.user = self.scope["user"]
group_name = "notifications_{}".format(self.user.employee.pk)
await self.channel_layer.group_discard (group_name, self.channel_name)
async def user_notification (self, event):
await self.send_json(event)
创建后,将使用post_save信号发送通知:
@receiver(post_save, sender=Notification)
def new_notification (sender, instance, **kwargs):
channel_layer = get_channel_layer()
group_name = "notifications_{}".format(instance.employee.pk)
async_to_sync(channel_layer.group_send)(
group_name, {
"type": "user.notification",
"event": "New notification",
"notification_pk": instance.pk,
}
)
我的路由如下:
application = ProtocolTypeRouter({
'websocket': AuthMiddlewareStack(
URLRouter(
[url(r'^notifications/$', NotificationConsumer),]
)
),
})
最后,我在前端使用WebSocketBridge:
const webSocketBridge = new channels.WebSocketBridge();
webSocketBridge.connect('/notifications/');
webSocketBridge.listen(function(action, stream){
//show the notification
});
如果有人对可能发生的事情以及为什么会出现此self.receive_lock.locked()错误有任何想法,我将不胜感激。
谢谢
答案 0 :(得分:1)
此问题已在channels-redis版本2.3.3中修复。 将channels-redis更新为2.3.3 如果这引发任何进一步的错误或不支持您的安装,请按照以下链接更新您的core.py文件。尽管不建议编辑程序包,但这将解决锁定问题。 Link