Python3 -- 多線程(threading模塊、queue模塊)


隊列模塊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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM