Django - gunicorn - application level variable (shared between workers)

So I have a django + gunicorn project toy. I want to have a statistical model that is pretty heavily loaded into memory only once, and then reused in work / threads.

How / where can I define an application level variable? I tried installing it on settings.py as well as on wsgi.py

+5
source share
1 answer

I do not think you can (and should not). Workers are separate processes that fork before they run any of your codes.

You can put the β€œmodel” (what makes it big?) In the Redis database and access it for every employee. The best option is probably to create a separate service in which you run one instance and exchange data via HTTP or RPC from your working one (see nameko for a simple (micro) service.

Another option would be to use a single celery worker, as well as statistical calculations in the task.

0
source

All Articles