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 = '文件名.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)