How to setup Gunicorn's workers to share data/state when using multiprocessing?

Viewed 567

I am running a Django instance through Gunicorn.

In this Django project, I have a view that starts a multiprocessing task in the background that is fetching some data every x milliseconds, and stops after x seconds if no new request has been received.

Using runserver, I have no problem, my multiprocessing continues to work as expected and my view fetches the data from this multiprocessing without any problem.

But when I switch to Gunicorn, it seems that when I request my view, it doesn't detect that the process is already running and tries to start a new one in parallel, returning wrong values.

Here is the code. my View is calling the NewReq function

import multiprocessing
manager=multiprocessing.Manager()
StateU3 = multiprocessing.Value('i', 0)
def NewReq(ID):
        if StateU3.value==0:
            p=multiprocessing.Process(target=BackgroundProcess)
            p.start()
        ...
        return (Values)

When the view is called by Gunicorn, it seems that the value StateU3 is not shared between the different requests, the way it is with runserver.

And here is my gunicorn.service file

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/home/project
ExecStart=/usr/local/bin/gunicorn --access-logfile - --workers 5 --bind         unix:/home/project/project.sock project.wsgi:application


[Install]
WantedBy=multi-user.target

Do you have any clues where the problem could come from? No. of workers? No. of threads?

0 Answers
Related