XENONnT / utilix

Package for XENON members to easily interface with RunDB, Midway batch queue, maybe Rucio (others?).
0 stars 6 forks source link

Use singleton pattern to only initialize `DB` once #122

Closed dachengx closed 1 month ago

dachengx commented 1 month ago

Similar to https://github.com/XENONnT/straxen/pull/1426.

The motivation is to limit the number of connections to DB. With singleton pattern:

_instances: Dict[Tuple, "DB"] = {}
_initialized: Dict[Tuple, bool] = {}

def __new__(cls, *args, **kwargs):
    key = (args, frozenset(kwargs.items()))
    if key not in cls._instances:
        cls._instances[key] = super(DB, cls).__new__(cls)
        cls._initialized[key] = False
    return cls._instances[key]

def __init__(self, *args, **kwargs):
    key = (args, frozenset(kwargs.items()))
    if not self._initialized[key]:
        self._instances[key].initialize(*args, **kwargs)
        self._initialized[key] = True
    return

def initialize(self,

the instance will be only one given the same *args, **kwargs.

Also, this PR reorganizes logger by only retaining one handler, otherwise, you will see multiple messages with the same content.

yuema137 commented 1 month ago

Hi, @dachengx. This is great! I have one concern regarding this update—with such an implementation, all different DB instances can only act like a global variable throughout the whole scope and lifetime of any script. I understand that this is usually the requirement for data processing, but I'm wondering if we are sure we have zero need to keep separate DBs? Probably also a question for @napoliion

dachengx commented 1 month ago

@yuema137 I think you mean switching DB.

dachengx commented 1 month ago

Sorry, let me first merge it for faster iteration. Please @yuema137 and @napoliion still comment about your opinion on this PR. If a problem happens I will make patches.