Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use deque for tasks in taskset instead of plain list #2698

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions locust/test/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
7 changes: 4 additions & 3 deletions locust/user/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import random
import traceback
from collections import deque
from time import time
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down
Loading