python析构_python 析构函数 实例析构 类析构

#!python# -*- coding:utf-8 -*-# 场景:# 目的:通过单例实现客户端调用sdk时,sdk中的方法对客户端数据的批处理# 参考:# {# Python单例模式(Singleton)的N种实现 - 知乎# https://zhuanlan.zhihu.com/p/37534850# 设计模式(Python)-单例模式 - 简书# https://www.jianshu.com/p/ec6589e02e2f# http://xiaorui.cc/2016/04/10/python多线程下保持单例模式的实例唯一/# PythonDecoratorLibrary - Python Wiki# https://wiki.python.org/moin/PythonDecoratorLibrary# 3. Data model — Python 3.7.3 documentation# https://docs.python.org/3/reference/datamodel.html#object.__new__# 8.10. Queue — A synchronized queue class — Python 2.7.16 documentation# https://docs.python.org/2/library/queue.html# The Queue class in this module implements all the required locking semantics.# 3. Data model — Python 3.7.3 documentation# https://docs.python.org/3/reference/datamodel.html#specialnames# 3. Data model — Python 3.7.3 documentation# https://docs.python.org/3/reference/datamodel.html#object.__del__# }# 注意:# 线程安全# 实例析构、类的析构# 需要测试:# 1、线程安全# 2、效率import threadingdef make_synchronized(func): func.__lock__ = threading.Lock() def synced_func(*args, **kws): with func.__lock__: return func(*args, **kws) return synced_funcclass SdkSingletonBatchHandler(object): __instance = None # 存放批处理的队列 批处理方法对其按照先进先出FIFO处理 queue_ = [] queueLength = None batchHandlerFunc = None # 队列生存时间 timeToLiveSeconds = 1 bornTime = 0 @make_synchronized def __new__(cls, *args, **kwargs): if cls.__instance is None: cls.bornTime = time.time() cls.__instance = object.__new__(cls, *args, **kwargs) if cls.queueLength is None: cls.queueLength = 10 if len(cls.queue_) >= cls.queueLength or cls.timeToLiveSeconds < time.time() - cls.bornTime: cls.batchHandler(cls.queue_) cls.queue_ = [] cls.bornTime = time.time() return cls.__instance @classmethod def batchHandler(cls, queue_): return cls.batchHandlerFuncimport timedef bizFuncNotBatch(param): time.sleep(0.02) print "do sth" + str(param)def bizFuncBatch(param): def batchHandler(paramList): for i in paramList: pass time.sleep(0.02) print "do sth" + str(paramList) s = SdkSingletonBatchHandler() s.__class__.queueLength = 100 s.__class__.batchHandlerFunc = batchHandler s.queue_.append(param) print "do sth Batch" # return 测试所需对象id return s# 测试# 线程安全def testThreadSafeWorker(): s1 = bizFuncBatch("param") s2 = bizFuncBatch("param") print "id1={},id2={}".format(id(s1), id(s2))task = []for i in range(300): t = threading.Thread(target=testThreadSafeWorker()) task.append(t)for i in task: i.start()for i in task: i.join()# 测试# 效率data = [{‘i‘: i, ‘v‘: "value"} for i in range(1000)]consoleInfo = []consoleInfo.append("notBatch:Start:" + time.ctime())for i in data: bizFuncNotBatch(i)consoleInfo.append("notBatch:End:" + time.ctime())consoleInfo.append("Batch:Start:" + time.ctime())for i in data: bizFuncBatch(i)consoleInfo.append("Batch:End:" + time.ctime())for i in consoleInfo: print i

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值