Django unit test with parallel db accesses giving database locked error
I am writing some Django unit tests to validate the behavior of my application when multiple clients access it at the same time. I have done this by writing tests like this:
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())
When I run this unit test I get an immediate error:
django.db.utils.OperationalError: database table is locked: person
I tried changing the timeout in the settings.py file but that 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?