Skip to content

Commit cf3dc47

Browse files
committed
Simplify code flow for broadcast requests
1 parent b9731ec commit cf3dc47

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

pymodbus/server/requesthandler.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,21 @@ def handle_later(self):
8686

8787
async def handle_request(self):
8888
"""Handle request."""
89-
broadcast = False
9089
if not self.last_pdu:
9190
return
9291
try:
9392
if self.server.broadcast_enable and not self.last_pdu.dev_id:
94-
broadcast = True
9593
# if broadcasting then execute on all device contexts,
9694
# note response will be ignored
97-
for dev_id in self.server.context.device_id():
98-
response = await self.last_pdu.update_datastore(self.server.context[dev_id])
99-
else:
100-
context = self.server.context[self.last_pdu.dev_id]
101-
response = await self.last_pdu.update_datastore(context)
95+
await asyncio.gather(*[
96+
self.last_pdu.update_datastore(self.server.context[dev_id])
97+
for dev_id in self.server.context.device_id()
98+
])
99+
100+
return
101+
102+
context = self.server.context[self.last_pdu.dev_id]
103+
response = await self.last_pdu.update_datastore(context)
102104

103105
except NoSuchIdException:
104106
Log.error("requested device id does not exist: {}", self.last_pdu.dev_id)
@@ -112,11 +114,9 @@ async def handle_request(self):
112114
traceback.format_exc(),
113115
)
114116
response = ExceptionResponse(self.last_pdu.function_code, ExceptionResponse.DEVICE_FAILURE)
115-
# no response when broadcasting
116-
if not broadcast:
117-
response.transaction_id = self.last_pdu.transaction_id
118-
response.dev_id = self.last_pdu.dev_id
119-
self.server_send(response, self.last_addr)
117+
response.transaction_id = self.last_pdu.transaction_id
118+
response.dev_id = self.last_pdu.dev_id
119+
self.server_send(response, self.last_addr)
120120

121121
def server_send(self, pdu, addr):
122122
"""Send message."""

0 commit comments

Comments
 (0)