18đź‘Ť
It may feel like a hack to you, but to me it looks like a legitimate, reasonable implementation of the “optimistic concurrency” approach — try doing whatever, detect conflicts caused by race conditions, if one occurs, retry a bit later. Some databases systematically uses that instead of locking, and it can lead to much better performance except under systems under a lot of write-load (which are quite rare in real life).
I like it a lot because I see it as a general case of the Hopper Principle: “it’s easy to ask forgiveness than permission”, which applies widely in programming (especially but not exclusively in Python — the language Hopper is usually credited for is, after all, Cobol;-).
One improvement I’d recommend is to wait a random amount of time — avoid a “meta-race condition” where two processes try at the same time, both find conflicts, and both retry again at the same time, leading to “starvation”. time.sleep(random.uniform(0.1, 0.6))
or the like should suffice.
A more refined improvement is to lengthen the expected wait if more conflicts are met — this is what is known as “exponential backoff” in TCP/IP (you wouldn’t have to lengthen things exponentially, i.e. by a constant multiplier > 1 each time, of course, but that approach has nice mathematical properties). It’s only warranted to limit problems for very write-loaded systems (where multiple conflicts during attempted writes happen quite often) and it may likely not be worth it in your specific case.
0đź‘Ť
Add optional FOR UPDATE clause to QuerySets http://code.djangoproject.com/ticket/2705
- [Django]-Django logging of custom management commands
- [Django]-How do I match the question mark character in a Django URL?
- [Django]-How to pass multiple values for a single URL parameter?
0đź‘Ť
I use Shawn Chin’s solution and it proves very useful. The only change I did was to replace the
self.position = self.parent.item_count
with
self.position = self.parent.latest('position').position
just to make sure I am dealing with the latest position number (which in my case might not be item_count because of some reserved unused positions)
- [Django]-Django REST framework post array of objects
- [Django]-No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model
- [Django]-Django: list all reverse relations of a model