import threading
import time
import queue
q = queue.Queue()
class producer:
def write_data(self):
for i in range(10):
if not q.full():
number = i+3
q.put(number)
print(f"write data for the {i} time: value is {number}")
time.sleep(5)
class consumer:
def output(self):
while True:
i = 0
if not q.empty():
number = q.get()
print(f"Get data for the {i} time: value is {number}")
i += 1
if __name__ == "__main__":
obj_producer = producer()
obj_consumer = consumer()
th1 = threading.Thread(target = obj_producer.write_data)
th2 = threading.Thread(target = obj_consumer.output)
th1.start()
th2.start()
python 多线程队列
最新推荐文章于 2022-07-18 16:16:12 发布