• 单例模式-4中模式


    基于类实现

    1,基于类实现

    import threading
    import time
    
    class Sigle(object):
        lock=threading.Lock()
        def __init__(self):
            pass
        @classmethod
        def get_sigle(cls,*args,**kwargs):
            if not hasattr(Sigle,'instance'):
                with Sigle.lock:        #加上线程锁,一次性只能进一次  如果只是单线程则不需要
                    if not hasattr(Sigle,'instance'):
                        Sigle.instance=Sigle(*args,**kwargs)
            return Sigle.instance
    
    def func():    
        l=Sigle.get_sigle()
        print(l)
    for i in range(10):
        t=threading.Thread(target=func,)    #多线程一起进入
        t.start()

    2,类基于__new__方法实现,在实例化之前创建一个对象

    import time
    import threading
    class Singleton(object):
        _instance_lock = threading.Lock()
        def __init__(self):
            pass
        def __new__(cls, *args, **kwargs):
            if not hasattr(Singleton, "_instance"):
                with Singleton._instance_lock:
                    if not hasattr(Singleton, "_instance"):
                        Singleton._instance = object.__new__(cls, *args, **kwargs)
            return Singleton._instance
    
    def task(arg):
        obj = Singleton()
        print(obj)
    
    for i in range(10):
        t = threading.Thread(target=task,args=[i,])
        t.start()

    3,类基于metaclass创建单例模式

    import threading
    class Sigle(type):
        lock=threading.Lock()
        def __call__(cls, *args, **kwargs):
            if not hasattr(cls,'instence'):
                with Sigle.lock:
                    if not hasattr(cls,'instence'):
                        cls.instence=super(Sigle,cls).__call__(*args,**kwargs)
            return cls.instence
    class Foo(metaclass=Sigle):  #metaclass 指定该类的元类,也就是以后他继承是Sigle类,而不是object.
        def __init__(self,name):
            self.name=name
    obj=Foo('yuan')

    4,数据库连接池单例模式

    创建数据连接池

    from DBUtils.PooledDB import PooledDB
    import pymysql
    import threading
    
    class Sigle(object):
        lock=threading.Lock()
        def __init__(self):
            self.pool=PooledDB(
                creator=pymysql,  # 使用链接数据库的模块
                maxconnections=6,  # 连接池允许的最大连接数,0和None表示不限制连接数
                mincached=2,  # 初始化时,链接池中至少创建的空闲的链接,0表示不创建
    
                maxcached=5,  # 链接池中最多闲置的链接,0和None不限制
                maxshared=3,
                # 链接池中最多共享的链接数量,0和None表示全部共享。PS: 无用,因为pymysql和MySQLdb等模块的 threadsafety都为1,所有值无论设置为多少,_maxcached永远为0,所以永远是所有链接都共享。
                blocking=True,  # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错
                maxusage=None,  # 一个链接最多被重复使用的次数,None表示无限制
                setsession=[],  # 开始会话前执行的命令列表。如:["set datestyle to ...", "set time zone ..."]
                ping=0,
                # ping MySQL服务端,检查是否服务可用。# 如:0 = None = never, 1 = default = whenever it is requested, 2 = when a cursor is created, 4 = when a query is executed, 7 = always
                host='127.0.0.1',
                port=3306,
                user='root',
                password='',
                database='flask',
                charset='utf8'
            )
    
        def __new__(cls, *args, **kwargs):
            if not hasattr(Sigle,'instance'):
                with Sigle.lock:
                    if not hasattr(Sigle,'instance'):
                        Sigle.instance=object.__new__(cls,*args,**kwargs)
            return Sigle.instance
    
        def connect(self):
            return self.pool.connection()

    调用数据连接池

    import pool
    import pymysql
    def run():
        poo=pool.Sigle()
        conn=poo.connect()
        cursor=conn.cursor()
        cursor.execute('select * from user')
        cur_list=cursor.fetchall()
        print(cur_list)
        conn.close()
    if __name__ == '__main__':
        run()
  • 相关阅读:
    Spring Session设计思路
    Jinja2 is a full-featured template engine for Python
    Computer Science:《The Essence of Winning and Losing》
    开源 敏捷 devops Lean Startup Huawei CRM
    BRMS ILOG
    日志易 IDS Snort 入侵检测
    理解和了解别人的正确姿势
    谷歌趋势 Google Trends
    OpenZipkin · A distributed tracing system
    Arthas OAM
  • 原文地址:https://www.cnblogs.com/52forjie/p/8252181.html
Copyright © 2020-2023  润新知