# -*- coding:utf-8 -*-
"""
“生产者-消费者”案例
queue.Queue
put(item,block=True,timeout=None)
get(block=True,timeout=None)
full()
empty()
qsize()
"""
import time
from threading import Thread
from queue import Queue
class Producer(Thread):
def __init__(self,q):
super(Producer,self).__init__()
self.q=q
def run(self):
num=0
while not self.q.full():
num+=1
self.q.put(num,block=True,timeout=None)
print("生产了一件产品,现在共有"+str(self.q.qsize())+"件产品")
time.sleep(0.5)
else:
print("仓库已满!")
class Consumer(Thread):
def __init__(self,q):
super(Consumer,self).__init__()
self.q=q
def run(self):
while not self.q.empty():
self.q.get()
print("消费了一件产品"+",现在共有"+str(se
Python多线程 - 线程间通信之“Queue队列”
最新推荐文章于 2023-08-24 09:56:09 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)