Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atomic operations in Django?

Tags:

I'm trying to implement (what I think is) a pretty simple data model for a counter:

class VisitorDayTypeCounter(models.Model):     visitType = models.CharField(max_length=60)     visitDate = models.DateField('Visit Date')     counter = models.IntegerField() 

When someone comes through, it will look for a row that matches the visitType and visitDate; if this row doesn't exist, it will be created with counter=0.

Then we increment the counter and save.

My concern is that this process is totally a race. Two requests could simultaneously check to see if the entity is there, and both of them could create it. Between reading the counter and saving the result, another request could come through and increment it (resulting in a lost count).

So far I haven't really found a good way around this, either in the Django documentation or in the tutorial (in fact, it looks like the tutorial has a race condition in the Vote part of it).

How do I do this safely?

like image 986
Tony Arkles Avatar asked Nov 11 '08 05:11

Tony Arkles


People also ask

What is atomic transaction in operating system?

An atomic transaction is an indivisible and irreducible series of database operations such that either all occurs, or nothing occurs. A guarantee of atomicity prevents updates to the database occurring only partially, which can cause greater problems than rejecting the whole series outright.

How does Django handle concurrency?

When you run multiple workers of your Django application, you will run into concurrency issues when the same queryset is updated by different processes at the same time. To prevent this, use select_for_update inside a transaction block to fetch your queryset so that it is locked until the transaction is completed.

Is SQL transaction atomic?

In SQL databases transaction atomicity is implemented most frequently using write-ahead logging (meaning that the transaction log entries are written before the actual tables and indexes are updated).

What is integrity error in Django?

The Django docs give a clue about what's happening here: Inside a transaction, when a call to a PostgreSQL cursor raises an exception (typically IntegrityError ), all subsequent SQL in the same transaction will fail with the error “current transaction is aborted, queries ignored until end of transaction block”.


1 Answers

As of Django 1.1 you can use the ORM's F() expressions.

from django.db.models import F product = Product.objects.get(name='Venezuelan Beaver Cheese') product.number_sold = F('number_sold') + 1 product.save() 

For more details see the documentation:

https://docs.djangoproject.com/en/1.8/ref/models/instances/#updating-attributes-based-on-existing-fields

https://docs.djangoproject.com/en/1.8/ref/models/expressions/#django.db.models.F

like image 193
bjunix Avatar answered Oct 13 '22 00:10

bjunix