python 线程安全 单例

265 阅读2分钟

什么叫 单例 模式

多次初始化 只返回 同一个对象 叫做单例模式

为什么 要使用单例模式

  1. 代码 中 有些 对象 比如 日志对象 为了防止 多次初始化 可以使用单例模式
  2. 有些 模型 对象 体积庞大, 加载缓慢, 必须使用 单例模式

python 单例 线程安全的 实现方式

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,name):
        self.name = name
        print(name)


obj1 = Foo('name2')
obj2 = Foo('name1')
print(obj1,obj2)

# 测试结果 init 函数 只被执行了一次
# python3 singlon.py 
# name2
# <__main__.Foo object at 0x7f7cd9b04860> <__main__.Foo object at 0x7f7cd9b04860>

使用 单例 封装 日志对象

  1. 实现日志单例
  2. 实现日志 分级 输出文件
  3. 实现日志 按照 每周 分文件保存

Singleton.py 单例基类

#!/usr/bin/python
# -*- coding:utf-8 -*-
'''
@File    :   Singleton.py
@Time    :   2020/12/14 14:17:11
@Author  :   lmk
@Version :   1.0
'''

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

if __name__ == '__main__':
    pass

singleton_log.py 每天 midnight 更换日志文件

#!/usr/bin/python
# -*- coding:utf-8 -*-
'''
@File    :   singleton_log.py
@Time    :   2020/12/14 14:26:05
@Author  :   lmk
@Version :   1.0
'''


from scripts.Singleton import SingletonType
from logging import Logger
import logging  # 引入logging模块
import logging.handlers
import os
import time

from gen_log.a_log_config import info_log_path,err_log_path


class singleLogger(metaclass=SingletonType):
    def __init__(self):
        # 第一步,创建一个logger
        self.logger = logging.getLogger()
        self.logger.setLevel(logging.INFO)  # Log等级总开关

        # 第二步,创建一个handler,用于写入日志文件
        # fh = logging.FileHandler(info_log_path,mode="w")
        info_fh = logging.handlers.TimedRotatingFileHandler(info_log_path,when="midnight")

        # fh.setLevel(logging.DEBUG)  # 输出到file的log等级的开关
        # 设置日志过滤器
        info_filter = logging.Filter()
        info_filter.filter = lambda record: record.levelno <= logging.WARNING # 设置过滤等级
        info_fh.setLevel(logging.INFO)
        info_fh.addFilter(info_filter)


        # fh = logging.FileHandler(info_log_path,mode="w")
        err_fh = logging.handlers.TimedRotatingFileHandler(err_log_path,when="midnight")


        # fh.setLevel(logging.DEBUG)  # 输出到file的log等级的开关
        err_filter = logging.Filter()
        err_filter.filter = lambda record: record.levelno > logging.WARNING
        err_fh.setLevel(logging.ERROR)
        err_fh.addFilter(err_filter)
       

        # 第三步,定义handler的输出格式
        formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: \n%(message)s\n")
        info_fh.setFormatter(formatter)
        err_fh.setFormatter(formatter)
        # 第四步,将logger添加到handler里面
        self.logger.addHandler(info_fh)
        self.logger.addHandler(err_fh)

logger = singleLogger().logger

a_log_config.py 日志配置

#!/usr/bin/python
# -*- coding:utf-8 -*-
'''
@File    :   a_log_config.py
@Time    :   2020/12/14 13:28:02
@Author  :   lmk
@Version :   1.0
'''


import os 
import time

opdn = os.path.dirname
dirpath = opdn(opdn(__file__))

logs_dir_path = os.path.join(dirpath,"Logs")

# 消息日志路径
# info_log_name = time.strftime('info_%Y%m%d.log', time.localtime(time.time()))
info_log_name = time.strftime('info.log', time.localtime(time.time()))
info_log_path = os.path.join(logs_dir_path,info_log_name)

# 错误日志路径
# err_log_name = time.strftime('err_%Y%m%d.log', time.localtime(time.time()))
err_log_name = time.strftime('err.log', time.localtime(time.time()))
err_log_path = os.path.join(logs_dir_path,err_log_name)



if __name__ == '__main__':
    pass

最后可以项目 入口 将异常写入 日志

main.py s = traceback.format_exc() logger.error(s)

将异常调用栈 信息 取出并 记录到日志


# 开始 定时 处理授权文件
@my_decorator.while_do
def grant_auth():
    try:
        global count
        count+=1
        deal_temp_grant()
        raise(Exception("err {}".format(count)))
    except:
        s = traceback.format_exc()
        logger.error(s)