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.

events.init.add_listener runs after Users are created on worker

See original GitHub issue

Describe the bug

It appears that locust.User tasks are executed before all events.init.add_listener listeners are executed on the worker.

Expected behavior

locust.User tasks should execute only after all events.init.add_listener listeners have run.

Actual behavior

It appears that locust.User tasks are executed before all events.init.add_listener listeners are done.

Steps to reproduce

# start worker
python3 -m locust -f test_locustfile.py --headless --worker --master-host=<ip>
# start master
python3 -m locust -f test_locustfile.py --headless --master --expect-workers=1
@events.init.add_listener
def on_locust_init(environment, **_kwargs):
    time.sleep(2)
    log.info('Ran init')

class CustomUser(User):
    @task
    def test(self):
        log.info('Running test')

Output from worker

Spawning 1 users at the rate 1 users/s (0 users already running)...
All users spawned: CustomUser: 1 (1 total running)
Running test
Running test
...
Running test
Ran init
Starting Locust 1.4.3
Running test
...

Environment

  • OS:Linux
  • Python version: 3.7
  • Locust version: 1.4.3
  • Locust command line that you ran: given above
  • Locust file contents (anonymized if necessary): given above

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
delulucommented, May 26, 2021

The tricky thing is that the init event is fired after the initialization of runner, while in the initialization of WorkerRunner it will send a “client_ready” msg to the master right away for being ready to execute the tasks.

It broke the purpose of init event, and it seems there’s no good way to monitor the completion of all init tasks then execute tasks later.

Maybe we can add another method to decouple the emitting of “client_ready” from the initialization of WorkerRunner.

@cyberw please let me know if it looks good to you, I can add a fix.

https://github.com/locustio/locust/blob/94e16dda0aa7bc5735d5d2db13031bfe416a7ab1/locust/main.py#L238

https://github.com/locustio/locust/blob/94e16dda0aa7bc5735d5d2db13031bfe416a7ab1/locust/main.py#L303

https://github.com/locustio/locust/blob/94e16dda0aa7bc5735d5d2db13031bfe416a7ab1/locust/runners.py#L743

1reaction
jbarz1commented, Mar 17, 2021

I’ll see if I can take a crack at it next week. But if someone else wants to attempt a fix, please go ahead 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

When does code in a service worker outside of an event ...
In general, code that's outside any event handler, in the "top-level" of the service worker's global scope, will run each and every time...
Read more >
Event hooks — Locust 2.14.0 documentation
Locust comes with a number of event hooks that can be used to extend Locust ... it may be useful to do some...
Read more >
Chrome Extensions: Manage events with service workers
Manage events with service workers · Register the service worker · Initialize the extension · Set up listeners · Filter events · React...
Read more >
Handling Events :: Eloquent JavaScript
The addEventListener method is used to register such a handler. Each event has a type ( "keydown" , "focus" , and so on)...
Read more >
Scripting API: Events.UnityEvent.AddListener - Unity - Manual
Use this to add a runtime callback. Adding multiple identical listeners results in only a single call being made. //Attach this script to...
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