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

Locust配置需求:完成全任务序列后再启动下一个用户

Locust实现串行启动用户(完成全任务序列再启动下一个)

需求说明

需要在Locust中实现:仅当当前用户完成任务序列中的全部任务后,才启动下一个用户;未完成全序列任务时,不生成新用户。

原代码

from locust import HttpUser,task,SequentialTaskSet,wait_time

@task 
def One(self):
 print("One")
 self.client.get(url="https://www.google.com")

@task
def Two(self):
print("Two")
self.client.get(url="https://www.google.com")

@task
def Three(self):
print("Three")
self.client.get(url="https://www.google.com")

@task
def Four(self):
print("Four") 
self.client.get(url="https://www.google.com")   
   
class mysequence(SequentialTaskSet):     
@task
def myseq(self):
   
  One(self)
  Two(self)
  Three(self)
  Four(self)

class abc(HttpUser):
 wait_time(0,1)
 tasks=[mysequence]

当前问题

运行日志显示,新用户会在当前用户完成全序列任务前就被生成,任务执行出现交叉:

PS C:\Users\abhijeet.goswami\src> locust -f tests/serial.py -u 5 -r 1 --host 
https://prudent-plus-staging.prudentbrokers.in/ --run-time 10s --headless --only-summary 
[2025-02-19 13:30:29,786] PGNPF3TAKACLT/INFO/locust.main: Starting Locust 2.32.5
[2025-02-19 13:30:29,786] PGNPF3TAKACLT/INFO/locust.main: Run time limit set to 10 seconds
[2025-02-19 13:30:29,786] PGNPF3TAKACLT/INFO/locust.runners: Ramping to 5 users at a rate of 1.00 per second
One
Two
One
Three
Two
One
Four
Three
Two
One
Four
[2025-02-19 13:30:33,835] PGNPF3TAKACLT/INFO/locust.runners: All users spawned: {"abc": 5} (5 total users)
One
Two
Three

解决方案

问题根源

  1. 原代码对SequentialTaskSet用法错误:未将任务直接作为任务集的类方法,而是通过封装成单个方法调用独立的@task函数,无法正确触发串行执行逻辑。
  2. Locust默认按指定速率并行生成用户,不会等待前一个用户完成任务序列。

修正后的代码

from locust import HttpUser, task, SequentialTaskSet, wait_time, events, runners

# 剩余需要启动的用户数
remaining_users = 0

@events.init.add_listener
def on_init(environment, **kwargs):
    global remaining_users
    # 从命令行参数获取总用户数
    remaining_users = environment.parsed_options.users

@events.user_stop.add_listener
def on_user_stop(user, **kwargs):
    global remaining_users
    remaining_users -= 1
    # 本地运行时,若还有剩余用户则生成下一个
    if remaining_users > 0 and isinstance(user.environment.runner, runners.LocalRunner):
        user.environment.runner.spawn_users(user.__class__, 1)

class MySequence(SequentialTaskSet):
    @task
    def task_one(self):
        print("One")
        self.client.get(url="https://www.google.com")
    
    @task
    def task_two(self):
        print("Two")
        self.client.get(url="https://www.google.com")
    
    @task
    def task_three(self):
        print("Three")
        self.client.get(url="https://www.google.com")
    
    @task
    def task_four(self):
        print("Four")
        self.client.get(url="https://www.google.com")
        # 完成最后一个任务后,中断当前任务集,触发用户停止事件
        self.interrupt()

class ABC(HttpUser):
    wait_time = wait_time(0, 1)
    tasks = [MySequence]

运行说明

执行命令时,初始用户数设为目标总用户数,生成速率设为1(仅启动第一个用户),后续用户会在前一个用户完成全序列任务后自动启动:

locust -f tests/serial.py -u 5 -r 1 --host https://prudent-plus-staging.prudentbrokers.in/ --run-time 10s --headless --only-summary

关键逻辑说明

  1. 正确使用SequentialTaskSet:将每个任务作为任务集的类方法,SequentialTaskSet会按定义顺序执行任务。
  2. 任务完成后中断用户:在最后一个任务执行完毕后调用self.interrupt(),触发用户停止事件。
  3. 监听用户停止事件生成新用户:通过user_stop事件监听,在前一个用户停止后,生成下一个用户,直到达到指定总用户数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 07:39:53