From 58a78b7068d293c8ec1fa275a2495b486020927b Mon Sep 17 00:00:00 2001 From: Dacheng Xu Date: Wed, 10 Jul 2024 09:04:55 -0400 Subject: [PATCH] Generate only one instance for `MongoDownloader` (#1398) --- straxen/storage/mongo_storage.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/straxen/storage/mongo_storage.py b/straxen/storage/mongo_storage.py index 07024c975..5edd75c33 100644 --- a/straxen/storage/mongo_storage.py +++ b/straxen/storage/mongo_storage.py @@ -3,6 +3,7 @@ from datetime import datetime from warnings import warn import pytz +from typing import Tuple, Dict from strax import exporter, to_str_tuple import gridfs from tqdm import tqdm @@ -253,6 +254,15 @@ def upload_single(self, config, abs_path): class MongoDownloader(GridFsInterface): """Class to download files from GridFs.""" + _instances: Dict[Tuple, "MongoDownloader"] = {} + + def __new__(cls, *args, **kwargs): + key = (args, frozenset(kwargs.items())) + if key not in cls._instances: + cls._instances[key] = super(MongoDownloader, cls).__new__(cls) + cls._instances[key].__init__(*args, **kwargs) + return cls._instances[key] + def __init__(self, store_files_at=None, *args, **kwargs): super().__init__(*args, **kwargs)