1. 程式人生 > >#queue隊列 #生產者消費者模型

#queue隊列 #生產者消費者模型

後進先出 item col con pri args lifo out done

技術分享
  1 #queue隊列  #生產者消費者模型
  2 
  3 #queue隊列 #有順序的容器
  4 #程序解耦
  5 #提高運行效率
  6 
  7 #class queue.Queue(maxsize=0) #先入先出
  8 #class queue.LifoQueue(maxsize=0)最後在第一
  9 #class queue.PriorityQueue(maxsize=0) #存儲數據時可設置優先級的隊列#VIP客戶
 10 
 11 #Queue.qsize()
 12 #Queue.empty() #return True if empty
 13 #
Queue.full() # return True if full 14 #Queue.put(item, block=True, timeout=None) 15 16 ‘‘‘ 17 import queue 18 q = queue.Queue() 19 q.put(‘d1‘) 20 q.put(‘d2‘) 21 q.put(‘d3‘) 22 print (q.qsize()) 23 24 print(q.get()) 25 print(q.get()) 26 print(q.get()) 27 print(q.get())#沒有東西就卡死了 28 ‘‘‘
29 ‘‘‘ 30 import queue 31 32 q = queue.Queue() 33 q.put(1) 34 q.put(2) 35 q.put(3) 36 37 print(q.get()) 38 print(q.get()) 39 print(q.get()) 40 #print(q.get())#沒有東西就卡死了 41 42 print(q.qsize())#查看 43 q.get_nowait()#異常 44 ‘‘‘ 45 ‘‘‘ 46 #後進先出 47 import queue 48 q = queue.LifoQueue()
49 q.put(1) 50 q.put(2) 51 q.put(3) 52 53 print(q.get()) 54 print(q.get()) 55 print(q.get()) 56 ‘‘‘ 57 ‘‘‘ 58 #VIP 59 import queue 60 61 q = queue.PriorityQueue() 62 q.put((-1,‘c‘)) 63 q.put((3,‘h‘)) 64 q.put((10,‘alex‘)) 65 q.put((6,‘w‘)) 66 67 print(q.get()) 68 print(q.get()) 69 print(q.get()) 70 print(q.get()) 71 ‘‘‘ 72 #生產者消費者模型 73 74 import threading,time 75 import queue 76 77 q = queue.Queue(maxsize=10) 78 79 def Producer(name): 80 count =1 81 while True: 82 q.put(骨頭%s% count) 83 print (生成了骨頭,count) 84 count +=1 85 time.sleep(0.1) 86 def Consumer(name): 87 #while q.qsize() > 0 : 88 while True: 89 print ([%s] 取到 [%s] 並且吃了它...% (name,q.get())) 90 time.sleep(1) 91 92 p = threading.Thread(target=Producer,args=(Alex,)) 93 c = threading.Thread(target=Consumer,args=(陳榮華,)) 94 c1 = threading.Thread(target=Consumer,args=(王森,)) 95 p.start() 96 c.start() 97 c1.start() 98 99 100 101 102 #下面來學習一個最基本的生產者消費者模型的例子 103 ‘‘‘ 104 import threading 105 import queue 106 107 def producer(): 108 for i in range(10): 109 q.put("骨頭 %s" % i ) 110 111 print("開始等待所有的骨頭被取走...") 112 q.join() 113 print("所有的骨頭被取完了...") 114 115 def consumer(n): 116 while q.qsize() >0: 117 print("%s 取到" %n , q.get()) 118 q.task_done() #告知這個任務執行完了 119 120 q = queue.Queue() 121 p = threading.Thread(target=producer,) 122 p.start() 123 c1 = consumer("李闖") 124 ‘‘‘ 125 126 127 ‘‘‘ 128 import time,random 129 import queue,threading 130 q = queue.Queue() 131 def Producer(name): 132 count = 0 133 while count <20: 134 time.sleep(random.randrange(3)) 135 q.put(count) 136 print(‘Producer %s has produced %s baozi..‘ %(name, count)) 137 count +=1 138 def Consumer(name): 139 count = 0 140 while count <20: 141 time.sleep(random.randrange(4)) 142 if not q.empty(): 143 data = q.get() 144 print(data) 145 print(‘1mConsumer %s has eat %s baozi...‘ %(name, data)) 146 else: 147 print("-----no baozi anymore----") 148 count +=1 149 p1 = threading.Thread(target=Producer, args=(‘A‘,)) 150 c1 = threading.Thread(target=Consumer, args=(‘B‘,)) 151 p1.start() 152 c1.start() 153 ‘‘‘
#queue隊列 #生產者消費者模型

#queue隊列 #生產者消費者模型