Concurrency issue? Standalone python script sharing settings.py and ORM with django server has unreliable view of DB objects?

63 views Asked by At

I'm having a strange problem that is difficult to reproduce (everything worked 2 days ago but some time between then and now no longer does--with no changes in the interim!)

I have a django server program which we are running via gunicorn with multiple worker subprocesses and a separate small REST webservice which shares the settings.py of the server program and acts on the same DB objects. The code for this server program is roughly as follows:

# my app's models.py
class TestConfig(models.Model):
    ## various attributes
class Test(models.Model):
    ## some attributes
    def startTest(self):
        return TestExecution.objects.create(test=self)
class TestExecution(models.Model):
    test = models.ForeignKey(
    Test,
    on_delete=models.CASCADE
    )
    config = models.ForeignKey(
    TestConfig,
    on_delete=models.CASCADE,
    null=True
    )

# excerpt from a post() method in my app's views.py
    test = Test.objects.get(test_id)
    if config_form.is_valid():
        config = config_form.save()
        config_id = config.id
        test_exe = test.startTest()
        test_exe.config = config
        test_exe.save()

        webservice_response = requests.get(
            'http://{}:{}/rest/add_to_queue/{}'.format(
                webservice_ip, webservice_port, test_exe.id))

The other program (small REST webservice) sharing the same settings.py as the django server program looks as follows:

os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
import django
django.setup()

# the REST endpoint referenced from the django server program
@app.route('/rest/add_to_queue/<test_exe_object_id>/')
@app.route('/rest/add_to_queue/<test_exe_object_id>')
def add_to_queue(test_exe_object_id):
from myapp.models import TestExecution
try:
    exe_object = TestExecution.objects.get(pk=int(test_exe_object_id))

# for completeness the database section of my settings.py:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test_db',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        },
    }
}

As I mentioned, this was all working fine a few days ago, then when I tried again today I was getting a DoesNotExist in the second program when trying to "get()" the TestExecution object using its 'id'.

0

There are 0 answers