我将Django频道与单独的Javascript前端结合使用。 Websockets工作正常。我现在想做的是:
message.channel_session['my_id']
然而,有时候这似乎有效,有时我得到KeyError
; my_id
似乎不存在。我的代码:
routing.py
:
from channels.routing import route
from chat.consumers import ws_connect, ws_receive
channel_routing = [
route("websocket.connect", ws_connect),
route("websocket.receive", ws_receive),
]
consumers.py
:
import uuid
from channels.sessions import channel_session
@channel_session
def ws_connect(message):
message.channel_session['my_id'] = str(uuid.uuid4())
@channel_session
def ws_receive(message):
my_id = message.channel_session['my_id'] # this one sometimes fails...
但是,当我将通道层后端从Redis切换到内存时,它工作正常(我使用./manage.py runserver
所以我不运行单独的工作人员)
有什么想法吗?是否存在竞争条件?