python 如何控制多线程数量
发布网友
发布时间:2022-04-25 13:00
我来回答
共1个回答
热心网友
时间:2024-03-16 08:25
import requests, timefrom threading import Threadclass MyThread(Thread):
def __init__(self, url):
Thread.__init__(self)
self.url = url def run(self):
open_url(self.url)def open_url(url):
r = requests.get(url[:-1])
print(r.status_code)
print(url) # return urlif __name__ == '__main__': with open("E:/all_domain.txt",'r') as f:
t_start = time.time()
threads = [] for url in f:
t = MyThread(url)
threads.append(t)
t.start()
print(len(threads)) for i in threads:
i.join()
t_end = time.time()
print('the thread way take %s s' % (t_end - t_start))