0

I have a Django app that uses Daphne behind Nginx as an app server. I have first used Gunicorn + Uvicorn as an app server but I ran into a problem that I thought I could avoid by simply switching to Daphne. Unfortunately, when I switched to Daphne, the same problem remained.

Description of the problem: I have an app that has users. I save active users in an array. It's all good while I have only one Gunicorn+Uvicorn worker or one Daphne+Supervisor process. Problem starts when I introduce multiple workers/processes. I didn't know that each worker/process has its own memory, so when I log in I am logged in on only one of the workers. For example, let's say that I have 4 workers. I log in. First response in app is okay but all the other are random and the more workers I have, the less chance there is that I'll get a right answer. That is, chances are equal to 1/n where n is a number of workers/processes. That's because logged in user is saved in an array on only one of those workers memory.

So, I need some tips how to solve that problem and make workers/processes share that array with logged in users. I saw that there is a --preload flag in Gunicorn but it makes copy on write for workers, so if I understand it correctly, when I log in on the current worker, main process memory will copy itself only for that specific worker and I'll still be logged in only one instance of the app.

What I need is an array to which all processes have an access.

I am at the moment trying to make it work using multiprocessing module, but if someone has a nicer solution, I'd really appreciate it.

All advices are welcome! Thank you! :D

0 Answers0