import requests
import threading
import time
def req():
s = time.time()
r = requests.get('https://www.google.com/')
e = time.time()
response_times.append(e - s)
response_times = []
thread_amount = 50
print(f"{thread_amount} parallel threads")
threads = []
s1 = time.time()
for i in range(thread_amount):
threads.append(threading.Thread(target = req))
s2 = time.time()
for thread in threads:
thread.start()
s3 = time.time()
for thread in threads:
thread.join()
s4 = time.time()
total_time = s4 - s1
spawn_time = s2 -s1
start_time = s3 -s2
finish_time = s4 -s3
average_time_per_thread = sum(response_times) / len(response_times)
print(f"average response time per request: {average_time_per_thread} s")
print(f"total time: {total_time} s")
print(f"spawn time: {spawn_time} s")
print(f"start time: {start_time} s")
print(f"finish time: {finish_time} s")
对于50个并行线程/请求,每个请求的平均响应时间为5.5秒。我已经知道这个问题与SSL证书有关,对于100多个并行请求,我得到一个错误:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 439, in send
resp = conn.urlopen(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 755, in urlopen
retries = retries.increment(
File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 574, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLZeroReturnError(6, 'TLS/SSL connection has been closed (EOF) (_ssl.c:997)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
self.run()
File "/usr/lib/python3.10/threading.py", line 946, in run
self._target(*self._args, **self._kwargs)
File "/root/speedtest.py", line 7, in req
r = requests.get('https://www.google.com/')
File "/usr/lib/python3/dist-packages/requests/api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "/usr/lib/python3/dist-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 514, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLZeroReturnError(6, 'TLS/SSL connection has been closed (EOF) (_ssl.c:997)')))
这个问题发生在我在ubuntu22.04服务器上执行以下操作之后:sudo snap install core; sudo snap refresh core
-sudo snap install --classic certbot
-sudo ln -s /snap/bin/certbot /usr/bin/certbot
-sudo apt-get install nginx
-sudo certbot --nginx
(在此步骤中,我必须为我的域创建一个证书)-sudo certbot renew --dry-run
在此之前,每个请求的平均响应时间是0.45秒。看起来我崩溃了python请求的SSL证书,但我不知道是哪个文件导致了这个问题。如果我将请求改为requests.get('https://www.google.com/', verify=False)
,平均响应时间将再次是0.45秒,但我认为这不是一个好的解决方案。
1条答案
按热度按时间14ifxucb1#
过了一段时间,我在另一台机器上安装了Ubuntu 22.04,它仍然是一样的。我用不同的Ubuntu版本做了一些测试,下面是我用新安装的操作系统和最初帖子中显示的脚本的结果:Ubuntu 22.04版:约6秒,Ubuntu 22.10:约4秒,Ubuntu 20.04:约1秒,Windows 10:约1秒
在Ubuntu 20.04上,我测试了python 3.8(预装)和3.10(预装在Ubuntu 22.xx上),两个版本都有相同的结果,大约1秒。有什么解决方案可以让它在Ubuntu 22上也工作吗?我有一台新电脑,想使用Ubuntu 22。