Skip to content

Instantly share code, notes, and snippets.

@daimon99
Created September 8, 2019 14:15
Show Gist options
  • Save daimon99/b632437d0566a6bcdcc0ffbf4bb55128 to your computer and use it in GitHub Desktop.
Save daimon99/b632437d0566a6bcdcc0ffbf4bb55128 to your computer and use it in GitHub Desktop.
python singleton
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
return cls._instances[cls]
import torch
import logging
import threading
from singleton import Singleton
log = logging.getLogger(__name__)
lock = threading.RLock()
class XxxService(metaclass=Singleton):
def __init__(self):
pass
def xxx(self):
with lock:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment