Search code examples
djangoredispickleuvicorn

How to share memory using gunicorn+uvicorn workers or Daphne+supervisor


I have a Django web application that uses Daphne web server. I have a problem when running multiple instances of app using supervisor (that corresponds to running app with multiple uvicorn workers using gunicorn). I tried to solve it by sharing memory using Redis DB to save user instances so that every worker can access logged in users but then I came across the problem. There is threading inside of User class and I can't pickle _thread.lock object so I can't use Redis. If I separate threading from my User class, I don't profit from it at all because then worker has to do same job again, that is, make a thread, when I send request. Is there a workaround to this problem? I.e. using internal shared memory or something like that?


Solution

  • Okay, so all I had to do was to remove a threading from variable inside of class and put threading only inside of method. That way I have not _thread.lock object anymore and I can pickle an object to put it inside of shared memory.