Skip to content

Commit 8af785b

Browse files
committed
add singleton design pattern
1 parent 525c8c9 commit 8af785b

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

app/utils.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
import logging
2-
from functools import lru_cache
1+
from threading import Lock
32

4-
from rich.console import Console
5-
from rich.logging import RichHandler
63

7-
console = Console(color_system="256", width=200, style="blue")
4+
class SingletonMeta(type):
5+
"""
6+
This is a thread-safe implementation of Singleton.
7+
"""
88

9+
_instances = {}
910

10-
@lru_cache
11-
def get_logger(module_name):
12-
logger = logging.getLogger(module_name)
13-
handler = RichHandler(
14-
rich_tracebacks=True, console=console, tracebacks_show_locals=True
15-
)
16-
handler.setFormatter(
17-
logging.Formatter("[ %(threadName)s:%(funcName)s:%(lineno)d ] - %(message)s")
18-
)
19-
logger.addHandler(handler)
20-
logger.setLevel(logging.DEBUG)
21-
return logger
11+
_lock: Lock = Lock()
12+
13+
def __call__(cls, *args, **kwargs):
14+
with cls._lock:
15+
if cls not in cls._instances:
16+
instance = super().__call__(*args, **kwargs)
17+
cls._instances[cls] = instance
18+
return cls._instances[cls]

0 commit comments

Comments
 (0)