队列:queue具有先进先出的特性
这样我们可以把线程放在queue中,从而实现生产者和消费者模式,详见:https://blog.csdn.net/pacermaker/article/details/103086173
import queue
import threading
from random import randint
from time import sleep
# d=queue.Queue(maxsize=3)
# d.put('ljj',0)
# d.put('franck')
# d.put('幻翎')
#
# # FIFO first in first out
# print(d.get())
# print(d.get())
# print(d.get())
# print(d.get(0))
class Production(threading.Thread):
def run(self):
while True:
r=randint(0,100)
q.put(r)
print(q.qsize())
print("生产出来%s号包子"%r)
sleep(1)
class Proces(threading.Thread):
def run(self):
while True:
re=q.get()
print("吃掉%s号包子"%re)
if __name__ == '__main__':
q=queue.Queue(10)
threads=[Production(),Production(),Production(),Proces()]
for t in threads:
t.start()