Skip to content

Commit ec10e87

Browse files
authored
Solve loop= in test. (#1004)
1 parent 0a81c92 commit ec10e87

File tree

3 files changed

+9
-43
lines changed

3 files changed

+9
-43
lines changed

examples/client_async.py

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"""
2323
import argparse
2424
import asyncio
25-
from threading import Thread
2625
import logging
2726

2827

@@ -42,7 +41,7 @@
4241
)
4342

4443

45-
async def setup_async_client(loop):
44+
async def setup_async_client():
4645
"""Run client setup."""
4746
args = get_commandline()
4847
_logger.info("### Create client object")
@@ -57,7 +56,6 @@ async def setup_async_client(loop):
5756
retry_on_empty=False, # Is an empty response a retry
5857
source_address=("localhost", 0), # bind socket to address
5958
strict=True, # use strict timing, t1.5 for Modbus RTU
60-
loop=loop,
6159
)
6260
elif args.comm == "udp":
6361
client = await AsyncModbusUDPClient(
@@ -69,7 +67,6 @@ async def setup_async_client(loop):
6967
retry_on_empty=False, # Is an empty response a retry
7068
source_address=("localhost", 0), # bind socket to address
7169
strict=True, # use strict timing, t1.5 for Modbus RTU
72-
loop=loop,
7370
)
7471
elif args.comm == "serial":
7572
client = await AsyncModbusSerialClient(
@@ -82,7 +79,6 @@ async def setup_async_client(loop):
8279
handle_local_echo=False, # Handle local echo of the USB-to-RS485 adaptor
8380
timeout=1, # waiting time for request to complete
8481
strict=True, # use strict timing, t1.5 for Modbus RTU
85-
loop=loop,
8682
)
8783
elif args.comm == "tls":
8884
client = await AsyncModbusTLSClient(
@@ -98,44 +94,16 @@ async def setup_async_client(loop):
9894
retry_on_empty=False, # Is an empty response a retry
9995
source_address=("localhost", 0), # bind socket to address
10096
strict=True, # use strict timing, t1.5 for Modbus RTU
101-
loop=loop,
10297
)
10398
return client
10499

105100

106101
async def run_async_client(modbus_calls=None):
107102
"""Run sync client."""
108103
_logger.info("### Client ready")
109-
110-
def done(future): # pylint: disable=unused-argument
111-
"""Done."""
112-
_logger.info("Done !!!")
113-
114-
def start_loop(loop):
115-
"""Start Loop"""
116-
asyncio.set_event_loop(loop)
117-
loop.run_forever()
118-
119-
loop = asyncio.new_event_loop()
120-
mythread = Thread(target=start_loop, args=[loop])
121-
mythread.daemon = True
122-
# Start the loop
123-
mythread.start()
124-
await asyncio.sleep(1)
125-
assert loop.is_running() # nosec
126-
asyncio.set_event_loop(loop)
127-
128-
client = await setup_async_client(loop)
129-
130-
# Run supplied modbus calls
104+
client = await setup_async_client()
131105
if modbus_calls:
132-
future = asyncio.run_coroutine_threadsafe(
133-
modbus_calls(client.protocol), loop=loop
134-
)
135-
future.add_done_callback(done)
136-
while not future.done():
137-
await asyncio.sleep(0.1)
138-
loop.stop()
106+
await modbus_calls(client.protocol)
139107
_logger.info("### End of Program")
140108

141109

pymodbus/client/asynchronous/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
from pymodbus.client.asynchronous.udp import AsyncModbusUDPClient as Client
1010
1111
# For asynchronous client use
12-
event_loop, client = Client(port=5020)
12+
client = Client(port=5020)
1313
"""

test/test_client_async_asyncio.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,11 @@ async def test_factory_start_success(self):
204204
async def test_factory_start_failing_and_retried(self, mock_async): # pylint: disable=unused-argument
205205
"""Test factory start failing and retried."""
206206
mock_protocol_class = mock.MagicMock()
207-
mock_loop = mock.MagicMock()
208-
mock_loop.create_connection = mock.MagicMock(
207+
loop = asyncio.get_running_loop()
208+
loop.create_connection = mock.MagicMock(
209209
side_effect=Exception("Did not work.")
210210
)
211-
client = ReconnectingAsyncioModbusTcpClient(
212-
protocol_class=mock_protocol_class, loop=mock_loop
213-
)
211+
client = ReconnectingAsyncioModbusTcpClient(protocol_class=mock_protocol_class)
214212

215213
# check whether reconnect is called upon failed connection attempt:
216214
with mock.patch(
@@ -226,10 +224,10 @@ async def test_factory_reconnect(self, mock_sleep):
226224
"""Test factory reconnect."""
227225
mock_protocol_class = mock.MagicMock()
228226
mock_sleep.side_effect = return_as_coroutine()
229-
loop = asyncio.get_event_loop()
227+
loop = asyncio.get_running_loop()
230228
loop.create_connection = mock.MagicMock(return_value=None)
231229
client = ReconnectingAsyncioModbusTcpClient(
232-
protocol_class=mock_protocol_class, loop=loop
230+
protocol_class=mock_protocol_class
233231
)
234232
client.delay_ms = 5000
235233

0 commit comments

Comments
 (0)