After a bit more of searching I've finally figured out what I wanted
is "atomic updates".

Using the F() expression seems to be the best solution without much
code overhaul:
http://stackoverflow.com/questions/280075/atomic-operations-in-django
http://efreedom.com/Question/1-1598932/Atomic-Increment-Counter-Django

Thanks JeeyoungKim for pointing to the right direction.

On 11月23日, 午後5:52, Iqbal Abdullah <iq...@marimore.co.jp> wrote:
> Since we only run the application on a single server we're using file
> based locking; fail_to_get_lock() and release_lock() tries to create a
> file in the tmp directory with the user's unique identification data,
> and release_lock() deletes it.
>
> What I don't understand is, even if the save() method is not thread
> safe, am I correct to assume that everytime I call something like
> object = MyModel.object.get(id=1) I will get the latest data for
> object?
>
> On 11月20日, 午後2:18, JeeyoungKim <jeeyou...@gmail.com> wrote:
>
> > I'm wondering, how are fail_to_get_lock and release_lock implemented?
> > It would be some kind of multiprocess lock, which seems overly
> > complicated..
>
> > using save() in this case is cumbersome, because save() isnot
> > threadsafe... you'll have to try to maintain your own lock.
>
> > If you only want to implement increments and decrements, you can use
> > F() object and update() methods to update the database.
>
> > I have an example, here
>
> >http://pastebin.com/qp4ExWC2
>
> > On Nov 19, 4:30 pm, Iqbal Abdullah <iq...@marimore.co.jp> wrote:
>
> > > Hi Steve,
>
> > > Ops, yes, I forgot to include the object.save() in the pseudo code
> > > above. Yes, we're actually saving it:
>
> > > 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 object.save()
> > > 8 print object.value    # returns 4
> > > 9 object.release_lock()
>
> > > On 11月20日, 午前7:39, Steve Holden <holden...@gmail.com> wrote:
>
> > > > On 11/19/2010 5:35 PM, Iqbal Abdullah wrote:
>
> > > > > Hi,
>
> > > > > This might be a gotcha on themodelsside, but I would like
> > > > > clarification and guidance on how to write the code better concerning
> > > > >multipleprocess accessing the same data viamodels.
>
> > > > > I have a backend script that runs the following code inmultiple
> > > > >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 2processesrunning the above script at the same time, I would
> > > > > expect the later process to return line 7 as 3 andnot4.
>
> > > > Shouldn't some saving occur for that to be true?
>
> > > > regards
> > > >  Steve
> > > > --
> > > > DjangoCon US 2010 September 7-9http://djangocon.us/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to