Database errors in Django when using threading

前端 未结 3 1277
小蘑菇
小蘑菇 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 11:50

    This sounds like it's an issue with transactions. If you're creating elements within the current request (or test), they're almost certainly in an uncommitted transaction that isn't accessible from the separate connection in the other thread. You probably need to manage your transctions manually to get this to work.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-07 12:07

    Becomes more clear from this part of the documentation

    class LiveServerTestCase(TransactionTestCase):
        """
        ...
        Note that it inherits from TransactionTestCase instead of TestCase because
        the threads do not share the same transactions (unless if using in-memory
        sqlite) and each thread needs to commit all their transactions so that the
        other thread can see the changes.
        """
    

    Now, the transaction has not been committed inside a TestCase, hence the changes are not visible to the other thread.

    0 讨论(0)
提交回复
热议问题