! # Python
# - * - Coding: UTF-. 8 - * -
# Objective: batch processing when the client calls a single sdk Example
# Reference:
# {
# the Python singleton pattern (the Singleton) N kinds achieved - almost known
https://zhuanlan.zhihu.com/p/37534850 #
# design patterns (Python) - singleton - Jane book
# https://www.jianshu.com/p/ec6589e02e2f
# PythonDecoratorLibrary - Python Wiki
# HTTPS: / /wiki.python.org/moin/PythonDecoratorLibrary
# 3. the Data Model - Python 3.7.3 Documentation
# https://docs.python.org/3/reference/datamodel.html#object.__new__
#
#}
# Note:
# thread-safe
class SdkSingletonLoger (Object):
__instance = None
# store batch queues FIFO the FIFO
queue = []
QueueLength = None
def __new__(cls, *args, **kwargs):
if cls.__instance is None:
cls.__instance = object.__new__(cls, *args, **kwargs)
if cls.queueLength is None:
cls.queueLength = 50
if len(cls.queue) >= cls.queueLength:
print "batchHandler"
print len(cls.queue)
print cls.queueLength
return cls.__instance
i = 0
t = (i, 333, {})
i += 1
s1 = SdkSingletonLoger()
s1.queue.append(t)
# s1.queueLength = 2
s1.__class__.queueLength = 2
t = (i, 333, {})
i += 1
s2 = SdkSingletonLoger()
s2.queue.append(t)
t = (i, 333, {})
i += 1
s3 = SdkSingletonLoger()
s3.queue.append(t)
t = (i, 333, {})
i += 1
s4 = SdkSingletonLoger()
s4.queue.append(t)
dd = 9
0
50
1
2
batchHandler
2
2
batchHandler
3
2
! # Python
# - * - Coding: UTF-. 8 - * -
# Objective: batch processing when the client calls a single sdk Example
# Reference:
# {
# the Python singleton pattern (the Singleton) N kinds achieved - almost known
https://zhuanlan.zhihu.com/p/37534850 #
# design patterns (Python) - singleton - Jane book
# https://www.jianshu.com/p/ec6589e02e2f
# PythonDecoratorLibrary - Python Wiki
# HTTPS: / /wiki.python.org/moin/PythonDecoratorLibrary
# 3. the Data Model - Python 3.7.3 Documentation
# https://docs.python.org/3/reference/datamodel.html#object.__new__
#
#}
# Note:
# thread-safe
class SdkSingletonLoger (Object):
__instance = None
# store batch queues FIFO the FIFO
queue = []
QueueLength = None
def __new__(cls, *args, **kwargs):
if cls.__instance is None:
cls.__instance = object.__new__(cls, *args, **kwargs)
if cls.queueLength is None:
cls.queueLength = 50
if len(cls.queue) >= cls.queueLength:
print "batchHandler"
print len(cls.queue)
print cls.queueLength
return cls.__instance
i = 0
t = (i, 333, {})
i += 1
s1 = SdkSingletonLoger()
s1.queue.append(t)
s1.queueLength = 2
# s1.__class__.queueLength = 2
t = (i, 333, {})
i += 1
s2 = SdkSingletonLoger()
s2.queue.append(t)
t = (i, 333, {})
i += 1
s3 = SdkSingletonLoger()
s3.queue.append(t)
t = (i, 333, {})
i += 1
s4 = SdkSingletonLoger()
s4.queue.append(t)
dd = 9
0
50
1
50
2
50
3
50
Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win32