Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
Django channels session issue with Redis as channel layer backend

I use Django Channels in combination with a seperate Javascript frontend. Websockets work fine. What I now want to do is:

  • when user opens website, backend generates a specific id. The backends saves that id in the session with message.channel_session['my_id']
  • on subsequent calls to the backend via the websocket, I want to retreive that key from the session.

However, sometimes this seems to work, and sometimes I get a KeyError; my_id does not seem to exist. My code:

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),
]

And 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...

However, when I switch channel layer backend from Redis to in-memory, it works fine (I use ./manage.py runserver so I don't run a seperate worker)

Any ideas? Is there a race condition or something?

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda