Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing Django code in Gunicorn config file
    text
    copied!<p>I am converting a Django application from modWSGI to Gunicorn/NGINX. I have some django code that I was running from the modwsgi file to make sure it gets run on startup of the server. I'm trying to figure out how to get this to work on Gunicorn startup. </p> <p>Disclaimer - the code I'm wanting to access is a poor man's cache that processes data from the database and stores it in a format thats friendly for a lot of the things I do later on in the Django app. <strong>This question is not about the merits of this method compared to using a caching solution.</strong></p> <p>Here's the code I had in mod-WSGI that worked just fine:</p> <pre><code>sys.path.append('/src/accx/navigation') from navigation.views import rebuild_cache_all rebuild_cache_all() </code></pre> <p>Where 'accx' is the Django project, and 'navigation' is the Django app.</p> <p>Now, I'm attempting to do the same using Gunicorn.</p> <pre><code>def when_ready(server): from navigation.views import rebuild_cache_all rebuild_cache_all() </code></pre> <p>I've also tried appending to the sys.path in various places in this code, but no matter what I do, I get the following error:</p> <pre><code>ImportError: No module named navigation.views </code></pre> <p>Am I missing something obvious? Am I going about this in the wrong way? Any and all guidance is appreciated! </p> <p><strong>.............ANSWER.............</strong><br /> After typing out the above question I was perusing thru some other gunicorn questions that showed up as 'related' to my question, but hadn't shown up in my searches. Whilst doing so I figured out my issue.</p> <p><strong>Trailing slash!</strong> </p> <p>I did not use one when attempting to add to the sys.path in gunicorn. Once I fixed that, gunicorn started complaining that it lost its Django_settings_module, so I ended up having to supply that as well. </p> <p>Here's what I have that now works:</p> <pre><code>import sys,os if not '/src/accx-venv/' in sys.path: sys.path.insert(0,'/src/accx-venv/') os.environ["DJANGO_SETTINGS_MODULE"] = "accx.settings" def when_ready(server): if not '/src/accx-venv/accx/' in sys.path: sys.path.insert(0,'/src/accx-venv/accx/') from navigation.views import rebuild_cache_all rebuild_cache_all() </code></pre> <p>There's probably another way that is cleaner, but this got me working.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload