From 7b17cfd8056e627abcb270056671e9c862cb193d Mon Sep 17 00:00:00 2001 From: Lars Holmberg Date: Fri, 3 May 2024 18:06:04 +0200 Subject: [PATCH 1/2] Use deque for tasks in TaskSet instead of plain list --- locust/user/task.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/locust/user/task.py b/locust/user/task.py index 1b40c7b11c..11089b9772 100644 --- a/locust/user/task.py +++ b/locust/user/task.py @@ -5,6 +5,7 @@ import logging import random import traceback +from collections import deque from time import time from typing import ( TYPE_CHECKING, @@ -276,7 +277,7 @@ class ForumPage(TaskSet): _parent: User def __init__(self, parent: User) -> None: - self._task_queue: list[Callable] = [] + self._task_queue: deque = deque() self._time_start = time() if isinstance(parent, TaskSet): @@ -369,7 +370,7 @@ def run(self): raise def execute_next_task(self): - self.execute_task(self._task_queue.pop(0)) + self.execute_task(self._task_queue.popleft()) def execute_task(self, task): # check if the function is a method bound to the current locust, and if so, don't pass self as first argument @@ -391,7 +392,7 @@ def schedule_task(self, task_callable, first=False): :param first: Optional keyword argument. If True, the task will be put first in the queue. """ if first: - self._task_queue.insert(0, task_callable) + self._task_queue.appendleft(task_callable) else: self._task_queue.append(task_callable) From 8ede95b290b5842ce1133cd31d104582d5afe846 Mon Sep 17 00:00:00 2001 From: Lars Holmberg Date: Fri, 3 May 2024 18:08:07 +0200 Subject: [PATCH 2/2] Adjust test case to use deque too --- locust/test/test_runners.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/locust/test/test_runners.py b/locust/test/test_runners.py index a903b2733a..0397e02550 100644 --- a/locust/test/test_runners.py +++ b/locust/test/test_runners.py @@ -35,7 +35,7 @@ import random import time import unittest -from collections import defaultdict +from collections import defaultdict, deque from operator import itemgetter from unittest import mock @@ -2984,7 +2984,7 @@ def test_exception_is_caught(self): class MyTaskSet(TaskSet): def __init__(self, *a, **kw): super().__init__(*a, **kw) - self._task_queue = [self.will_error, self.will_stop] + self._task_queue = deque([self.will_error, self.will_stop]) @task(1) def will_error(self):