Skip to content

Commit

Permalink
Merge pull request #2698 from locustio/use-deque-for-tasks-in-taskset…
Browse files Browse the repository at this point in the history
…-instead-of-plain-list

Use deque for tasks in taskset instead of plain list
  • Loading branch information
cyberw authored May 3, 2024
2 parents 9417953 + 8ede95b commit eb95466
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
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

0 comments on commit eb95466

Please sign in to comment.