python 多線程
阿新 • • 發佈:2017-05-31
阻塞 ads coo range wait def true mina 將在
Lock對比Rlock
#coding:utf-8 import threading lock = threading.Lock() #Lock對象 lock.acquire() lock.acquire() #產生了死鎖。 lock.release() lock.release() print lock.acquire() import threading rLock = threading.RLock() #RLock對象 rLock.acquire() rLock.acquire() #在同一線程內,程序不會堵塞。 rLock.release() rLock.release()
多線程與隊列
# Python queue隊列,實現並發,在網站多線程推薦最後也一個例子,比這貨簡單,但是不夠規範 # encoding: utf-8 __author__ = ‘yeayee.com‘ # 由本站增加註釋,可隨意Fork、Copy from queue import Queue # Queue在3.x中改成了queue import random import threading import time class Producer(threading.Thread): """ Producer thread 制作線程 """ def__init__(self, t_name, queue): # 傳入線程名、實例化隊列 threading.Thread.__init__(self, name=t_name) # t_name即是threadName self.data = queue """ run方法 和start方法: 它們都是從Thread繼承而來的,run()方法將在線程開啟後執行, 可以把相關的邏輯寫到run方法中(通常把run方法稱為活動[Activity]); start()方法用於啟動線程。 """ def run(self):for i in range(5): # 生成0-4五條隊列 print("%s: %s is producing %d to the queue!" % (time.ctime(), self.getName(), i)) # 當前時間t生成編號d並加入隊列 self.data.put(i) # 寫入隊列編號 time.sleep(random.randrange(10) / 5) # 隨機休息一會 print("%s: %s producing finished!" % (time.ctime(), self.getName)) # 編號d隊列完成制作 class Consumer(threading.Thread): """ Consumer thread 消費線程,感覺來源於COOKBOOK """ def __init__(self, t_name, queue): threading.Thread.__init__(self, name=t_name) self.data = queue def run(self): for i in range(5): val = self.data.get() print("%s: %s is consuming. %d in the queue is consumed!" % (time.ctime(), self.getName(), val)) # 編號d隊列已經被消費 time.sleep(random.randrange(10)) print("%s: %s consuming finished!" % (time.ctime(), self.getName())) # 編號d隊列完成消費 def main(): """ Main thread 主線程 """ queue = Queue() # 隊列實例化 producer = Producer(‘Pro.‘, queue) # 調用對象,並傳如參數線程名、實例化隊列 consumer = Consumer(‘Con.‘, queue) # 同上,在制造的同時進行消費 producer.start() # 開始制造 consumer.start() # 開始消費 """ join()的作用是,在子線程完成運行之前,這個子線程的父線程將一直被阻塞。 join()方法的位置是在for循環外的,也就是說必須等待for循環裏的兩個進程都結束後,才去執行主進程。 """ producer.join() consumer.join() print(‘All threads terminate!‘) if __name__ == ‘__main__‘: main() """運行結果: Thu Feb 4 11:05:48 2016: Pro. is producing 0 to the queue! Thu Feb 4 11:05:48 2016: Pro. is producing 1 to the queue! Thu Feb 4 11:05:48 2016: Con. is consuming. 0 in the queue is consumed! Thu Feb 4 11:05:49 2016: Pro. is producing 2 to the queue! Thu Feb 4 11:05:50 2016: Pro. is producing 3 to the queue! Thu Feb 4 11:05:51 2016: Pro. is producing 4 to the queue! Thu Feb 4 11:05:52 2016: Con. is consuming. 1 in the queue is consumed! Thu Feb 4 11:05:53 2016: <bound method Producer.getName of <Producer(Pro., started 6864)>> producing finished! Thu Feb 4 11:06:00 2016: Con. is consuming. 2 in the queue is consumed! Thu Feb 4 11:06:06 2016: Con. is consuming. 3 in the queue is consumed! Thu Feb 4 11:06:06 2016: Con. is consuming. 4 in the queue is consumed! Thu Feb 4 11:06:12 2016: Con. consuming finished! All threads terminate! """
python 隊列
1 FIFO隊列先進先出:class Queue.Queue(maxsize)
2 LIFO類似於堆,即先進後出:class Queue.LifoQueue(maxsize)
3 優先級隊列級別越低越先出來:class Queue.PriorityQueue(maxsize)
隊列實例分別有以下操作方法:
Queue.qsize() 返回隊列的大小
Queue.empty() 如果隊列為空,返回True,反之False
Queue.full() 如果隊列滿了,返回True,反之False
Queue.full 與 maxsize 大小對應
Queue.get([block[, timeout]]) 獲取隊列,timeout等待時間
Queue.get_nowait() 相當Queue.get(False)
Queue.put(item) 寫入隊列,timeout等待時間
Queue.put_nowait(item) 相當Queue.put(item, False)
Queue.task_done() 在完成一項工作之後,Queue.task_done() 函數向任務已經完成的隊列發送一個信號
Queue.join() 實際上意味著等到隊列為空,再執行別的操作
python 多線程