i think you need transactions?

problem being that if all those processes happen at about the same
time, they will all see enough money, then they will all process it.

On Jul 1, 5:12 am, pength <[EMAIL PROTECTED]> wrote:
> sorry for the bad indent of the "#models.py" part. I post it again,
> hope this time that part will be shown OK.
>
> #models.py
> class Apple_For_Sale(models.Model):
>         price = models.PositiveSmallIntegerField()
>         has_been_sold = models.BooleanField(default=False)
>
> class People(models.Model):
>         money = models.PositiveSmallIntegerField()
>
>         def buy_apple(self,apple): #apple should be an instance of
> Apple_For_Sale
>                 if self.money >= apple.price and not apple.has_been_sold:
>                         self.money -= apple.price
>                         self.save()
>                         apple.has_been_sold = True
>                         apple.save()
>                         Sale_Records.objects.create(apple=apple,people=self)
>
> class Sale_Records(models.Model): #create one record when one people
> buy
>     apple = models.ForeignKey(Apple_For_Sale)
>     people = models.ForeignKey(People)
>     dealt_date = models.DateTimeField(auto_now_add=True)
>
> On Jul 1, 4:53 pm, pength <[EMAIL PROTECTED]> wrote:
>
> > I am using Django0.96/apache2/mod_python(running prefork MPM) / mysql,
> > for my Django project, a typical Django environment, I think. And
> > usually there're more than 10 apache2 processes running on my site, as
> > the online users is about 100+.
>
> > I found a problem that sometimes if one user clicks a link several
> > times very fast (means several identical requests are sent to the
> > server), some error will happen. Although in my codes, I am tring to
> > prevent such thing. Let me make a simple example to illustrate my
> > problem.
>
> > #models.py
> > class Apple_For_Sale(models.Model):
> >         price = models.PositiveSmallIntegerField()
> >         has_been_sold = models.BooleanField(default=False)
>
> > class People(models.Model):
> >         money = models.PositiveSmallIntegerField()
>
> >         def buy_apple(self,apple): #apple should be an instance of
> > Apple_For_Sale
> >                 if self.money >= apple.price and not apple.has_been_sold:
> >                         self.money -= apple.price
> >                         self.save()
> >                         apple.has_been_sold = True
> >                         apple.save()
>
> > Sale_Records.objects.create(apple=apple,people=self)
>
> > class Sale_Records(models.Model): #create one record when one people
> > buy
> >     apple = models.ForeignKey(Apple_For_Sale)
> >     people = models.ForeignKey(People)
> >     dealt_date = models.DateTimeField(auto_now_add=True)
>
> > #views.py
> > def people_buy_apple(request,aid):
> >         "aid is the id of an apple_for_sale
> >         a = get_object_or_404(Apple_For_Sale, pk=aid)
> >         p = People.object.get(xxx) #....get people...omitted here
> >         p.buy_apple(a)
> >         return ....
>
> > The logic is simple. If the people has enough money and the apples
> > have not already been sold, then the "buy apple" will be successful.
>
> > And in my site, I think 99% of such things happened correctly. But,
> > very rarely, when one user clicks a link of "buy apple" several times
> > fast, there'll be more than 1 records of Sale_Records created, for
> > same "apple" and "people", and the people's money was minused several
> > times for one identical apple. And if the network is laggy, it seems
> > the frequency of such error occurs becomes higher.
>
> > For my understanding, when the several "identical requests" are sent
> > to my server, the requests are dispatched to different apache2
> > processes, and nearly the same time, each process "think" the "buy
> > apple" can happen successfully. so....the error comes out.
>
> > In my actual site,  I have added some "unqiue = True" to avoid this
> > kind of error, in DB level. For example:
> > class Sale_Records(models.Model):
> >     apple = models.ForeignKey(Apple_For_Sale, unique=True)
>
> > But really I doubt this is the right way to solve my problem.
>
> > Can anyone help? Can I avoid such error by using "mod_wsgi in daemon
> > mode with only 1
> > thread " (ref. to 
> > :http://groups.google.com/group/django-users/browse_thread/thread/5bd5...)
>
> > Thanks a lot!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to