from concurrent.futures import ThreadPoolExecutor
import time
import requests
import urllib3
urllib3.disable_warnings()
# 定义一个准备作为线程任务的函数
def action():
my_sum = 0
requests.get('https://www.baidu.com/', verify=False)
return my_sum
# 创建一个包含100条线程的线程池
pool = ThreadPoolExecutor(max_workers=100)
start_time = time.perf_counter()
for i in range(50):
# 创建线程池
pool.submit(action)
print(i)
print('运行时长:', time.perf_counter()-start_time)
# 关闭线程池
pool.shutdown()
实测50个请求不到0.1秒,对于python而言挺好的了