Database errors in Django when using threading

前端 未结 3 1276
小蘑菇
小蘑菇 2021-02-07 11:11

I am working in a Django web application which needs to query a PostgreSQL database. When implementing concurrency using Python threading interface, I am getting DoesNotEx

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-07 12:00

    Try using TransactionTestCase:

    class ThreadingTest(TransactionTestCase):
    

    TestCase keeps data in memory and doesn't issue a COMMIT to database. Probably the threads are trying to connect directly to DB, while the data is not commited there yet. Seedescription here: https://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.TransactionTestCase

    TransactionTestCase and TestCase are identical except for the manner in which the database is reset to a known state and the ability for test code to test the effects of commit and rollback. A TransactionTestCase resets the database before the test runs by truncating all tables and reloading initial data. A TransactionTestCase may call commit and rollback and observe the effects of these calls on the database.

提交回复
热议问题