Skip to content

Instantly share code, notes, and snippets.

@nmccready
Created May 3, 2013 13:37
Show Gist options
  • Save nmccready/5509151 to your computer and use it in GitHub Desktop.
Save nmccready/5509151 to your computer and use it in GitHub Desktop.
locust via lamda?
from locust import Locust, TaskSet
urls = ["someurl1", "someurl2"]
lamda_set = {}
for url in urls:
lamda_set[lambda l: l.client.get(url)] = 1
class UserBehavior(TaskSet):
tasks = lamda_set
def on_start(self):
pass
class WebsiteUser(Locust):
task_set = UserBehavior
min_wait = 1000
max_wait = 1000
@nmccready
Copy link
Author

GOT it!!! With help from here, http://stackoverflow.com/questions/452610/how-do-i-create-a-list-of-python-lambdas-in-a-list-comprehension-for-loop

Basically the lambda was being stored to the same reference. So by defining a function to return a lambda, it creates a new one each time.

from locust import Locust, TaskSet

urls = ("url1", "url2")


def local_print(arg):
    print arg


def create_lamda(url):
    print url
    return lambda l: l.client.get(url)

lambda_set = {}

weight = 1
for url in urls:
    lamda_set[create_lamda(url)] = weight
    weight += 1


class UserBehavior(TaskSet):
    print lambda_set
    tasks = lambda_set


def on_start(self):
    pass


class WebsiteUser(Locust):
    task_set = UserBehavior
    min_wait = 1000
    max_wait = 1000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment