From 28f9d29a1e94955db4a29ccd87d48eb3bcfb873b Mon Sep 17 00:00:00 2001 From: Dana Johnson Date: Fri, 5 Jun 2020 21:17:02 -0700 Subject: [PATCH 1/2] Add simple documentation with use case for init event --- docs/writing-a-locustfile.rst | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/writing-a-locustfile.rst b/docs/writing-a-locustfile.rst index f545db320d..b7b6bcfaa2 100644 --- a/docs/writing-a-locustfile.rst +++ b/docs/writing-a-locustfile.rst @@ -445,6 +445,23 @@ events. You can set up listeners for these events at the module level of your lo When running Locust distributed the ``test_start`` and ``test_stop`` events will only be fired in the master node. +init event +========== + +The ``init`` event is triggered at the beginning of each Locust process. This is especially useful in distributed mode +where each worker process (not each user) needs a chance to do some initialization. For example, let's say you have some +global state that all users spawned from this process will need: + +.. code-block:: python + + from locust import events + from locust.runners import MasterRunner + + @events.init.add_listener + def on_locust_init(environment, **kwargs): + if not isinstance(environment.runner, MasterRunner): + # do the setup work + Making HTTP requests ===================== From e601df8f5cb62720db3d68d9be6ce185d7b6a7f9 Mon Sep 17 00:00:00 2001 From: Lars Holmberg Date: Sat, 6 Jun 2020 09:57:58 +0200 Subject: [PATCH 2/2] minor improvement of init example --- docs/writing-a-locustfile.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/writing-a-locustfile.rst b/docs/writing-a-locustfile.rst index b7b6bcfaa2..90f75c9da4 100644 --- a/docs/writing-a-locustfile.rst +++ b/docs/writing-a-locustfile.rst @@ -459,8 +459,10 @@ global state that all users spawned from this process will need: @events.init.add_listener def on_locust_init(environment, **kwargs): - if not isinstance(environment.runner, MasterRunner): - # do the setup work + if isinstance(environment.runner, MasterRunner): + print("I'm on master node") + else: + print("I'm on a worker or standalone node") Making HTTP requests