Y
Y
YuriyCherniy2021-09-08 22:45:04
Django
YuriyCherniy, 2021-09-08 22:45:04

How to fix Singleton issue when running Django app running under Gunicorn with more than one worker?

Greetings. In a Django project, I wrote a simple guest post counter using the Singleton design pattern. Everything worked fine on the dev server. But as soon as I ran the code on a remote server with Gunicorn, problems began. It turned out that each worker creates a new instance of the class, which naturally breaks all the work of the counter, which should work with a single instance. What are the ways to solve this problem. I do not want to write counter data to the database. This is not very important information to contact the database every time the page is loaded. The counter is end-to-end and shows data in the site's navbar all the time. I wonder if it is possible to fix this at all, or do they not work with Singleton in Django at all?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-09-08
@YuriyCherniy

In today's world, it's a sin to write stateful services.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question