Python的Queue模块中提供了同步的、线程安全的队列类,包括FIFO(先入先出)队列Queue,LIFO(后入先出)队列LifoQueue,和优先级队列PriorityQueue。这些队列都实现了锁原语,能够在多线程中直接使用。可以使用队列来实现线程间的同步。
#encoding=utf-8
import threading
import time
from Queue
import Queue
class Producer(threading.Thread):
def run(self):
global queue
count = 0
while True:
for i
in range(100):
if queue.qsize() > 1000:
pass else:
count = count +1
msg =
'生成产品'+str(count)
queue.put(msg)
print msg
time.sleep(1)
class Consumer(threading.Thread):
def run(self):
global queue
while True:
for i
in range(3):
if queue.qsize() < 100:
pass else:
msg = self.name +
'消费了 '+queue.get()
print msg
time.sleep(1)
queue = Queue()
def test():
for i
in range(500):
queue.put(
'初始产品'+str(i))
for i
in range(2):
p = Producer()
p.start()
for i
in range(5):
c = Consumer()
c.start()
if __name__ ==
'__main__':
test()