Python学习之路:队列及生产者消费者模型

队列:

先进先出

后进先出

VIP(优先级)

import queue

# q = queue.LifoQueue()#后进先出
#
# q.put(1)
# q.put(2)
# q.put(3)
# print(q.get())
# print(q.get())
# print(q.get())
q = queue.PriorityQueue()
q.put((10,"alex"))
q.put((-1,"chengrh"))
q.put((6,"wangsan"))
q.put((4,"zhangsan"))
print(q.get())
print(q.get())
print(q.get())
print(q.get())
队列
import threading,time
import queue

q = queue.Queue(maxsize = 10)

def producer(n):
    count = 1
    while True:

        q.put("骨头 %s" % count )
        print("生产了骨头",count)
        count += 1
        time.sleep(0.5)



def consumer(n):

    while True:

        print("[%s] 取到[%s]并且吃了它..." %(n  , q.get()))
        time.sleep(1)
        q.task_done() #告知这个任务执行完了


q = queue.Queue()



p = threading.Thread(target=producer,args = ("Alex",))
c = threading.Thread(target = consumer,args = ("Chengrh",))
p.start()

c.start()
生产者消费者模型

 

posted @ 2018-04-08 16:29  Py小白  阅读(248)  评论(0编辑  收藏  举报