import threading
class Test(object):
from threading import Lock
lock = Lock()
flag = None
def __new__(cls, *args, **kwargs):
with cls.lock:
if cls.flag is None:
cls.flag = super().__new__(cls)
return cls.flag
def func():
res = Msg()
print(res)
t = threading.Thread(target=fun).start()
t2 = threading.Thread(target=fun).start()
#<__main__.Msg object at 0x00000193A9C3A198>
#<__main__.Msg object at 0x00000193A9C3A198>
原文:https://www.cnblogs.com/wangtaobiu/p/10951437.html