隊列模塊queue:
from queue import Queue # 使用 q = Queue() q.put(url) # url ,這里只是舉個栗子 # 獲取隊列內容 q.get() # 當隊列為空時,發生阻塞 # 獲取隊列內容 q.get(block=True, timeout=3) # 超過3秒,拋異常 # 獲取隊列內容 q.get(block=False) # 隊列為空時,直接拋異常 # 判斷隊列是否為空 q.empty() # 如果隊列為空,返回True,反之False
線程模塊threading:
from threading import Thread # 使用流程 t = Thread(target=函數名) # 創建線程對象 t.start() # 創建並啟動線程 t.join() # 阻塞等待回收線程
創建多線程:
from threading import Thread # 使用流程 t_list = [] for i in range(10): t = Thread(target=函數名) # 創建線程對象 t_list.appent(t) t.start() for t in t_list: t.join()