本人测试,在网上找了这段多线程代码做接口测试,求大佬们看看为什么子线程走完了一直不继续运行

2019-12-16 10:04:20 +08:00
 as1997121000

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)

4252 次点击
所在节点    Python
22 条回复
as1997121000
2019-12-17 10:11:36 +08:00
@cherbim @helloSpringBoot @crackhopper 文件我传百度云了,大佬们可以参考下 aHR0cHM6Ly9wYW4uYmFpZHUuY29tL3MvMVFUa3lvbjkwYndrOWZoWW1vWGdmMUE=
as1997121000
2019-12-17 10:37:06 +08:00
@no1xsyzy 感谢大佬,我把执行函数的 while True 去了就是我想要的结果了,感谢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/629363

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX