Hi,

This might be a gotcha on the models side, but I would like
clarification and guidance on how to write the code better concerning
multiple process accessing the same data via models.

I have a backend script that runs the following code in multiple
processes:

1 object = MyModel.object.get(id=1)
2 print object.value    # starting value is 5
3 while object.fail_to_get_lock():
4    sleep(5)
5 object = MyModel.object.get(id=1)  # Re-get the object so we can
have the latest state
6 object.value = object.value - 1
7 print object.value    # returns 4
8 object.release_lock()

If the above code fails to get the lock because another process is
running the code, it goes to sleep until the other process finishes.
The other process will also be decrementing object.value, so if we
have 2 processes running the above script at the same time, I would
expect the later process to return line 7 as 3 and not 4.

As a result, if 2 processes runs the above code at the same time,
instead of getting a final value for object.value of 3, I get a 4.
db is MySQL with innodb engine and auto_commit on.

Is there some sort of caching going on in the models? How would I
write the code so that it will always see the latest state of data in
the db?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
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.

Reply via email to