How to fix error "'WorkerNodes' object has no attribute 'encode'" while runnin

admin2025-04-18  4

I am trying to do a distributed load generation and coordinate data between workers. There is an example of communicating across nodes from official Locust docs: .py

But locust fails with the following error in master-locust:

[2025-01-29 11:50:24,427] f3d0cd3a1386/INFO/locust.runners: Sending spawn jobs of 1 users at 1.00 spawn rate to 1 ready workers
[2025-01-29 11:50:24,428] f3d0cd3a1386/INFO/locust.util.exception_handler: Exception found on retry 1: -- retry after 1s
[2025-01-29 11:50:24,428] f3d0cd3a1386/ERROR/locust.util.exception_handler: 'WorkerNodes' object has no attribute 'encode'
Traceback (most recent call last):
  File "/opt/venv/lib/python3.12/site-packages/locust/util/exception_handler.py", line 13, in wrapper
    return function(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/locust/rpc/zmqrpc.py", line 34, in send_to_client
    self.socket.send_multipart([msg.node_id.encode(), msg.serialize()])
                                ^^^^^^^^^^^^^^^^^^
AttributeError: 'WorkerNodes' object has no attribute 'encode'
[2025-01-29 11:50:25,431] f3d0cd3a1386/INFO/locust.util.exception_handler: Exception found on retry 2: -- retry after 3s
[2025-01-29 11:50:25,432] f3d0cd3a1386/ERROR/locust.util.exception_handler: 'WorkerNodes' object has no attribute 'encode'
Traceback (most recent call last):
  File "/opt/venv/lib/python3.12/site-packages/locust/util/exception_handler.py", line 13, in wrapper
    return function(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/locust/rpc/zmqrpc.py", line 34, in send_to_client
    self.socket.send_multipart([msg.node_id.encode(), msg.serialize()])
                                ^^^^^^^^^^^^^^^^^^
AttributeError: 'WorkerNodes' object has no attribute 'encode'
[2025-01-29 11:50:28,434] f3d0cd3a1386/INFO/locust.util.exception_handler: Exception found on retry 3: -- retry after 5s
[2025-01-29 11:50:28,435] f3d0cd3a1386/ERROR/locust.util.exception_handler: 'WorkerNodes' object has no attribute 'encode'
Traceback (most recent call last):
  File "/opt/venv/lib/python3.12/site-packages/locust/util/exception_handler.py", line 13, in wrapper
    return function(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/locust/rpc/zmqrpc.py", line 34, in send_to_client
    self.socket.send_multipart([msg.node_id.encode(), msg.serialize()])
                                ^^^^^^^^^^^^^^^^^^
AttributeError: 'WorkerNodes' object has no attribute 'encode'
[2025-01-29 11:50:33,437] f3d0cd3a1386/INFO/locust.util.exception_handler: Retry failed after 3 times.
[2025-01-29 11:50:33,442] f3d0cd3a1386/ERROR/root: Uncaught exception in event handler: 
Traceback (most recent call last):
  File "/opt/venv/lib/python3.12/site-packages/locust/event.py", line 47, in fire
    handler(**kwargs)
  File "/mnt/locust/locustfile.py", line 65, in on_test_start
    environment.runner.send_message("test_users", data, worker)
  File "/opt/venv/lib/python3.12/site-packages/locust/runners.py", line 1185, in send_message
    self.server.send_to_client(Message(msg_type, data, client_id))
  File "/opt/venv/lib/python3.12/site-packages/locust/util/exception_handler.py", line 13, in wrapper
    return function(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/locust/rpc/zmqrpc.py", line 34, in send_to_client
    self.socket.send_multipart([msg.node_id.encode(), msg.serialize()])
                                ^^^^^^^^^^^^^^^^^^
AttributeError: 'WorkerNodes' object has no attribute 'encode'

[2025-01-29 11:50:34,444] f3d0cd3a1386/INFO/locust.runners: Spawning is complete and report waittime is expired, but not all reports received from workers: {"WebsiteUser": 0} (0 total users)

Seems like the problem appears here:

environment.runner.send_message("test_users", data, worker)

because after excluding 'worker' from parameters the error disappears. But I need to send different data to workers.

Does anyeone have any ideas how to fix this code?

I am trying to do a distributed load generation and coordinate data between workers. There is an example of communicating across nodes from official Locust docs: https://github.com/locustio/locust/blob/master/examples/custom_messages.py

But locust fails with the following error in master-locust:

[2025-01-29 11:50:24,427] f3d0cd3a1386/INFO/locust.runners: Sending spawn jobs of 1 users at 1.00 spawn rate to 1 ready workers
[2025-01-29 11:50:24,428] f3d0cd3a1386/INFO/locust.util.exception_handler: Exception found on retry 1: -- retry after 1s
[2025-01-29 11:50:24,428] f3d0cd3a1386/ERROR/locust.util.exception_handler: 'WorkerNodes' object has no attribute 'encode'
Traceback (most recent call last):
  File "/opt/venv/lib/python3.12/site-packages/locust/util/exception_handler.py", line 13, in wrapper
    return function(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/locust/rpc/zmqrpc.py", line 34, in send_to_client
    self.socket.send_multipart([msg.node_id.encode(), msg.serialize()])
                                ^^^^^^^^^^^^^^^^^^
AttributeError: 'WorkerNodes' object has no attribute 'encode'
[2025-01-29 11:50:25,431] f3d0cd3a1386/INFO/locust.util.exception_handler: Exception found on retry 2: -- retry after 3s
[2025-01-29 11:50:25,432] f3d0cd3a1386/ERROR/locust.util.exception_handler: 'WorkerNodes' object has no attribute 'encode'
Traceback (most recent call last):
  File "/opt/venv/lib/python3.12/site-packages/locust/util/exception_handler.py", line 13, in wrapper
    return function(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/locust/rpc/zmqrpc.py", line 34, in send_to_client
    self.socket.send_multipart([msg.node_id.encode(), msg.serialize()])
                                ^^^^^^^^^^^^^^^^^^
AttributeError: 'WorkerNodes' object has no attribute 'encode'
[2025-01-29 11:50:28,434] f3d0cd3a1386/INFO/locust.util.exception_handler: Exception found on retry 3: -- retry after 5s
[2025-01-29 11:50:28,435] f3d0cd3a1386/ERROR/locust.util.exception_handler: 'WorkerNodes' object has no attribute 'encode'
Traceback (most recent call last):
  File "/opt/venv/lib/python3.12/site-packages/locust/util/exception_handler.py", line 13, in wrapper
    return function(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/locust/rpc/zmqrpc.py", line 34, in send_to_client
    self.socket.send_multipart([msg.node_id.encode(), msg.serialize()])
                                ^^^^^^^^^^^^^^^^^^
AttributeError: 'WorkerNodes' object has no attribute 'encode'
[2025-01-29 11:50:33,437] f3d0cd3a1386/INFO/locust.util.exception_handler: Retry failed after 3 times.
[2025-01-29 11:50:33,442] f3d0cd3a1386/ERROR/root: Uncaught exception in event handler: 
Traceback (most recent call last):
  File "/opt/venv/lib/python3.12/site-packages/locust/event.py", line 47, in fire
    handler(**kwargs)
  File "/mnt/locust/locustfile.py", line 65, in on_test_start
    environment.runner.send_message("test_users", data, worker)
  File "/opt/venv/lib/python3.12/site-packages/locust/runners.py", line 1185, in send_message
    self.server.send_to_client(Message(msg_type, data, client_id))
  File "/opt/venv/lib/python3.12/site-packages/locust/util/exception_handler.py", line 13, in wrapper
    return function(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/locust/rpc/zmqrpc.py", line 34, in send_to_client
    self.socket.send_multipart([msg.node_id.encode(), msg.serialize()])
                                ^^^^^^^^^^^^^^^^^^
AttributeError: 'WorkerNodes' object has no attribute 'encode'

[2025-01-29 11:50:34,444] f3d0cd3a1386/INFO/locust.runners: Spawning is complete and report waittime is expired, but not all reports received from workers: {"WebsiteUser": 0} (0 total users)

Seems like the problem appears here:

environment.runner.send_message("test_users", data, worker)

because after excluding 'worker' from parameters the error disappears. But I need to send different data to workers.

Does anyeone have any ideas how to fix this code?

Share Improve this question edited Jan 29 at 13:41 Yako asked Jan 29 at 12:18 YakoYako 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

The worker parameter is supposed to be the string identifier of the worker. You can get this by using WorkerNode.id.

You seem to be passing an object of type WorkerNodes?

转载请注明原文地址:http://www.anycun.com/QandA/1744966473a90146.html