Let’s suppose I have a Django app named MyApp
in which I have a Django model called MyModel
. This model has a method called process
. The database is Postgresql so it allows concurrent queries.
What I need to do is to test if the MyModel.process()
method can run simultaneously without problem.
So, in my tests, when I do
class test_smtg(TestCase):
def setup(self):
self.my_model = MyModel()
self.my_model.save()
def test_single_processing(self):
self.my_model.process()
It processes successfully. However, when I do this instead:
import threading
class test_smtg(TestCase):
def setup(self):
self.my_model = MyModel()
self.my_model.save()
def test_concurrent_processing(self):
thread = threading.Thread(target=self.my_model.process)
thread.start()
It immediately reports me an error, such that the ID of my_model
cannot be found in table myapp_mymodel
. Why is that? Why is the program not able to see the object in the database while running on a separated thread? And what can I do to still test if the concurrency works whitout crashing my tests?
1