我制作了一个小型 python 脚本来对我一直在开发的网站进行压力测试,然后再将其公开。它通过每 5 分钟生成一个新线程来运行,该线程在 while(true) 循环中运行以下代码。
conn = httplib.HTTPSConnection("site", 000, "pem", "pem", timeout = 30)
conn.request("GET", "/reports.php?" + url, headers = headers)
response = conn.getresponse()
read = (response.read())
如果我只有 1 个线程,那么代码请求/响应每次都会成功。当我创建新线程时,我更频繁地收到以下错误。最终,当有大约 10 个线程时,大约 90% 的时间它都会失败。
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 505, in run
self.__target(*self.__args, **self.__kwargs)
File "stresstest.py", line 32, in threadproc
stressTest(conn)
File "stresstest.py", line 76, in stressTest
response = conn.getresponse()
File "/usr/lib/python2.7/httplib.py", line 1027, in getresponse
response.begin()
File "/usr/lib/python2.7/httplib.py", line 407, in begin
version, status, reason = self._read_status()
File "/usr/lib/python2.7/httplib.py", line 365, in _read_status
line = self.fp.readline()
File "/usr/lib/python2.7/socket.py", line 430, in readline
data = recv(1)
File "/usr/lib/python2.7/ssl.py", line 232, in recv
return self.read(buflen)
File "/usr/lib/python2.7/ssl.py", line 151, in read
return self._sslobj.read(len)
SSLError: The read operation timed out
我想知道 httplib 使用的 ssl 代码是否不是线程安全的,或者是否有其他原因导致失败?