I have a module R that handles gets and sets to a redis cluster. It is imported all over a flask api's endpoints. My first thought was to use a Singleton class in R so that we maintain one single connection to the redis cluster, but I'm not entirely I should putting a singleton class pattern into a code base that is only looked at once a year by different developers, I really don't want someone trying to instantiate it multiple times at a later stage.
So, instead, in my module init.py I set up the connection to the cluster, and import this connection to my redis cluster module, then whereever I use R, the connection is always the same connection without having to use a singleton.
e.g.:
_init _.py:
try:
RedisConnection = ConnectionMaker(...)
R.py:
from ...caching import RedisConnection
...
def set_cache():
RedisConnection.set(....)
some_endpoint.py
from ....caching import set_cache, ...
some_other_endpoint.py
from ....caching import set_cache, ...
I think this is safe because 'Since Python modules are first-class runtime objects, they effectively become singletons, initialized at the time of first import.'. However, is there anything that I am missing, anything dangerous?