Sorry, this code is supposed to be while ModelLock.objects.filter(name=name).filter(isLocked=True).count()>0: pass lock = ModelLock.objects.filter(name=name)[0] lock.isLocked=True lock.save()
On Jan 22, 1:48 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > > Or you could possibly have a lock management table/model in your database. > > I had that idea. > > I could have: > class ModelLock(models.Model): > name = models.CharField(max_length=100) > isLocked = models.BooleanField() > > This is an ok solution -- pretty much the only one i have. But there > is a HUGE problem with this. > > Here is how the locking might work (assuming for each row i'd like to > lock, there is a ModelLock) > > while ModelLock.objects.filter(name=name).filter(isLocked=True): > pass > lock = ModelLock.objects.filter(name=name)[0] > lock.isLocked=True > lock.save() > > The huge problem: ModelLock is not a proper mutex. The calls to > isLocked=True & save() are NOT atomic. > > I've heard about inter-process mutexes, but that wouldn't even solve > the problem you bring up of requests on different servers. > > A memcached lock would probably be better because at least setting the > lock would be faster than setting it in a database table. > > Ivan > > On Jan 22, 12:27 pm, Rajesh Dhawan <[EMAIL PROTECTED]> wrote: > > > Hi, > > > > Let's say I have a model with an integer value. I'd like to increment > > > that value. > > > > Multiple processes would like to increment that value. > > > > Does the transaction middleware properly protect against this? > > > > Example: > > > Processes P1, P2, model instance m, with m.val > > > > - P1 grabs the object : m = MyModel.objects.filter(get the m i want) > > > [0] > > > - P2 grabs the object : m = MyModel.objects.filter(get the m i want) > > > [0] > > > - P1 m.val += 1 > > > - P2 m.val += 1 > > > - P1 m.save() > > > - P2 m.save() > > > > What happens normally? What happens with the transaction middleware? > > > The transaction middleware can't do anything special in these > > situations. The instances of 'm' in processes P1 and P2 are two > > separate Python object instances. If P2's m.save() gets called after > > P1's m.save(), then m.val from P2's instance will get saved in the > > database regardless of the m.val set by P1. The two instances are not > > connected to each other. > > > > What happens if the view is more complicated and only conditionally > > > accesses certain models. Will all models actually accessed be properly > > > locked? > > > No. The model instances are not locked like this at all. > > > You will need a global shared memory/storage area to simulate an > > interprocess lock of your own. Realize that your processes could even > > be on different web servers. For example, you could use a "slot" in > > memcache to acquire and release a custom lock every time around the > > statements where you increment m.val. Or you could possibly have a > > lock management table/model in your database. > > > -Rajesh D --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---