最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin9PV0评论

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?

Post a comment

comment list (0)

  1. No comments so far