question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"IndexError: Cannot choose from an empty sequence" between tasks

See original GitHub issue

Description of issue

Spurious error messages during execution.

Environment settings

Steps to reproduce

I’ve registered all my tasks by looping through the OpenAPI JSON for the API like this:

class APIRequest(TaskSet):
    def setup(self) -> None:
        for api_path, properties in self.client.get('/api/doc/?format=openapi').json()['paths'].items():
            [exclude some API paths]
            self.schedule_task(self.api_task, args=[api_path])

    def api_task(self, path: str) -> None:
        response = self.client.get("/api{}?limit=1000".format(path))
        assert response.status_code == HTTPStatus.OK, response.content.decode()


class ApiRequests(HttpLocust):
    task_set = APIRequest
    min_wait = 200
    max_wait = 1000
    host = 'https://{}'.format(os.environ['WEB_HOSTNAME'])


if __name__ == '__main__':
    locust = ApiRequests()
    locust.run()

I then run Locust: locust --csv=output/ --locustfile=api.py --no-web --run-time=5m

Expected behavior

Locust should run for five minutes and return successfully, reporting progress as it runs.

Actual behavior

Even if the run is successful, Locust will frequently print one, two or three error messages such as the below between each time it prints the summary table:

[2019-02-04 00:19:23,150] 2e0a2ecc40ea/ERROR/stderr: Traceback (most recent call last):
  File "/src/app/.venv/lib/python3.6/site-packages/locust/core.py", line 358, in run
    self.schedule_task(self.get_next_task())
  File "/src/app/.venv/lib/python3.6/site-packages/locust/core.py", line 419, in get_next_task
    return random.choice(self.tasks)
  File "/src/app/.venv/lib/python3.6/random.py", line 260, in choice
    raise IndexError('Cannot choose from an empty sequence') from None
IndexError: Cannot choose from an empty sequence

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:7

github_iconTop GitHub Comments

1reaction
cyberwcommented, Mar 30, 2020

Setup is run only once (and thus should never have been made an object method, that is a design mistake), so only the first TaskSet instance will have self.apis value.

If you set a class variable (LoadTestBehaviour.apis instead of self.apis), it should work.

1reaction
cyberwcommented, Oct 30, 2019

Oh I see what you are trying to do now. remove the call to schedule_tasks. I dont think you are meant to call it yourself. Instead, just populate a list of paths in setup and pick from that list inside your task method.

Read more comments on GitHub >

github_iconTop Results From Across the Web

IndexError: Cannot choose from an empty sequence
I am trying to run this command python run.py --mode MLE and got this error. I am not able to find the correct...
Read more >
Python error IndexError Cannot choose from an empty sequence
Your file name seems to be random. Random is a module and can not be used as a file name. Change the filename...
Read more >
Probability Calculator - IndexError: Cannot choose from an ...
That means that you move all 19 to the contents, draw them, and then try to draw the twentieth which fails because the...
Read more >
IndexError: Cannot choose from an empty sequence
I am getting the following error while loading data into the dataloader. why the data loader is unable to load all images and...
Read more >
Python Choice Function - Scaler Topics
When we pass any empty sequence to the choice() function, it will throw an IndexError. An empty sequence means it could be an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found