Django reproduce concurrency example with select_for_update()

瘦欲@ 提交于 2021-01-27 19:20:36

问题


I'm reading over this article on handling concurrency in django

The post talk about the following example:

  • User A fetches the account — balance is 100$.
  • User B fetches the account — balance is 100$.
  • User B withdraws 30$ — balance is updated to 100$ — 30$ = 70$.
  • User A deposits 50$ — balance is updated to 100$ + 50$ = 150$.

I tried to reproduce with a UserBalance model with balance column that use IntegerField(), as the article said i need to use select_for_update() to avoid this issue

def test_con(id):
    with transaction.atomic():
        user_balance = UserBalance.objects.select_for_update().get(pk=int(id))
        sleep(60)
        user_balance.balance -= 30
        user_balance.save()

So this transaction is lock for 60 seconds

In the first shell i go into manage.py shell to call it

In another shell after i execute test_con(5) i ran the following :

>>> user_balance = UserBalance.objects.get(pk=5)
>>> user_balance.balance += 50
>>> user_balance.save()
>>> print(str(user_balance.balance))

the second shell wait till the first shell test_con(5) transaction is completed(as expected) and return the balance but it return 150 ? shouldn't it be 120 ?

I'm confused about why this locking mechanic doesn't work


回答1:


After testing some more, thank to @CharanjitSingh for the suggestion of using F expression.

I decided to use it within my transaction, also create another function to reproduce a transaction happen at the same time (instead of using second shell and run pure ORM update):

def test_con(id):
    with transaction.atomic():
        user_balance = UserBalance.objects.select_for_update().get(pk=int(id))
        sleep(20)
        user_balance.balance = F('balance') - 30
        user_balance.save()

def run_con(id):
    with transaction.atomic():
        user_balance = UserBalance.objects.select_for_update().get(pk=int(id))
        user_balance.balance = F('balance') + 50
        user_balance.save()

I ran test_con(5) first and then run_con(5) on second shell, after 20 seconds i check my database and the calculation is now correct and it return 120 :)



来源:https://stackoverflow.com/questions/62211732/django-reproduce-concurrency-example-with-select-for-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!