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

KeyError on weighted tasks #886

Closed
glentsch opened this issue Sep 17, 2018 · 2 comments
Closed

KeyError on weighted tasks #886

glentsch opened this issue Sep 17, 2018 · 2 comments

Comments

@glentsch
Copy link

Description of issue / feature request

The docs state you specify weights for callables: https://docs.locust.io/en/stable/writing-a-locustfile.html#tasks-attribute, by the tasks attribute being a dictionary. However you get a KeyError.

Expected behavior

A task should be selected.

Actual behavior

There is a KeyError when selecting a task:

File "/usr/local/lib/python3.6/site-packages/locust/core.py", line 358, in run
    self.schedule_task(self.get_next_task())
  File "/usr/local/lib/python3.6/site-packages/locust/core.py", line 419, in get_next_task
    return random.choice(self.tasks)
  File "/usr/local/lib/python3.6/random.py", line 258, in choice
    return seq[i]
KeyError: 38

This is occurring because self.tasks is a dictionary, not a list. Instead it should use: random.choice(self.tasks.keys()) if self.tasks is a dict.

Environment settings (for bug reports)

  • OS: macOS 10.13.6
  • Python version: 3.6.4
  • Locust version: 0.9.0

Steps to reproduce (for bug reports)

class MyTaskSet(TaskSet):
   def func1(self):
      #do something....
    tasks = {func1: 2, ...}
@dblakemore
Copy link

This isn't quite correct. tasks on a TaskSet class might be defined as a callable:int dict, but this is then parsed into a sequance in the TaskSetMeta class:

        if "tasks" in classDict and classDict["tasks"] is not None:
            tasks = classDict["tasks"]
            if isinstance(tasks, dict):
                tasks = six.iteritems(tasks)
            
            for task in tasks:
                if isinstance(task, tuple):
                    task, count = task
                    for i in xrange(0, count):
                        new_tasks.append(task)
                else:
                    new_tasks.append(task)

I suspect your issue is more related to the way in which you're defining your callables. Can you post / send me the actual code that is raising this error (not just a snippet)

@glentsch
Copy link
Author

I refactored the code, and wrote a mockup of what I had done, though cannot reproduce the bug. I will close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants