我有2个模型:Product
和Order
。
Product
有一个整数字段用于库存,而Order
具有状态和Product
的外键:
class Product(models.Model):
name = models.CharField(max_length=30)
stock = models.PositiveSmallIntegerField(default=1)
class Order(models.Model):
product = models.ForeignKey('Product')
DRAFT = 'DR'; INPROGRESS = 'PR'; ABORTED = 'AB'
STATUS = ((INPROGRESS, 'In progress'),(ABORTED, 'Aborted'),)
status = models.CharField(max_length = 2, choices = STATUS, default = DRAFT)
我的目标是让每个新订单的产品库存减少一个,并且每个订单取消时增加一个。为此,我已经超载了save
模型的Order
方法(受Django: When saving, how can you check if a field has changed?启发):
from django.db.models import F
class Order(models.Model):
product = models.ForeignKey('Product')
status = models.CharField(max_length = 2, choices = STATUS, default = DRAFT)
EXISTING_STATUS = set([INPROGRESS])
__original_status = None
def __init__(self, *args, **kwargs):
super(Order, self).__init__(*args, **kwargs)
self.__original_status = self.status
def save(self, *args, **kwargs):
old_status = self.__original_status
new_status = self.status
has_changed_status = old_status != new_status
if has_changed_status:
product = self.product
if not old_status in Order.EXISTING_STATUS and new_status in Order.EXISTING_STATUS:
product.stock = F('stock') - 1
product.save(update_fields=['stock'])
elif old_status in Order.EXISTING_STATUS and not new_status in Order.EXISTING_STATUS:
product.stock = F('stock') + 1
product.save(update_fields=['stock'])
super(Order, self).save(*args, **kwargs)
self.__original_status = self.status
使用RestFramework,我创建了2个视图,一个用于创建新订单,一个用于取消现有订单。两者都使用简单的序列化器:
class OrderSimpleSerializer(serializers.ModelSerializer):
class Meta:
model = Order
fields = (
'id',
'product',
'status',
)
read_only_fields = (
'status',
)
class OrderList(generics.ListCreateAPIView):
model = Order
serializer_class = OrderSimpleSerializer
def pre_save(self, obj):
super(OrderList,self).pre_save(obj)
product = obj.product
if not product.stock > 0:
raise ConflictWithAnotherRequest("Product is not available anymore.")
obj.status = Order.INPROGRESS
class OrderAbort(generics.RetrieveUpdateAPIView):
model = Order
serializer_class = OrderSimpleSerializer
def pre_save(self, obj):
obj.status = Order.ABORTED
以下是访问这两个视图的方法:
from myapp.views import *
urlpatterns = patterns('',
url(r'^order/$', OrderList.as_view(), name='order-list'),
url(r'^order/(?P<pk>[0-9]+)/abort/$', OrderAbort.as_view(), name='order-abort'),
)
我正在使用Django 1.6b4,Python 3.3,Rest Framework 2.7.3和PostgreSQL 9.2。
以下是我用来演示的脚本:
import sys
import urllib.request
import urllib.parse
import json
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor)
def create_order():
url = 'http://127.0.0.1:8000/order/'
values = {'product':1}
data = urllib.parse.urlencode(values).encode('utf-8')
request = urllib.request.Request(url, data)
response = opener.open(request)
return response
def cancel_order(order_id):
abort_url = 'http://127.0.0.1:8000/order/{}/abort/'.format(order_id)
values = {'product':1,'_method':'PUT'}
data = urllib.parse.urlencode(values).encode('utf-8')
request = urllib.request.Request(abort_url, data)
try:
response = opener.open(request)
except Exception as e:
if (e.code != 403):
print(e)
else:
print(response.getcode())
def main():
response = create_order()
print(response.getcode())
data = response.read().decode('utf-8')
order_id = json.loads(data)['id']
time.sleep(1)
for i in range(2):
p = Process(target=cancel_order, args=[order_id])
p.start()
if __name__ == '__main__':
main()
对于库存为1的产品,此脚本提供以下输出:
201 # means it creates an order for Product, thus decreasing stock from 1 to 0
200 # means it cancels the order for Product, thus increasing stock from 0 to 1
200 # means it cancels the order for Product, thus increasing stock from 1 to 2 (shouldn't happen)
我添加了一个示例项目来重现该错误: https://github.com/ThinkerR/django-concurrency-demo
答案 0 :(得分:3)
答案 1 :(得分:2)
我认为问题不是原子地更新产品计数 - Django ORM的F()
表达式应该正确处理。但是合并后的操作:
不是原子操作。两个线程A和B可以有以下事件序列(两个线程都处理相同订单的取消请求):
A:检查订单状态:新取消,与前一个不同 B:检查订单状态:新取消,与前一个不同 答:原子地将产品数从0更新为1 B:原子地将产品计数从1更新为2 答:将订单状态更新为已取消 B:将订单状态更新为已取消的
您需要做的是以下之一:
F
或类似的原语来执行此操作。 (主要思想是更改订单和产品数据更新的顺序,以便您首先更新并自动测试订单状态。) 总结:除非您已经为您的应用程序使用HTTP级事务,否则请尝试在Django配置文件(ATOMIC_REQUESTS = True
)中设置settings.py
。
如果您不这样做或不能这样做,请注意替代方法不会为您提供订单 - 产品对的一致性。试着想一想如果Django服务器在产品更新和订单之间崩溃会发生什么 - 只会更新一个。 (这是数据库事务必须由数据库引擎注意到客户端中止的原因 - 由于网络连接断开 - 并回滚事务。)
答案 2 :(得分:0)
正如您所提到的,您在并发请求中遇到了竞争条件。要摆脱这一点,你应该使操作成为原子。我要做的是使用Redis使订单操作成为原子。然后在我可以的时候写回常规数据库。
修改强>
经过一些评论后,似乎最好的办法是加入select_for_update(wait=True)