from multiprocessing.dummy import Pool as ThreadPool, Condition s_list = [] con = Condition() def Shop(i): global con global s_list # 加锁保护共享资源 for x in range(5): with con: s_list.append(x) print(f"[生产者{i}]生产商品{x}") con.notify_all() # 通知消费者有货了 def User(i): global con global s_list while True: with con: if s_list: print(f"列表商品:{s_list}") name = s_list.pop() # 消费商品 print(f"[消费者{i}]消费商品{name}") print(f"列表剩余:{s_list}") else: con.wait()
def __init__(self): self.con = Condition() # 条件变量
def __init__(self): self.__h_list = [] self.__con = Condition() # 条件变量 self.__index = 0 # 索引