Skip to content

Commit aaf40a7

Browse files
authored
Merge pull request #2654 from locustio/fix-issue-with-concurrent-custom-messages-and-docs
Fix issue with concurrent custom messages and docs
2 parents 18b19e0 + 061552f commit aaf40a7

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

Diff for: docs/running-distributed.rst

+3-5
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,13 @@ the messages will simply be handled by the runner that sends them.
137137
Using the default options while registering a message handler will run the listener function
138138
in a **blocking** way, resulting in the heartbeat and other messages being delayed for the amount
139139
of the execution.
140-
If it is known that the listener function will handle time-intensive tasks, it is possible to register the
141-
function as **concurrent** (as a separate greenlet).
140+
If you think that your message handler will need to run for more than a second then you can register it
141+
as **concurrent**. Locust will then make it run in its own greenlet. Note that these greenlets will never
142+
be join():ed.
142143

143144
.. code-block::
144145
environment.runner.register_message('test_users', setup_test_users, concurrent=True)
145146
146-
Please use this feature with care, as otherwise it could result in greenlets running and influencing
147-
the running loadtest.
148-
149147
For more details, see the `complete example <https://github.com/locustio/locust/tree/master/examples/custom_messages.py>`_.
150148

151149

Diff for: examples/custom_messages.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@
99
def setup_test_users(environment, msg, **kwargs):
1010
# Fired when the worker receives a message of type 'test_users'
1111
usernames.extend(map(lambda u: u["name"], msg.data))
12-
# Even though "acknowledge_concurrent_users" was sent first, "acknowledge_users"
13-
# will print its statement first, as "acknowledge_concurrent_users" was registered
14-
# running concurrently, and therefore not blocking other messages.
15-
environment.runner.send_message("concurrent_message", "This is a non blocking message")
1612
environment.runner.send_message("acknowledge_users", f"Thanks for the {len(msg.data)} users!")
13+
environment.runner.send_message("concurrent_message", "Message to concurrent handler")
1714

1815

1916
def on_acknowledge(msg, **kwargs):
@@ -22,8 +19,9 @@ def on_acknowledge(msg, **kwargs):
2219

2320

2421
def on_concurrent_message(msg, **kwargs):
25-
gevent.sleep(10)
26-
print(msg.data)
22+
print(f"concurrent_message received with data: '{msg.data}'")
23+
gevent.sleep(10) # if this handler was run with concurrent=False it would halt the message handling loop in locust
24+
print("finished processing concurrent_message")
2725

2826

2927
@events.init.add_listener

Diff for: locust/runners.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ def client_listener(self) -> NoReturn:
11431143
if not concurrent:
11441144
listener(environment=self.environment, msg=msg)
11451145
else:
1146-
gevent.spawn(listener, self.environment, msg)
1146+
gevent.spawn(listener, environment=self.environment, msg=msg)
11471147
except Exception:
11481148
logging.error(f"Uncaught exception in handler for {msg.type}\n{traceback.format_exc()}")
11491149

0 commit comments

Comments
 (0)