Skip to content

Commit

Permalink
Distributor: dont block locust's client listener loop while iterating.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberw committed Dec 19, 2023
1 parent 1ea5a29 commit 6da5bf2
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions locust_plugins/distributor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from gevent.event import AsyncResult
import greenlet
import gevent
from locust.env import Environment
from locust.runners import WorkerRunner

Expand All @@ -23,12 +24,8 @@ def __init__(self, environment: Environment, iterator: Optional[Iterator], name=
if self.runner:
# received on master
def _distributor_request(environment: Environment, msg, **kwargs):
item = next(self.iterator)
self.runner.send_message(
f"_{name}_response",
{"item": item, "gid": msg.data["gid"]},
client_id=msg.data["client_id"],
)
# do this in the background to avoid blocking locust's client_listener loop
gevent.spawn(self._master_next_and_send, msg.data["gid"], msg.data["client_id"])

# received on worker
def _distributor_response(environment: Environment, msg, **kwargs):
Expand All @@ -37,6 +34,14 @@ def _distributor_response(environment: Environment, msg, **kwargs):
self.runner.register_message(f"_{name}_request", _distributor_request)
self.runner.register_message(f"_{name}_response", _distributor_response)

def _master_next_and_send(self, gid, client_id):
item = next(self.iterator)
self.runner.send_message(
f"_{self.name}_response",
{"item": item, "gid": gid},
client_id=client_id,
)

def __next__(self):
"""Get the next data dict from iterator
Expand Down

0 comments on commit 6da5bf2

Please sign in to comment.