! # Python
# - * - Coding: UTF-. 8 - * -
# Scene:
# Objective: to achieve the client calls sdk, sdk batch method of the client by a single data Example
# Reference:
# {
# the Python single EXAMPLE mode (the singleton) N kinds of implementation - know almost
# https://zhuanlan.zhihu.com/p/37534850
# design pattern (the Python) - singleton - simple book
# https://www.jianshu.com/ p / ec6589e02e2f
holding single embodiment mode is the only example of a multithreaded # http://xiaorui.cc/2016/04/10/python /
# PythonDecoratorLibrary - the Python Wiki
# https://wiki.python.org/moin/PythonDecoratorLibrary
# the Data Model 3. - Python 3.7.3 Documentation
# https://docs.python.org/3/reference/datamodel.html#object.__new__
# 8.10 Queue - Queue the synchronized class A - Python 2.7.16 Documentation.
# HTTPS: //docs.python.org/2/library/queue.html
At The Queue class in the this # Module All the implements at The required locking semantics.
# 3. the Data Model - Python 3.7.3 Documentation
# https://docs.python.org/3/reference/datamodel.html#specialnames
# 3. the Data Model - the Python 3.7.3 Documentation
# https://docs.python.org/3/reference/datamodel.html#object.__del__
#}
# Note:
# thread safety
# destructor example, class destructor
# tests required:
# 1, the security thread
# 2, the efficiency of
# the TODO class destructor
Import Threading
DEF make_synchronized (FUNC):
FUNC = .__ lock__ of threading.Lock ()
DEF synced_func (* args, ** KWS):
with FUNC .__ lock__:
return FUNC ( args *, ** KWS)
return synced_func
SdkSingletonBatchHandler class (Object):
__instance = None
queue # Batch Method according to its stored batch processing FIFO FIFO
queue_ = []
QueueLength = None
batchHandlerFunc = None
# queue lifetime
timeToLiveSeconds. 1 =
__bornTime = 0
@make_synchronized
DEF __new__ is (CLS, args *, ** kwargs):
IF CLS None .__ instance IS:
CLS .__ = bornTime the time.time ()
CLS .__ .__ new new Object instance = __ (CLS, args *, ** kwargs)
IF cls.queueLength IS None :
cls.queueLength = 10
IF len (cls.queue_)> or = cls.queueLength cls.timeToLiveSeconds <the time.time () - .__ bornTime CLS:
cls.batchHandler (cls.queue_)
cls.queue_ = []
cls.bornTime = time.time()
return cls.__instance
@classmethod
def batchHandler(cls, queue_):
return cls.batchHandlerFunc
import time
def 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
class TestClassDel(object):
__instance = None
def __init__(self):
print("init")
def __new__(cls, *args, **kwargs):
if cls.__instance is None:
cls.__instance = object.__new__(cls, *args, **kwargs)
return cls.__instance
def __del__(self):
print(1)
s1 = TestClassDel()
s2 = TestClassDel()
del s1
del s2
Heat
Heat
1
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
class TestClassDel(object):
__instance = None
def __init__(self):
print "init"
def __new__(cls, *args, **kwargs):
if cls.__instance is None:
cls.__instance = object.__new__(cls, *args, **kwargs)
return cls.__instance
def __del__(self):
print 1
s1 = TestClassDel()
s2 = TestClassDel()
del s1
del s2
Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win32
heat
heat
No print 1
Note that the above embodiment is a single-mode
class TestClassDel(object):
__instance = None
def __init__(self):
print "init"
def __new__(cls, *args, **kwargs):
if cls.__instance is None:
cls.__instance = object.__new__(cls, *args, **kwargs)
return cls.__instance
def __del__(self):
print 1
s1 = TestClassDel()
s2 = TestClassDel()
del s1
del s2
class T():
def __call__(self, *args, **kwargs):
print 1
d=T().__call__()
1 Print
Inspired
[Note] target environment python2.7.5