1

is it defining a singleton in the circuits is simple like this? I dont know the potential impact, are there any risks implement the singleton in this way?

class Singleton(Component):
__instance = None
def __new__(cls):
    if cls.__instance is None:
        cls.__instance = super(Singleton,cls).__new__(cls)
        cls.__instance.__initialized = False
    return cls.__instance

def __init__(self):      
    if(self.__initialized): return
    self.__initialized = True
    print ("INIT")
  • 1
    Original author of the circuits framework here. Instead of using the Singleton pattern (_which is fine btw_) you should really take advantage of the builtin `registered` and `started` events and the `findcmp()` utility. For example the Sockets components do this to ensure there is only ever one Poller registered and active in teh system and if there isn't it will automatically registered an appropriate one. See: https://github.com/circuits/circuits/blob/59b2a7be553fa788d06e7575b39a5eb2ec96f884/circuits/net/sockets.py#L126-L141 – James Mills Apr 08 '20 at 13:55
  • @JamesMills got it! althought the standard singlton pattern works, check the singleton instance at registered and started stage is more circuitnic! but build a singleton boolean flag into the component class would be more pythonic, isn't? – soloman_1988 Apr 08 '20 at 14:20
  • Either way would work :) -- Yes And @ "circuitnic" – James Mills Apr 09 '20 at 15:55

0 Answers0