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

Event spawning_complete fires every time a user is created #2671

Open
2 tasks done
NicoAdrian opened this issue Apr 9, 2024 · 5 comments
Open
2 tasks done

Event spawning_complete fires every time a user is created #2671

NicoAdrian opened this issue Apr 9, 2024 · 5 comments
Labels

Comments

@NicoAdrian
Copy link

NicoAdrian commented Apr 9, 2024

Prerequisites

Description

The event spawning_complete is fired everytime a user is created inside a worker instead of when all users in the worker have spawned. Is it a bug or the intended behaviour ? If so, what can I do to fire a callback when all users inside a worker have spawned ?

@events.spawning_complete.add_listener
def on_spawning_complete(user_count):
    print("SPAWN", user_count)

Prints:
SPAWN 1
SPAWN 1
SPAWN 2
SPAWN 2
SPAWN 3

(I have 2 workers and set user count to 5 with a spawn rate of 1 in the GUI)

Instead, I expected only "SPAWN 3"

To add more details of what I want to do: I want to disable http decompression (there isn't a built-in option in the FastHTTPClient to do this, so I do zlib.decompress = lambda *a, **b: None, it's kinda hacky I know) once all users have started (after all users have completed their on_start method).

Command line

locust --worker

Locustfile contents

@events.spawning_complete.add_listener
def on_spawning_complete(user_count):
    print("SPAWN", user_count)

Python version

3.11

Locust version

2.24.1

Operating system

Linux CentOS

@NicoAdrian NicoAdrian added the bug label Apr 9, 2024
@cyberw
Copy link
Collaborator

cyberw commented Apr 9, 2024

It is kind of a bug. I think the issue was introduced in 2.0. It could be reimplemented on master side, but workers dont really know when spawning has finished so it will be hard to make it work the way it used to.

@NicoAdrian
Copy link
Author

NicoAdrian commented Apr 9, 2024

@cyberw the event is correctly called on master side, when all users have spawned.

So I found a kinda ugly workaround:

  1. On the init event, I register a message for workers (the callback passed will disable decompression) AND I keep a global reference to the environment object because environment is not passed in the spawning_completeevent.
  2. On the spawning_complete event, I check if the runner is an instance of MasterRunner. If so, I sleep like 10s (to wait for all on_start method have completed), and then I send the message so the workers receive it and disable decompression

@NicoAdrian
Copy link
Author

(Would be great if all the events receive the environment object, could be usefull)

@cyberw
Copy link
Collaborator

cyberw commented Apr 9, 2024

Well, when you say it like that, implementing spawning_completed on workers based on a message from master should be pretty easy :)

If you feel like contributung this then a PR would be most welcome, if not lets keep this ticket open for later.

@cyberw cyberw reopened this Apr 10, 2024
@NicoAdrian
Copy link
Author

@cyberw Sorry I read let's NOT keep it open 😊

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

No branches or pull requests

2 participants