开发者

Which methods should be overridden to implement automatic locking and unlocking records in Django

How can I modify the behaviour of models in Django to perform automatic locking and unlocking records in database on selecting them and make this behaviour transparent for a programmer? I already know how to lock and unlock a record (database record locking) but we'd like to know where this code should be placed in Django models. I'd like it to work for every all(), filter(), exclude() and other queries, and unlocking should be invoked while save() and also in queries in which we don't save anything.

UPDATE: This application has several threads that can be run by two or more servers simultaneously and I want to ensure t开发者_JAVA技巧hat no records from a common database would be processed by more than one thread. Threads search for some records and then send some data through sockets and finally update these records. In other words, besides Django website there are servers.


You'll want to obtain the lock during the model __init__(); this can be implemented fairly simply through inheritance. The model's Manager will have a QuerySet instantiated for that model and your lock will be obtained as soon as the retrieved model is added to the query _result_cache[] or when the query's iterator() is called. Of course, you'll have to make sure that if the model doesn't have an associated pk yet that you forgo the lock contention.

If you don't want to do DB-level locking, you can look at Django-cachebot. It handles model invalidation, but the most important thing is that it can keep model records in a common store for your threads, so you can implement a model-locking state and have that pushed to the store and queried by other threads.

I also have this urge to give you big ups for an odd question. I wouldn't try this at home, so I sincerely hope you know what your doing!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜