[Django]-Share object between django users

1👍

Contrary to your assertion, Django does not reinitialize on every request. Actually, Django processes last for many requests, and anything defined at module level will be shared across all requests handled by that process. This can often be a cause of thread safety bugs, but in your case is exactly what you want.

Of course a web site normally runs several processes concurrently, and it is impossible to share objects between them. Also, it is up to your server to determine how many processes to spawn and when to recycle them. But one object instantiation per process is better than one per request for your use case.

1👍

You can use any fast key-value storages such as memcached or LevelDB to share objects between application instances. You can use JSON to serialize and deserialize these objects.

0👍

You no need any magic to do singleton-like object in python. Just write module, for example shared.py inside your django project. Put your dictionary initialization here and import it from anywhere.

Leave a comment