Locust访问Web界面时触发AttributeError异常问题
Locust Web界面访问触发AttributeError: '_io.BufferedReader' object has no attribute 'CLOSED'
问题详情
我有一个之前能正常运行的基础Locust负载测试脚本,代码如下:
from locust import HttpUser, between, task class QuickstartUser(HttpUser): wait_time = between(1, 5) @task def get_status(self): self.client.get("/status/")
执行命令:
locust -f <package-name>/tests/load_tests.py -r 20 -u 400 -H http://localhost:8000
访问Web界面时触发如下错误:
[2022-12-28 23:23:30,962] MacBook-Pro.fritz.box/INFO/locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces) [2022-12-28 23:23:30,968] MacBook-Pro.fritz.box/INFO/locust.main: Starting Locust 2.14.0 Traceback (most recent call last): File "src/gevent/greenlet.py", line 906, in gevent._gevent_cgreenlet.Greenlet.run File "/Users/<user>/Coding/PycharmProjects/<project>-fastapi/.venv/lib/python3.10/site-packages/gevent/baseserver.py", line 34, in _handle_and_close_when_done return handle(*args_tuple) File "/Users/<user>/Coding/PycharmProjects/<project>-fastapi/.venv/lib/python3.10/site-packages/gevent/pywsgi.py", line 1577, in handle handler.handle() File "/Users/<user>/Coding/PycharmProjects/<project>-fastapi/.venv/lib/python3.10/site-packages/gevent/pywsgi.py", line 464, in handle result = self.handle_one_request() File "/Users/<user>/Coding/PycharmProjects/<project>-fastapi/.venv/lib/python3.10/site-packages/gevent/pywsgi.py", line 656, in handle_one_request if self.rfile.CLOSED: AttributeError: '_io.BufferedReader' object has no attribute 'CLOSED' 2022-12-28T22:23:35Z <Greenlet at 0x106fbc3a0: _handle_and_close_when_done(<bound method WSGIServer.handle of <WSGIServer at , <bound method StreamServer.do_close of <WSGIServer, (<gevent._socket3.socket [closed] at 0x106fcb460 o)> failed with AttributeError
当前环境依赖树(通过poetry show locust --tree获取):
locust 2.14.0 开发者友好的负载测试框架 ├── configargparse >=1.0 ├── flask >=2.0.0 │ ├── click >=8.0 │ │ └── colorama * │ ├── itsdangerous >=2.0 │ ├── jinja2 >=3.0 │ │ └── markupsafe >=2.0 │ └── werkzeug >=2.2.2 │ └── markupsafe >=2.1.1 (循环依赖已中止) ├── flask-basicauth >=0.2.0 │ └── flask * │ ├── click >=8.0 │ │ └── colorama * │ ├── itsdangerous >=2.0 │ ├── jinja2 >=3.0 │ │ └── markupsafe >=2.0 │ └── werkzeug >=2.2.2 │ └── markupsafe >=2.1.1 (循环依赖已中止) ├── flask-cors >=3.0.10 │ ├── flask >=0.9 │ │ ├── click >=8.0 │ │ │ └── colorama * │ │ ├── itsdangerous >=2.0 │ │ ├── jinja2 >=3.0 │ │ │ └── markupsafe >=2.0 │ │ └── werkzeug >=2.2.2 │ │ └── markupsafe >=2.1.1 (循环依赖已中止) │ └── six * ├── gevent >=20.12.1 │ ├── cffi >=1.12.2 │ │ └── pycparser * │ ├── greenlet >=2.0.0 │ ├── setuptools * │ ├── zope-event * │ │ └── setuptools * (循环依赖已中止) │ └── zope-interface * │ └── setuptools * (循环依赖已中止) ├── geventhttpclient >=2.0.2 │ ├── brotli * │ ├── certifi * │ ├── gevent >=0.13 │ │ ├── cffi >=1.12.2 │ │ └── pycparser * │ ├── greenlet >=2.0.0 │ ├── setuptools * │ ├── zope-event * │ │ └── setuptools * (循环依赖已中止) │ └── zope-interface * │ └── setuptools * (循环依赖已中止) │ └── six * ├── msgpack >=0.6.2 ├── psutil >=5.6.7 ├── pywin32 * ├── pyzmq >=22.2.1,<23.0.0 || >23.0.0 │ ├── cffi * │ │ └── pycparser * │ └── py * ├── requests >=2.23.0 │ ├── certifi >=2017.4.17 │ ├── charset-normalizer >=2,<3 │ ├── idna >=2.5,<4 │ └── urllib3 >=1.21.1,<1.27 ├── roundrobin >=0.0.2 ├── typing-extensions >=3.7.4.3 └── werkzeug >=2.0.0 └── markupsafe >=2.1.1
解决方案
这个错误是gevent与Python 3.10的兼容性问题导致的——旧版本gevent的pywsgi模块会尝试访问_io.BufferedReader对象的CLOSED属性,但Python 3.10里该对象并没有这个属性。
解决步骤:
- 升级gevent到22.10.2或更高版本,该版本修复了Python 3.10下的这个适配问题
- 执行以下命令更新依赖:
poetry add gevent>=22.10.2
- 重新运行Locust命令即可恢复正常
内容的提问来源于stack exchange,提问作者Daniel
相关产品推荐
相关产品推荐

