Skip to content

Commit

Permalink
modbus_server: call execute in a way that those can be either corouti…
Browse files Browse the repository at this point in the history
…nes or normal methods (#2139)
  • Loading branch information
ilkka-ollakka authored and janiversen committed Jun 18, 2024
1 parent 66d3d64 commit 0f12581
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pymodbus/server/async_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(self, owner):
self.receive_queue: asyncio.Queue = asyncio.Queue()
self.handler_task = None # coroutine to be run on asyncio loop
self.framer: ModbusFramer
self.loop = asyncio.get_running_loop()

def _log_exception(self):
"""Show log exception."""
Expand Down Expand Up @@ -173,6 +174,9 @@ def execute(self, request, *addr):
if self.server.request_tracer:
self.server.request_tracer(request, *addr)

asyncio.run_coroutine_threadsafe(self._async_execute(request, *addr), self.loop)

async def _async_execute(self, request, *addr):
broadcast = False
try:
if self.server.broadcast_enable and not request.slave_id:
Expand All @@ -181,9 +185,17 @@ def execute(self, request, *addr):
# note response will be ignored
for slave_id in self.server.context.slaves():
response = request.execute(self.server.context[slave_id])
# Temporary check while we move execute to async method
if asyncio.iscoroutine(response):
response = await response
else:
context = self.server.context[request.slave_id]
response = request.execute(context)

# Temporary check while we move execute to async method
if asyncio.iscoroutine(response):
response = await response

except NoSuchSlaveException:
Log.error("requested slave does not exist: {}", request.slave_id)
if self.server.ignore_missing_slaves:
Expand Down

0 comments on commit 0f12581

Please sign in to comment.