Flask框架单例模式实现方法详解

作者:lmw1239225096 时间:2023-01-24 17:04:55 

本文实例讲述了Flask框架单例模式实现方法。分享给大家供大家参考,具体如下:

单例模式:

程序运行时只能生成一个实例,避免对同一资源产生冲突的访问请求。

Django   admin.py下的admin.site.register() ,  site就是使用文件导入方式的单例模式

创建到单例模式4种方式:

  • 1.文件导入

  • 2. 类方式

  • 3.基于__new__方式实现

  • 4.基于metaclass方式实现

1.文件导入:

in  single.py


class Singleton():
 def __init__(self):
   pass
site = Singleton()

类似:

import time  第一次已经把导入的time模块,放入内存
import time  第二次内存已有就不导入了

in  app.py


from single.py import site #第一次导入,实例化site对象并放入内存

in  views.py


from single.py import site #第二次导入,直接从内存拿。

2.类方式:

缺点:改变了单例的创建方式


obj = Singleton.instance()


# 单例模式:无法支持多线程情况
import time
class Singleton(object):
 def __init__(self):
   import time
   time.sleep(1)
 @classmethod
 def instance(cls, *args, **kwargs):
   if not hasattr(Singleton, "_instance"):
     Singleton._instance = Singleton(*args, **kwargs)
   return Singleton._instance
# # 单例模式:支持多线程情况
import time
import threading
class Singleton(object):
 _instance_lock = threading.Lock()
 def __init__(self):
   time.sleep(1)
 @classmethod
 def instance(cls, *args, **kwargs):
   if not hasattr(Singleton, "_instance"):
     with Singleton._instance_lock:
       if not hasattr(Singleton, "_instance"):
         Singleton._instance = Singleton(*args, **kwargs)
   return Singleton._instance

3.基于__new__方式实现:

单例创建方式:


obj1 = Singleton()
obj2 = Singleton()


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

4.基于metaclass方式实现

基于metaclass方式实现的原理:

  • 1.对象是类创建,创建对象时候类的__init__方法自动执行,对象()执行类的 __call__ 方法

  • 2.类是type创建,创建类时候type的__init__方法自动执行,类() 执行type的 __call__方法

单例创建方式:


obj1 = Foo()
obj2 = Foo()


import threading
class SingletonType(type):
 _instance_lock = threading.Lock()
 def __call__(cls, *args, **kwargs):
   if not hasattr(cls, "_instance"):
     with SingletonType._instance_lock:
       if not hasattr(cls, "_instance"):
         cls._instance = super(SingletonType,cls).__call__(*args, **kwargs)
   return cls._instance
class Foo(metaclass=SingletonType):
 def __init__(self):
   pass

希望本文所述对大家基于flask框架的Python程序设计有所帮助。

来源:https://blog.csdn.net/lmw1239225096/article/details/79035320

标签:Flask,单例模式
0
投稿

猜你喜欢

  • JavaScript开发时的五个小提示

    2007-11-21 19:54:00
  • 使用python刷访问量的示例代码

    2023-11-09 12:55:30
  • Django用户认证系统 组与权限解析

    2022-05-06 06:10:22
  • Python正则表达式的另类解答

    2023-08-02 06:58:04
  • 详解.NET数据库连接池

    2024-01-20 16:05:59
  • Pytorch 实现sobel算子的卷积操作详解

    2022-02-22 10:11:37
  • Python 利用Entrez库筛选下载PubMed文献摘要的示例

    2021-05-27 11:35:01
  • 解决tensorflow测试模型时NotFoundError错误的问题

    2021-08-02 09:33:56
  • 用Python获取摄像头并实时控制人脸的实现示例

    2022-12-11 09:50:21
  • 纯JavaScript 实现flappy bird小游戏实例代码

    2024-05-11 09:05:57
  • Python实现遗传算法(二进制编码)求函数最优值方式

    2023-05-07 15:51:40
  • mysql设置远程访问数据库的多种方法

    2024-01-23 05:26:45
  • 关于Python解包知识点总结

    2021-01-03 22:34:15
  • 浅谈Python用QQ邮箱发送邮件时授权码的问题

    2021-10-30 13:06:59
  • Python 避免字典和元组的多重嵌套问题

    2021-01-06 00:07:26
  • 在Mac中配置Python虚拟环境过程解析

    2023-11-14 06:55:43
  • Google首页的CSS Sprite

    2007-09-29 21:36:00
  • python实现在控制台输入密码不显示的方法

    2023-06-19 22:44:23
  • 10个超实用jQuery插件资源

    2009-07-17 18:54:00
  • tensorflow 实现数据类型转换

    2023-09-20 11:35:47
  • asp之家 网络编程 m.aspxhome.com