推荐 最新
Frank的私人司机

单例 单例,多线程才是单例,在多进程单例无效,大家帮解释?

单例 单例,多线程才是单例,在多进程单例无效,大家帮解释? import multiprocessing import threading import time def singleton(cls): _instance = {} def inner(): if cls not in _instance: _instance[cls] = cls() return _instance[cls] return inner @singleton class Cls(object): count = 0 def __init__(self): self.count += 1 def run1(): for i in range(0, 100): a = Cls() a.count += 1 b = Cls() b.count += 1 b = Cls() b.count += 1 time.sleep(1) print("a", a.count) def run2(): for i in range(0, 100): a = Cls() a.count += 1 b = Cls() b.count += 1 b = Cls() b.count += 1 time.sleep(1) print("b", a.count) if __name__ == '__main__': threading.Thread(target=run1).start() threading.Thread(target=run2).start() # multiprocessing.Process(target=run1).start() # multiprocessing.Process(target=run2).start()

0
1
0
浏览量155