django - How do I get parallel access to retry on "database table is locked" error? - Stack Overflow

admin2025-04-26  2

My Django unit tests to validate the behavior of my application when multiple clients access it at the same time:

from django.test import TestCase
from django.db import connection
from django.db import transaction

class ApplicationTestCase(TestCase):

    def test_parallel(self):

        @transaction.atomic
        def local_provision(i):
            with connection.cursor() as cursor:
              # Some SQL commands that read and write the person table.
              # These commands take less than one second.
              
              return f"done with {i}"
            
        with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
            futures = [executor.submit(local_provision, i) for i in range(5)]
            for future in concurrent.futures.as_completed(futures):
                print(future.result())

I get:

django.db.utils.OperationalError: database table is locked: person

Changing the timeout in settings.py did not make any difference. Why isn't SQLite waiting until the file is unlocked and then retrying? How can I get SQLite to retry?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745602993a309418.html

最新回复(0)