Skip to content

Commit 24c200c

Browse files
modbus_server: call execute in a way that those can be either coroutines or normal methods (#2139)
1 parent a2e82d5 commit 24c200c

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

pymodbus/server/async_io.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def __init__(self, owner):
4949
self.receive_queue: asyncio.Queue = asyncio.Queue()
5050
self.handler_task = None # coroutine to be run on asyncio loop
5151
self.framer: ModbusFramer
52+
self.loop = asyncio.get_running_loop()
5253

5354
def _log_exception(self):
5455
"""Show log exception."""
@@ -173,6 +174,9 @@ def execute(self, request, *addr):
173174
if self.server.request_tracer:
174175
self.server.request_tracer(request, *addr)
175176

177+
asyncio.run_coroutine_threadsafe(self._async_execute(request, *addr), self.loop)
178+
179+
async def _async_execute(self, request, *addr):
176180
broadcast = False
177181
try:
178182
if self.server.broadcast_enable and not request.slave_id:
@@ -181,9 +185,17 @@ def execute(self, request, *addr):
181185
# note response will be ignored
182186
for slave_id in self.server.context.slaves():
183187
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
184191
else:
185192
context = self.server.context[request.slave_id]
186193
response = request.execute(context)
194+
195+
# Temporary check while we move execute to async method
196+
if asyncio.iscoroutine(response):
197+
response = await response
198+
187199
except NoSuchSlaveException:
188200
Log.error("requested slave does not exist: {}", request.slave_id)
189201
if self.server.ignore_missing_slaves:

0 commit comments

Comments
 (0)