Skip to content

Commit

Permalink
fix: add singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
Undertone0809 committed Jun 1, 2023
1 parent a42579f commit 6eef0e6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion broadcast_service/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import logging
from typing import Optional, List, Callable
from concurrent.futures import ThreadPoolExecutor
from broadcast_service.singleton import Singleton

__all__ = ['broadcast_service', 'BroadcastService', 'enable_log']

Expand All @@ -23,7 +24,7 @@ def enable_log():
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')


class BroadcastService:
class BroadcastService(metaclass=Singleton):
"""
This class implements broadcast mode, you can import the instance by single class.
By BroadcastService, you can send topic message,it will automatically execute the
Expand Down
24 changes: 24 additions & 0 deletions broadcast_service/singleton.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""The singleton metaclass for ensuring only one instance of a class."""
import abc


class Singleton(abc.ABCMeta, type):
"""
Singleton metaclass for ensuring only one instance of a class.
"""

_instances = {}

def __call__(cls, *args, **kwargs):
"""Call method for the singleton metaclass."""
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]


class AbstractSingleton(abc.ABC, metaclass=Singleton):
"""
Abstract singleton class for ensuring only one instance of a class.
"""

pass
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

setuptools.setup(
name="broadcast_service",
version="1.3.0",
version="1.3.1",
author="Zeeland",
author_email="[email protected]",
description="A lightweight third-party broadcast/pubsub library",
Expand Down

0 comments on commit 6eef0e6

Please sign in to comment.