Django and Sqlite Concurrency Issue

I read a little about concurrency issues with sqlite, but I don't see how they apply to Django, since it is essentially single-threaded. I also do not use multiprocessor modules. I have absolutely no experience in parallel programming, so if someone can determine WHY the following code raises OperationalError: "database is locked", I would appreciate it.

views.py

def screening(request, ovramt=None): errors = [] if request.method == "POST": form = ScreeningForm(request.POST) if form.is_valid(): print "Woo valid!!" return HttpResponse() else: # GET if ovramt is None: o = Ovramt.objects.select_related(depth=1).latest("date_completed") print "found?" print o.id else: try: o = Ovramt.objects.select_related(depth=1).get(id=ovramt) except: errors.append("OVRAMT NOT FOUND") if o.residents.count() <= 0: o.add_active_residents() residents = list(o.residents) 

models.py

 def add_active_residents(self): ssa_res = SSA_Resident.objects.select_related(depth=1).filter(ssa=self.ssa, active=True) for r in ssa_res: self.residents.add(r.resident) # Fails Here self.save() 

The add_active_residents method works fine until it is called from the views module. Is there an open connection with an open database in the view that prevents writing from the model? Does anyone have an explanation why this code will be erroneous?

+4
source share
4 answers

In the following method, the function

 def add_active_residents(self): ssa_res = SSA_Resident.objects.select_related(depth=1).filter(ssa=self.ssa, active=True) for r in ssa_res: self.residents.add(r.resident) # Fails Here self.save() 

Why is there select_related? You really only need the FK ssa_res elements. Why do we need additional queries for related items?

+4
source

Are you using Python 2.6?

If so, this is a (apparently) known issue that can be mitigated by adding:

 DATABASE_OPTIONS = {'timeout': 30} 

to your settings.py

See http://code.djangoproject.com/ticket/9409

+2
source

I understand that only write operations will result in the db-locked condition. http://www.sqlite.org/lockingv3.html

It's hard to say what the problem is, not knowing how django handles sqlite internally.

Speaking about using sqlite with standard cgi, I noticed that in some cases it may take a long time to release the lock. You might want to increase the timeout value mentioned by Matthew Christensen.

+2
source

It looks like you are actually running a multi-threaded application, despite what you say. I don’t know much about Django, but I would suggest that although it can be single-threaded, any debug server or production server on which you run the application will not be "essentially single-threaded."

+1
source

All Articles