Skip to content

Commit 9c4466b

Browse files
Request/Response: change execute to be async method (#2142)
1 parent d1aed6f commit 9c4466b

19 files changed

+112
-119
lines changed

pymodbus/pdu/bit_read_message.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def __init__(self, address=None, count=None, slave=0, **kwargs):
155155
"""
156156
ReadBitsRequestBase.__init__(self, address, count, slave, **kwargs)
157157

158-
def execute(self, context):
158+
async def execute(self, context):
159159
"""Run a read coils request against a datastore.
160160
161161
Before running the request, we make sure that the request is in
@@ -223,7 +223,7 @@ def __init__(self, address=None, count=None, slave=0, **kwargs):
223223
"""
224224
ReadBitsRequestBase.__init__(self, address, count, slave, **kwargs)
225225

226-
def execute(self, context):
226+
async def execute(self, context):
227227
"""Run a read discrete input request against a datastore.
228228
229229
Before running the request, we make sure that the request is in

pymodbus/pdu/bit_write_message.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def decode(self, data):
7979
self.address, value = struct.unpack(">HH", data)
8080
self.value = value == ModbusStatus.ON
8181

82-
def execute(self, context):
82+
async def execute(self, context):
8383
"""Run a write coil request against a datastore.
8484
8585
:param context: The datastore to request from
@@ -213,7 +213,7 @@ def decode(self, data):
213213
values = unpack_bitstring(data[5:])
214214
self.values = values[:count]
215215

216-
def execute(self, context):
216+
async def execute(self, context):
217217
"""Run a write coils request against a datastore.
218218
219219
:param context: The datastore to request from

pymodbus/pdu/diag_message.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def __init__(self, data=0x0000, **kwargs):
199199
DiagnosticStatusRequest.__init__(self, **kwargs)
200200
self.message = data
201201

202-
def execute(self, *args):
202+
async def execute(self, *args):
203203
"""Raise if not implemented."""
204204
raise NotImplementedException("Diagnostic Message Has No Execute Method")
205205

@@ -245,7 +245,7 @@ def __init__(self, message=b"\x00\x00", slave=None, **kwargs):
245245
raise ModbusException(f"message({type(message)}) must be bytes")
246246
self.message = message
247247

248-
def execute(self, *_args):
248+
async def execute(self, *_args):
249249
"""Execute the loopback request (builds the response).
250250
251251
:returns: The populated loopback response message
@@ -301,7 +301,7 @@ def __init__(self, toggle=False, slave=None, **kwargs):
301301
else:
302302
self.message = [ModbusStatus.OFF]
303303

304-
def execute(self, *_args):
304+
async def execute(self, *_args):
305305
"""Clear event log and restart.
306306
307307
:returns: The initialized response message
@@ -343,7 +343,7 @@ class ReturnDiagnosticRegisterRequest(DiagnosticStatusSimpleRequest):
343343

344344
sub_function_code = 0x0002
345345

346-
def execute(self, *args):
346+
async def execute(self, *args):
347347
"""Execute the diagnostic request on the given device.
348348
349349
:returns: The initialized response message
@@ -377,7 +377,7 @@ class ChangeAsciiInputDelimiterRequest(DiagnosticStatusSimpleRequest):
377377

378378
sub_function_code = 0x0003
379379

380-
def execute(self, *args):
380+
async def execute(self, *args):
381381
"""Execute the diagnostic request on the given device.
382382
383383
:returns: The initialized response message
@@ -412,7 +412,7 @@ class ForceListenOnlyModeRequest(DiagnosticStatusSimpleRequest):
412412

413413
sub_function_code = 0x0004
414414

415-
def execute(self, *args):
415+
async def execute(self, *args):
416416
"""Execute the diagnostic request on the given device.
417417
418418
:returns: The initialized response message
@@ -451,7 +451,7 @@ class ClearCountersRequest(DiagnosticStatusSimpleRequest):
451451

452452
sub_function_code = 0x000A
453453

454-
def execute(self, *args):
454+
async def execute(self, *args):
455455
"""Execute the diagnostic request on the given device.
456456
457457
:returns: The initialized response message
@@ -482,7 +482,7 @@ class ReturnBusMessageCountRequest(DiagnosticStatusSimpleRequest):
482482

483483
sub_function_code = 0x000B
484484

485-
def execute(self, *args):
485+
async def execute(self, *args):
486486
"""Execute the diagnostic request on the given device.
487487
488488
:returns: The initialized response message
@@ -515,7 +515,7 @@ class ReturnBusCommunicationErrorCountRequest(DiagnosticStatusSimpleRequest):
515515

516516
sub_function_code = 0x000C
517517

518-
def execute(self, *args):
518+
async def execute(self, *args):
519519
"""Execute the diagnostic request on the given device.
520520
521521
:returns: The initialized response message
@@ -548,7 +548,7 @@ class ReturnBusExceptionErrorCountRequest(DiagnosticStatusSimpleRequest):
548548

549549
sub_function_code = 0x000D
550550

551-
def execute(self, *args):
551+
async def execute(self, *args):
552552
"""Execute the diagnostic request on the given device.
553553
554554
:returns: The initialized response message
@@ -581,7 +581,7 @@ class ReturnSlaveMessageCountRequest(DiagnosticStatusSimpleRequest):
581581

582582
sub_function_code = 0x000E
583583

584-
def execute(self, *args):
584+
async def execute(self, *args):
585585
"""Execute the diagnostic request on the given device.
586586
587587
:returns: The initialized response message
@@ -614,7 +614,7 @@ class ReturnSlaveNoResponseCountRequest(DiagnosticStatusSimpleRequest):
614614

615615
sub_function_code = 0x000F
616616

617-
def execute(self, *args):
617+
async def execute(self, *args):
618618
"""Execute the diagnostic request on the given device.
619619
620620
:returns: The initialized response message
@@ -648,7 +648,7 @@ class ReturnSlaveNAKCountRequest(DiagnosticStatusSimpleRequest):
648648

649649
sub_function_code = 0x0010
650650

651-
def execute(self, *args):
651+
async def execute(self, *args):
652652
"""Execute the diagnostic request on the given device.
653653
654654
:returns: The initialized response message
@@ -682,7 +682,7 @@ class ReturnSlaveBusyCountRequest(DiagnosticStatusSimpleRequest):
682682

683683
sub_function_code = 0x0011
684684

685-
def execute(self, *args):
685+
async def execute(self, *args):
686686
"""Execute the diagnostic request on the given device.
687687
688688
:returns: The initialized response message
@@ -717,7 +717,7 @@ class ReturnSlaveBusCharacterOverrunCountRequest(DiagnosticStatusSimpleRequest):
717717

718718
sub_function_code = 0x0012
719719

720-
def execute(self, *args):
720+
async def execute(self, *args):
721721
"""Execute the diagnostic request on the given device.
722722
723723
:returns: The initialized response message
@@ -750,7 +750,7 @@ class ReturnIopOverrunCountRequest(DiagnosticStatusSimpleRequest):
750750

751751
sub_function_code = 0x0013
752752

753-
def execute(self, *args):
753+
async def execute(self, *args):
754754
"""Execute the diagnostic request on the given device.
755755
756756
:returns: The initialized response message
@@ -783,7 +783,7 @@ class ClearOverrunCountRequest(DiagnosticStatusSimpleRequest):
783783

784784
sub_function_code = 0x0014
785785

786-
def execute(self, *args):
786+
async def execute(self, *args):
787787
"""Execute the diagnostic request on the given device.
788788
789789
:returns: The initialized response message
@@ -834,7 +834,7 @@ def get_response_pdu_size(self):
834834
data = 0
835835
return 1 + 2 + 2 + 2 + data
836836

837-
def execute(self, *args):
837+
async def execute(self, *args):
838838
"""Execute the diagnostic request on the given device.
839839
840840
:returns: The initialized response message

pymodbus/pdu/mei_message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def decode(self, data):
8484
params = struct.unpack(">BBB", data)
8585
self.sub_function_code, self.read_code, self.object_id = params
8686

87-
def execute(self, _context):
87+
async def execute(self, _context):
8888
"""Run a read exception status request against the store.
8989
9090
:returns: The populated response

pymodbus/pdu/other_message.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def decode(self, data):
5454
:param data: The incoming data
5555
"""
5656

57-
def execute(self, _context=None):
57+
async def execute(self, _context=None):
5858
"""Run a read exception status request against the store.
5959
6060
:returns: The populated response
@@ -159,7 +159,7 @@ def decode(self, data):
159159
:param data: The incoming data
160160
"""
161161

162-
def execute(self, _context=None):
162+
async def execute(self, _context=None):
163163
"""Run a read exception status request against the store.
164164
165165
:returns: The populated response
@@ -270,7 +270,7 @@ def decode(self, data):
270270
:param data: The incoming data
271271
"""
272272

273-
def execute(self, _context=None):
273+
async def execute(self, _context=None):
274274
"""Run a read exception status request against the store.
275275
276276
:returns: The populated response
@@ -395,7 +395,7 @@ def decode(self, data):
395395
:param data: The incoming data
396396
"""
397397

398-
def execute(self, context=None):
398+
async def execute(self, context=None):
399399
"""Run a report slave id request against the store.
400400
401401
:returns: The populated response

pymodbus/pdu/pdu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def decode(self, _data):
252252
def encode(self):
253253
"""Decode so this failure will run correctly."""
254254

255-
def execute(self, _context):
255+
async def execute(self, _context):
256256
"""Build an illegal function request error response.
257257
258258
:returns: The error response packet

pymodbus/pdu/register_read_message.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def __init__(self, address=None, count=None, slave=0, **kwargs):
139139
"""
140140
super().__init__(address, count, slave, **kwargs)
141141

142-
def execute(self, context):
142+
async def execute(self, context):
143143
"""Run a read holding request against a datastore.
144144
145145
:param context: The datastore to request from
@@ -200,7 +200,7 @@ def __init__(self, address=None, count=None, slave=0, **kwargs):
200200
"""
201201
super().__init__(address, count, slave, **kwargs)
202202

203-
def execute(self, context):
203+
async def execute(self, context):
204204
"""Run a read input request against a datastore.
205205
206206
:param context: The datastore to request from
@@ -310,7 +310,7 @@ def decode(self, data):
310310
register = struct.unpack(">H", data[i : i + 2])[0]
311311
self.write_registers.append(register)
312312

313-
def execute(self, context):
313+
async def execute(self, context):
314314
"""Run a write single register request against a datastore.
315315
316316
:param context: The datastore to request from

pymodbus/pdu/register_write_message.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def decode(self, data):
5757
"""
5858
self.address, self.value = struct.unpack(">HH", data)
5959

60-
def execute(self, context):
60+
async def execute(self, context):
6161
"""Run a write single register request against a datastore.
6262
6363
:param context: The datastore to request from
@@ -200,7 +200,7 @@ def decode(self, data):
200200
for idx in range(5, (self.count * 2) + 5, 2):
201201
self.values.append(struct.unpack(">H", data[idx : idx + 2])[0])
202202

203-
def execute(self, context):
203+
async def execute(self, context):
204204
"""Run a write single register request against a datastore.
205205
206206
:param context: The datastore to request from
@@ -321,7 +321,7 @@ def decode(self, data):
321321
"""
322322
self.address, self.and_mask, self.or_mask = struct.unpack(">HHH", data)
323323

324-
def execute(self, context):
324+
async def execute(self, context):
325325
"""Run a mask write register request against the store.
326326
327327
:param context: The datastore to request from

pymodbus/server/async_io.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,10 @@ async def _async_execute(self, request, *addr):
184184
# if broadcasting then execute on all slave contexts,
185185
# note response will be ignored
186186
for slave_id in self.server.context.slaves():
187-
response = request.execute(self.server.context[slave_id])
188-
# Temporary check while we move execute to async method
189-
if asyncio.iscoroutine(response):
190-
response = await response
187+
response = await request.execute(self.server.context[slave_id])
191188
else:
192189
context = self.server.context[request.slave_id]
193-
response = request.execute(context)
194-
195-
# Temporary check while we move execute to async method
196-
if asyncio.iscoroutine(response):
197-
response = await response
190+
response = await request.execute(context)
198191

199192
except NoSuchSlaveException:
200193
Log.error("requested slave does not exist: {}", request.slave_id)

test/sub_client/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def __init__(self, base, req, retries=0):
392392
async def delayed_resp(self):
393393
"""Send a response to a received packet."""
394394
await asyncio.sleep(0.05)
395-
resp = self.req.execute(self.ctx)
395+
resp = await self.req.execute(self.ctx)
396396
pkt = self.base.ctx.framer.buildPacket(resp)
397397
self.base.ctx.data_received(pkt)
398398

0 commit comments

Comments
 (0)