定义
class Singleton(object):
"""Singleton base class"""
_instance_lock = _thread.allocate_lock()
def __init__(self, *args, **kwargs):
pass
def __new__(cls, *args, **kwargs):
if not hasattr(cls, "instance_dict"):
Singleton.instance_dict = {}
if str(cls) not in Singleton.instance_dict.keys():
with Singleton._instance_lock:
_instance = super().__new__(cls)
Singleton.instance_dict[str(cls)] = _instance
return Singleton.instance_dict[str(cls)]
使用
class SystemRuning(Singleton):
def __init__(self):
pass
def GetRunStatus(self):
usr = uos.statvfs("/usr")
memA = gc.mem_alloc()/1024
memF = gc.mem_free()/1024
bak = uos.statvfs("/bak")
p1=(1-(usr[0] * usr[3])/(usr[0] * usr[2]))*100.00
p2=(memA/(memF+memA))*100.00
return "====SystemRun====\r\n==usr:\r\n 文件块数:{}\r\n 文件空间:{}\r\n 可用块数: {}\r\n 可用空间:{} KB\r\n 已使用率:{}%\r\n==bar:\r\n 文件块数:{}\r\n 文件空间:{}\r\n 可用块数: {}\r\n 可用空间:{} KB\r\n===RAM:\r\n 已经使用:{}KB\r\n 剩余可用:{}KB\r\n 已使用率:{}%".format(usr[2],(usr[0] * usr[2])/1024,usr[3],(usr[0]*usr[3])/1024,'%.2f'%p1,bak[0],(bak[0] * bak[2])/1024,bak[3],(bak[0] * bak[3])/1024,'%.2f'%memA,'%.2f'%memF,'%.2f'%p2)
调用
print(SystemRuning())
print(SystemRuning())
- 打印出来为同一对象
- 都会执行
__init__
方法