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

145
Visualizações
Solving race conditions in Django/postgres

I have a django view which sends a payment receipt, and in multi-threaded environments, this view sends the receipt multiple times if called more than once in quick succession. My code which does the update looks like

updated = Transaction.objects \
    .filter(id=transaction_id, status='processing') \
    .update(status='paid')
if updated:
    # send email

Everything I've read on the internet suggests that selecting and updating rows that need to be updated in one query as I've done here should work. What am I doing wrong?

I also tried

with transaction.atomic():
    trans = Transaction.objects.select_for_update() \
                       .get(id=transaction_id)
    if trans.status == 'processing':
        trans.status = 'paid'
        trans.save()
        updated = True
    else:
        updated = False

Other things I've tried:

  • do everything in raw sql, i.e. cursor().execute('...')
  • calling transaction.commit() after the update
  • setting the Postgres isolation level to SERIALIZABLE

The site runs apache/mod_wsgi, Django 1.8 and Postgres 9.4, and the problem goes away if I set threads=1 processes=1 in my mod_wsgi configuration. I also reproduced the problem with gunicorn, with multiple workers. It's hosted on Webfaction although would assume that doesn't matter.

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