# coding:utf-8
import requests
import threading
from queue import Queue
import time
num_xian = input("請輸入要執行的執行緒數:")
# 添加執行緒
threadList = int(num_xian)
# 設定佇列長度
workQueue = Queue(10)
# 執行緒池
threads = []
stare = time.time()
def crawler(threadName, q):
# 執行多執行緒
# 從佇列里取出資料
j = q.get(timeout=2)
try:
print('執行緒:' + threadName)
except Exception as e:
print(q.qsize(), threadName, "號執行緒運行錯誤 ", e)
class myThread(threading.Thread):
def __init__(self, name, q):
threading.Thread.__init__(self)
self.name = name
self.q = q
def run(self):
try:
print(self.name + "號執行緒啟動 ")
crawler(self.name, self.q)
print(self.name + "號執行緒結束 ")
except:
print(self.name + "號執行緒啟動失敗 ")
# 創建新執行緒
for tName in range(1,threadList+1):
thread = myThread(tName, workQueue)
thread.start()
threads.append(thread)
# 讀取資料,放入佇列
filename = 'saas_user_mini.txt'
f = open(filename, 'r', encoding="utf-8").readlines()
for i in f:
workQueue.put(i)
# 等待所有執行緒完成
for t in threads:
t.join()
end = time.time()
print('Queue多執行緒批量執行時間為:', end - start)
uj5u.com熱心網友回復:
求大佬們幫幫忙轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/115241.html
下一篇:如何將偽彩色影像轉回灰度影像
