Atomic increment of a counter in django


I'm trying to atomically increment a simple counter in Django. My code looks like this:

from models import Counter
from django.db import transaction

def increment_counter(name):
    counter = Counter.objects.get_or_create(name = name)[0]
    counter.count += 1

If I understand Django correctly, this should wrap the function in a transaction and make the increment atomic. But it doesn't work and there is a race condition in the counter update. How can this code be made thread-safe?

10/21/2009 5:49:08 AM

Accepted Answer

New in Django 1.1

Counter.objects.get_or_create(name = name)
Counter.objects.filter(name = name).update(count = F('count')+1)

or using an F expression:

counter = Counter.objects.get_or_create(name = name)
counter.count = F('count') +1 update_fields=["count"] )

Remember to Specify Which fields to update, Or you might encounter race conditions on other possible fields of the model!

A topic on the race condition associated with this approach has been added to the official documentation.

11/29/2018 8:03:31 PM

In Django 1.4 there is support for SELECT ... FOR UPDATE clauses, using database locks to make sure no data is accesses concurrently by mistake.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow