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)
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.