You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Locust无法识别@task装饰器求助:首次压测遇异常报错

问题原因及解决办法

你的locustfile.py里存在语法错误,导致Locust无法正确识别任务:

  • 在boat方法中,self.client("/other_page")是错误写法,client是HttpSession实例,不能直接调用,必须使用对应的HTTP方法(比如get)。

修正后的代码:

from locust import HttpUser, task, between

class StaticWebSite(HttpUser):
    wait_time = between(1, 5)
    host = "https://example.com"

    @task
    def index(self):
        self.client.get("/")

    @task
    def boat(self):
        self.client.get("/other_page")  # 修正此处,添加.get方法

补充说明

当Locust加载用户类时,如果某个带@task装饰器的方法存在语法错误,会导致整个任务解析失败,最终触发"No tasks defined"的错误提示。修复语法错误后,Locust就能正常识别两个任务了。

内容的提问来源于stack exchange,提问作者Roland

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 02:17:36