2017-02-14 61 views
2

我正在创建一个包含与服务器的套接字通信的Python应用程序。我想有一个模块可以用于整个应用程序(其他几个模块)。目前我模块是这样的:Python中的类的单个实例

class SocketCommunication: 

    def __init__(self): 
     self.socketIO = SocketIO(settings.ADDRESS, settings.PORT, Namespace) 

    def emit(self, message, data): 
     json_data = json.dumps(data.__dict__) 
     self.socketIO.emit(message, json_data) 


class Namespace(BaseNamespace): 
    def on_connect(self): 
     print '[Connected]' 

    def on_disconnect(self): 
     print "[Disconnected]" 

当我使用这个在其他模块中我做了以下内容:

import SocketCommunication 
self.sc = SocketCommunication() 

的问题是,每次我这样做的时候,创建一个新的连接,将为在服务器上显示为新客户端,这是不可取的。 从我能读的东西中,应该避免使用Python中的单例,因此我很好奇这种类型的问题的最佳做法是什么?

+2

阅读关于** _ Borg Design Pattern _ ** [这里](http://code.activestate.com/recipes/577870-singleton-we-dont-need-no-stinkin-singleton-the-bo/ )和[这里](http://code.activestate.com/recipes/66531/)提供了一种将类转换为单例的简单方法。还有其他方法。 – martineau

+1

我会使用这个类作为一个单例。我从来没有遇到过这种方法的问题。 – Alfe

+0

您说您在其他模块中执行的'import SocketCommunication'将不起作用,因为'SocketCommunication'是您的其中一个类的名称。它需要像'from my_module import SocketCommunication'那样才能使下一行有效。也就是说,在定义类之后,可以有效地使该类成为在my_module.py文件中添加'SocketCommunication = SocketCommunication()'的单例。这样一来,创建更多实例将非常困难,因为类名将被自身的实例覆盖。 – martineau

回答

1

以下是在Python中使用单例的三种方法。 使用metaclassdecorator来达到目标​​。

  1. 使用__new__

    class Singleton(object): 
        def __new__(cls, *args, **kw): 
         if not hasattr(cls, '_instance'): 
          orig = super(Singleton, cls) 
          cls._instance = orig.__new__(cls, *args, **kw) 
         return cls._instance 
    
    class MyClass(Singleton): 
        a = 1 
    
    one = MyClass() 
    two = MyClass() 
    
    two.a = 3 
    print one.a 
    #3 
    print id(one) 
    #29097904 
    print id(two) 
    #29097904 
    print one == two 
    #True 
    print one is two 
    #True 
    
  2. 使用__metaclass__

    class Singleton2(type): 
        def __init__(cls, name, bases, dict): 
         super(Singleton2, cls).__init__(name, bases, dict) 
         cls._instance = None 
    
    def __call__(cls, *args, **kw): 
        if cls._instance is None: 
         cls._instance = super(Singleton2, cls).__call__(*args, **kw) 
        return cls._instance 
    
    
    class MyClass2(object): 
        __metaclass__ = Singleton2 
    
    one = MyClass2() 
    two = MyClass2() 
    
    two.a = 3 
    print one.a 
    #3 
    print id(one) 
    #31495472 
    print id(two) 
    #31495472 
    print one == two 
    #True 
    print one is two 
    #True 
    
  3. 使用decorator

    def singleton(cls, *args, **kw): 
        instances = {} 
        def _singleton(): 
         if cls not in instances: 
          instances[cls] = cls(*args, **kw) 
         return instances[cls] 
        return _singleton 
    
    @singleton 
    class MyClass3(object): 
        a = 1 
        def __init__(self, x=0): 
         self.x = x 
    
    one = MyClass3() 
    two = MyClass3() 
    
    two.a = 3 
    print one.a 
    #3 
    print id(one) 
    #29660784 
    print id(two) 
    #29660784 
    print one == two 
    #True 
    print one is two 
    #True 
    one.x = 1 
    print one.x 
    #1 
    print two.x 
    #1 
    

我更喜欢使用decorator

0

单身人士是有争议的,因为他们经常被用来包装全局变量。这就是为什么有些人主张避免。全局测试使得测试更加困难,它们限制了访问控制,并且经常导致变量之间的强耦合。 (见http://wiki.c2.com/?GlobalVariablesAreBad更多细节,为什么全局变量一般是不好的做法)

在您的特定情况下,使用单很可能是合适的,因为你只是试图阻止SocketCommunication被初始化多次(有很好的理由),而而不是试图将其用作全球化国家的容器。

查看Are Singletons really that bad?What is so bad about singletons?关于单身人士的一些讨论。

+0

感谢您的回答。看起来在这种特殊情况下,当我只需要一个类的单个实例时,单例是最好的解决方案。 – user3170354