[Python] How to synchronize requested data in Django

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    Assuming I have requested test18 and requested test17

    Request test18 to execute first, request test17 to execute later. However, request test18 will complete the data update first, and request test17 to complete the data update later

    This will cause the result of request test17 to overwrite the result of test18 How can I ensure that the results of test17 do not overwrite the results of test 18

    Here is my django code

    def test17(request):
    obj = Book.objects.filter(id=1)[0]
    obj.book_name = 'history'
    obj.save()
    return HttpResponse("Success!")


    def test18(request):
    obj = Book.objects.filter(id=1)[0]
    obj.num = 2
    # Ensure that test17 is processed first
    time.sleep(2)
    obj.save()
    return HttpResponse("Success!")


    Source sql data: id:1 book_name match num:0

    SQL data after request id:1 book_name match num:2

    I hope the data from test18 will not overwrite the data from test17. During the data processing period of test18, test17 completed the data update. How should I handle this situation

    Continue reading...

Compartilhe esta Página