> Also, the cases are much more complicated than val+=1. They are > multiple lines of python operations on models. > I just used that as an example to illustrate the problem. > > A lock is the real solution, but I have no idea how to implement it > properly.
If you go with the memcached idea I proposed above, you don't even need to manage multiple locks per row of data. That will keep things simple enough for you to have a first shot at implementing this. So, let's say you have one "global" lock i.e. key in memcached called mylock. Your multi-process synchronization would go like this: - Before you execute lines of code that need to be synchronized between processes, read mylock from memcached and set it to the current timestamp if it's null. In other words, acquire a lock. On the other hand, if mylock is not null, it means another synchronized block has claimed a lock. So, check periodically in a loop for that lock to get released back to null before this process proceeds. - Execute your synchronized lines of code including any model.save calls so that your work gets back to the DB. Do a transaction commit if you are manually managing your transactions. - Clear mylock from memcached. The advantage of putting a timestamp as the value of mylock is that you can detect stale locks i.e. the process that put in the lock died before releasing the lock. If you know that no lock should stay for more than 30 seconds, you will be in a position to automatically clear out stale locks. Of course, this is a very rudimentary locking pattern. It doesn't take into account any kind of queuing of pending requests for the lock. But it might be sufficient. If you get this to work, you could then consider a per instance key such as mylock-<ID> where <ID> is the model's PK. -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 -~----------~----~----~----~------~----~------~--~---