Locust: IndexError: Cannot choose from an empty sequence

Created on 26 May 2018  ·  8Comments  ·  Source: locustio/locust

Description of issue / feature request

  File "/media/jawahar/jon1/new_project/bm/testDev/lib/python3.6/site-packages/locust/core.py", line 268, in run
    self.schedule_task(self.get_next_task())
  File "/media/jawahar/jon1/new_project/bm/testDev/lib/python3.6/site-packages/locust/core.py", line 329, in get_next_task
    return random.choice(self.tasks)
  File "/media/jawahar/jon1/new_project/bm/testDev/lib/python3.6/random.py", line 258, in choice
    raise IndexError('Cannot choose from an empty sequence') from None
IndexError: Cannot choose from an empty sequence

Repeately i am getting this error after Start swarming number of users.

Expected behavior

No Error related to this.

Actual behavior

Traceback showed about

Environment settings (for bug reports)

  • OS:Ubuntu 17
  • Python version: 3.6.5
  • Locust version: 0.8.0/0.8.1

Steps to reproduce (for bug reports)

Run some api and you will get this error

Most helpful comment

IndexError: Cannot choose from an empty sequence

@jawahar273,
That exception gets raised when there are no tasks defined. Can you verify you actually declared some tasks in your locustfile? (either use the @tasks decorator or define a class that inherits from TaskSet)

for Locust devs,
A nice enhancement would be to catch the IndexError in /locust/core.py and raise a RuntimeError instead.. It should display an intuitive message like "No tasks defined in locustfile", and then exit.

All 8 comments

IndexError: Cannot choose from an empty sequence

@jawahar273,
That exception gets raised when there are no tasks defined. Can you verify you actually declared some tasks in your locustfile? (either use the @tasks decorator or define a class that inherits from TaskSet)

for Locust devs,
A nice enhancement would be to catch the IndexError in /locust/core.py and raise a RuntimeError instead.. It should display an intuitive message like "No tasks defined in locustfile", and then exit.

ah.. we should check for this in code.

import logging

from locust import TaskSet, task

logger = logging.getLogger(__name__)


class UserBehavior(TaskSet):

    def __init__(self, parent):
        super().__init__(parent=parent)

        self.token_key = "Token "

    def base_url(self):

        return "/api/"

    def packages_url(self):

        return self.base_url() + "package/"

    def user_details(self):

        return {"username": "demo", "password": "demobmmb"}

    def on_start(self):
        """ on_start is called
            when a Locust start before
            any task is scheduled
        """
        self.login()

    def login(self):
        # self.token_key = 'Token '
        logger.info("login user")
        response = self.client.post(
            self.base_url() + "rest-auth/login/", data=self.user_details()
        )
        self.token_key = self.token_key + response.text

        logger.debug("response from host" + str(response))
        print("Response status code:", response.status_code)
        print("Response content:", response.text)

    @task(1)
    def currency_details(self):
        currency = self.client.get(
            self.packages_url() + "currency/",
            headers={"authentication": self.token_key},
        )

        if currency.status_code >= 300:

            logger.debug("response from host for currency" + currency.text)

        else:

            logger.info("success in currency", currency.status_code)

This is the actual code.

what are the items in "Addtion feedback" about?

Leave it.
Did you get the error that I was trying to show.

Did you get the error that I was trying to show

no

Yes i got the error after removing the @task(1) and if you run the code with @task(1) there is no error in running the locust.

Is this a series issue or not.

if you write an invalid locustfile and don't declare tasks, expect things to break. As I said previously, the error message could be improved.

Was this page helpful?
0 / 5 - 0 ratings