Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!