Skip to content

Commit 90c28a6

Browse files
committed
update close().
1 parent 72e5a0c commit 90c28a6

File tree

9 files changed

+22
-24
lines changed

9 files changed

+22
-24
lines changed

examples/client_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ async def run_async_client(client, modbus_calls=None):
124124
assert client.connected
125125
if modbus_calls:
126126
await modbus_calls(client)
127-
await client.close()
127+
client.close()
128128
_logger.info("### End of Program")
129129

130130

pymodbus/client/base.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ def connection_lost(self, reason):
213213
if hasattr(self.transport, "_sock"):
214214
self.transport._sock.close() # pylint: disable=protected-access
215215
self.transport = None
216-
self.loop.call_soon(self.close, self)
216+
x = self.delay_ms
217+
self.close()
218+
self.delay_ms = x
217219

218220
Log.info("Protocol lost connection.")
219221
self._launch_reconnect()
@@ -261,7 +263,7 @@ def _build_response(self, tid):
261263
self.transaction.addTransaction(my_future, tid)
262264
return my_future
263265

264-
async def close(self, reconnect: bool = False) -> None:
266+
def close(self, reconnect: bool = False) -> None:
265267
"""Close connection.
266268
267269
:param reconnect: (default false), try to reconnect
@@ -272,19 +274,15 @@ async def close(self, reconnect: bool = False) -> None:
272274
self.transport.abort()
273275
self.transport.close()
274276
self.transport = None
275-
await asyncio.sleep(0.1)
276-
277277
if self._reconnect_task:
278278
self._reconnect_task.cancel()
279279
self._reconnect_task = None
280-
await asyncio.sleep(0.1)
281280

282281
if not reconnect:
283282
self.delay_ms = 0
284283
return
285284

286285
self._launch_reconnect()
287-
await asyncio.sleep(0.1)
288286

289287
def _launch_reconnect(self):
290288
"""Launch delayed reconnection coroutine"""
@@ -367,7 +365,7 @@ def __exit__(self, klass, value, traceback):
367365

368366
async def __aexit__(self, klass, value, traceback):
369367
"""Implement the client with exit block."""
370-
await self.close()
368+
self.close()
371369

372370
def __str__(self):
373371
"""Build a string representation of the connection.

pymodbus/client/serial.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async def run():
4242
4343
await client.connect()
4444
...
45-
await client.close()
45+
client.close()
4646
"""
4747

4848
transport = None
@@ -100,7 +100,7 @@ async def connect(self): # pylint: disable=invalid-overridden-method
100100
Log.info("Connected to {}", self.params.port)
101101
except Exception as exc: # pylint: disable=broad-except
102102
Log.warning("Failed to connect: {}", exc)
103-
await self.close(reconnect=True)
103+
self.close(reconnect=True)
104104
return self.connected
105105

106106

pymodbus/client/tcp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async def run():
3434
3535
await client.connect()
3636
...
37-
await client.close()
37+
client.close()
3838
"""
3939

4040
def __init__(
@@ -95,7 +95,7 @@ async def _connect(self):
9595
)
9696
except Exception as exc: # pylint: disable=broad-except
9797
Log.warning("Failed to connect: {}", exc)
98-
await self.close(reconnect=True)
98+
self.close(reconnect=True)
9999
else:
100100
Log.info("Connected to {}:{}.", self.params.host, self.params.port)
101101
self.reset_delay()

pymodbus/client/tls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ async def run():
6666
6767
await client.connect()
6868
...
69-
await client.close()
69+
client.close()
7070
"""
7171

7272
def __init__(
@@ -104,7 +104,7 @@ async def _connect(self):
104104
)
105105
except Exception as exc: # pylint: disable=broad-except
106106
Log.warning("Failed to connect: {}", exc)
107-
await self.close(reconnect=True)
107+
self.close(reconnect=True)
108108
return
109109
Log.info("Connected to {}:{}.", self.params.host, self.params.port)
110110
self.reset_delay()

pymodbus/client/udp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async def run():
3737
3838
await client.connect()
3939
...
40-
await client.close()
40+
client.close()
4141
"""
4242

4343
def __init__(
@@ -95,7 +95,7 @@ async def _connect(self):
9595
return endpoint
9696
except Exception as exc: # pylint: disable=broad-except
9797
Log.warning("Failed to connect: {}", exc)
98-
await self.close(reconnect=True)
98+
self.close(reconnect=True)
9999

100100

101101
class ModbusUdpClient(ModbusBaseClient):

pymodbus/transport/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def send(self, data: bytes) -> bool:
9191
Log.debug("send: {}", data, ":hex")
9292
return False
9393

94-
async def close(self) -> None:
94+
def close(self) -> None:
9595
"""Close the underlying connection."""
9696

9797
# ----------------------------------------------------------------------- #
@@ -103,7 +103,7 @@ async def __aenter__(self):
103103

104104
async def __aexit__(self, _class, _value, _traceback) -> None:
105105
"""Implement the client with async exit block."""
106-
await self.close()
106+
self.close()
107107

108108
def __str__(self) -> str:
109109
"""Build a string representation of the connection."""

test/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ async def test_client_connection_lost():
320320
assert not client.connected
321321
client.connection_lost(mock.sentinel.PROTOCOL)
322322
assert not client.connected
323-
await client.close()
323+
client.close()
324324

325325

326326
async def test_client_base_async():

test/test_server_task.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ async def test_async_task_no_server(comm):
153153
await asyncio.sleep(0.1)
154154
with pytest.raises((asyncio.exceptions.TimeoutError, ConnectionException)):
155155
await client.read_coils(1, 1, slave=0x01)
156-
await client.close()
156+
client.close()
157157

158158

159159
@pytest.mark.xdist_group(name="server_serialize")
@@ -171,7 +171,7 @@ async def test_async_task_ok(comm):
171171
rr = await client.read_coils(1, 1, slave=0x01)
172172
assert len(rr.bits) == 8
173173

174-
await client.close()
174+
client.close()
175175
await asyncio.sleep(0.1)
176176
assert not client.transport
177177
await server.ServerAsyncStop()
@@ -194,7 +194,7 @@ async def test_async_task_reuse(comm):
194194
rr = await client.read_coils(1, 1, slave=0x01)
195195
assert len(rr.bits) == 8
196196

197-
await client.close()
197+
client.close()
198198
await asyncio.sleep(0.1)
199199
assert not client.transport
200200

@@ -204,7 +204,7 @@ async def test_async_task_reuse(comm):
204204
rr = await client.read_coils(1, 1, slave=0x01)
205205
assert len(rr.bits) == 8
206206

207-
await client.close()
207+
client.close()
208208
await asyncio.sleep(0.1)
209209
assert not client.transport
210210

@@ -254,7 +254,7 @@ async def test_async_task_server_stop(comm):
254254
rr = await client.read_coils(1, 1, slave=0x01)
255255
assert len(rr.bits) == 8
256256

257-
await client.close()
257+
client.close()
258258
await asyncio.sleep(0.5)
259259
assert not client.transport
260260
await server.ServerAsyncStop()

0 commit comments

Comments
 (0)