Skip to content

Commit 1872ffb

Browse files
committed
Fix #640 asyncio client timeout fix
1 parent 9097aa1 commit 1872ffb

File tree

6 files changed

+29
-26
lines changed

6 files changed

+29
-26
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
version 2.5.3
22
----------------------------------------------------------
33
* Fix retries on tcp client failing randomly.
4+
* Fix Asyncio client timeout arg not being used.
45

56
version 2.5.2
67
----------------------------------------------------------

pymodbus/client/asynchronous/async_io/__init__.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class ReconnectingAsyncioModbusTcpClient(object):
234234
#: Maximum delay in milli seconds before reconnect is attempted.
235235
DELAY_MAX_MS = 1000 * 60 * 5
236236

237-
def __init__(self, protocol_class=None, loop=None):
237+
def __init__(self, protocol_class=None, loop=None, **kwargs):
238238
"""
239239
Initialize ReconnectingAsyncioModbusTcpClient
240240
:param protocol_class: Protocol used to talk to modbus device.
@@ -251,6 +251,7 @@ def __init__(self, protocol_class=None, loop=None):
251251
self.connected = False
252252
#: Reconnect delay in milli seconds.
253253
self.delay_ms = self.DELAY_MIN_MS
254+
self._proto_args = kwargs
254255

255256
def reset_delay(self):
256257
"""
@@ -291,7 +292,7 @@ def _create_protocol(self):
291292
"""
292293
Factory function to create initialized protocol instance.
293294
"""
294-
protocol = self.protocol_class()
295+
protocol = self.protocol_class(**self._proto_args)
295296
protocol.factory = self
296297
return protocol
297298

@@ -350,7 +351,7 @@ def _reconnect(self):
350351
class AsyncioModbusTcpClient(object):
351352
"""Client to connect to modbus device over TCP/IP."""
352353

353-
def __init__(self, host=None, port=502, protocol_class=None, loop=None):
354+
def __init__(self, host=None, port=502, protocol_class=None, loop=None, **kwargs):
354355
"""
355356
Initializes Asyncio Modbus Tcp Client
356357
:param host: Host IP address
@@ -369,6 +370,7 @@ def __init__(self, host=None, port=502, protocol_class=None, loop=None):
369370
self.port = port
370371

371372
self.connected = False
373+
self._proto_args = kwargs
372374

373375
def stop(self):
374376
"""
@@ -384,7 +386,7 @@ def _create_protocol(self):
384386
"""
385387
Factory function to create initialized protocol instance.
386388
"""
387-
protocol = self.protocol_class()
389+
protocol = self.protocol_class(**self._proto_args)
388390
protocol.factory = self
389391
return protocol
390392

@@ -439,14 +441,14 @@ class ReconnectingAsyncioModbusTlsClient(ReconnectingAsyncioModbusTcpClient):
439441
"""
440442
Client to connect to modbus device repeatedly over TLS."
441443
"""
442-
def __init__(self, protocol_class=None, loop=None, framer=None):
444+
def __init__(self, protocol_class=None, loop=None, framer=None, **kwargs):
443445
"""
444446
Initialize ReconnectingAsyncioModbusTcpClient
445447
:param protocol_class: Protocol used to talk to modbus device.
446448
:param loop: Event loop to use
447449
"""
448450
self.framer = framer
449-
ReconnectingAsyncioModbusTcpClient.__init__(self, protocol_class, loop)
451+
ReconnectingAsyncioModbusTcpClient.__init__(self, protocol_class, loop, **kwargs)
450452

451453
@asyncio.coroutine
452454
def start(self, host, port=802, sslctx=None, server_hostname=None):
@@ -490,7 +492,7 @@ def _create_protocol(self):
490492
"""
491493
Factory function to create initialized protocol instance.
492494
"""
493-
protocol = self.protocol_class(framer=self.framer)
495+
protocol = self.protocol_class(framer=self.framer, **self._proto_args)
494496
protocol.transaction = FifoTransactionManager(self)
495497
protocol.factory = self
496498
return protocol
@@ -506,7 +508,7 @@ class ReconnectingAsyncioModbusUdpClient(object):
506508
#: Maximum delay in milli seconds before reconnect is attempted.
507509
DELAY_MAX_MS = 1000 * 60 * 5
508510

509-
def __init__(self, protocol_class=None, loop=None):
511+
def __init__(self, protocol_class=None, loop=None, **kwargs):
510512
"""
511513
Initializes ReconnectingAsyncioModbusUdpClient
512514
:param protocol_class: Protocol used to talk to modbus device.
@@ -523,6 +525,7 @@ def __init__(self, protocol_class=None, loop=None):
523525
self.port = 0
524526

525527
self.connected = False
528+
self._proto_args = kwargs
526529
self.reset_delay()
527530

528531
def reset_delay(self):
@@ -572,7 +575,7 @@ def _create_protocol(self, host=None, port=0):
572575
"""
573576
Factory function to create initialized protocol instance.
574577
"""
575-
protocol = self.protocol_class()
578+
protocol = self.protocol_class(**self._proto_args)
576579
protocol.host = host
577580
protocol.port = port
578581
protocol.factory = self
@@ -637,7 +640,7 @@ class AsyncioModbusUdpClient(object):
637640
Client to connect to modbus device over UDP.
638641
"""
639642

640-
def __init__(self, host=None, port=502, protocol_class=None, loop=None):
643+
def __init__(self, host=None, port=502, protocol_class=None, loop=None, **kwargs):
641644
"""
642645
Initializes Asyncio Modbus UDP Client
643646
:param host: Host IP address
@@ -656,6 +659,7 @@ def __init__(self, host=None, port=502, protocol_class=None, loop=None):
656659
self.port = port
657660

658661
self.connected = False
662+
self._proto_args = kwargs
659663

660664
def stop(self):
661665
"""
@@ -674,7 +678,7 @@ def _create_protocol(self, host=None, port=0):
674678
"""
675679
Factory function to create initialized protocol instance.
676680
"""
677-
protocol = self.protocol_class()
681+
protocol = self.protocol_class(**self._proto_args)
678682
protocol.host = host
679683
protocol.port = port
680684
protocol.factory = self
@@ -842,7 +846,7 @@ def init_tcp_client(proto_cls, loop, host, port, **kwargs):
842846
:return:
843847
"""
844848
client = ReconnectingAsyncioModbusTcpClient(protocol_class=proto_cls,
845-
loop=loop)
849+
loop=loop, **kwargs)
846850
yield from client.start(host, port)
847851
return client
848852

@@ -863,7 +867,8 @@ def init_tls_client(proto_cls, loop, host, port, sslctx=None,
863867
:return:
864868
"""
865869
client = ReconnectingAsyncioModbusTlsClient(protocol_class=proto_cls,
866-
loop=loop, framer=framer)
870+
loop=loop, framer=framer,
871+
**kwargs)
867872
yield from client.start(host, port, sslctx, server_hostname)
868873
return client
869874

@@ -880,6 +885,6 @@ def init_udp_client(proto_cls, loop, host, port, **kwargs):
880885
:return:
881886
"""
882887
client = ReconnectingAsyncioModbusUdpClient(protocol_class=proto_cls,
883-
loop=loop)
888+
loop=loop, **kwargs)
884889
yield from client.start(host, port)
885890
return client

pymodbus/client/asynchronous/factory/tcp.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ def io_loop_factory(host="127.0.0.1", port=Defaults.Port, framer=None,
7777
return protocol, future
7878

7979

80-
def async_io_factory(host="127.0.0.1", port=Defaults.Port, framer=None,
81-
source_address=None, timeout=None, **kwargs):
80+
def async_io_factory(host="127.0.0.1", port=Defaults.Port, **kwargs):
8281
"""
8382
Factory to create asyncio based asynchronous tcp clients
8483
:param host: Host IP address
@@ -95,10 +94,10 @@ def async_io_factory(host="127.0.0.1", port=Defaults.Port, framer=None,
9594
proto_cls = kwargs.get("proto_cls", None)
9695
if not loop.is_running():
9796
asyncio.set_event_loop(loop)
98-
cor = init_tcp_client(proto_cls, loop, host, port)
97+
cor = init_tcp_client(proto_cls, loop, host, port, **kwargs)
9998
client = loop.run_until_complete(asyncio.gather(cor))[0]
10099
else:
101-
cor = init_tcp_client(proto_cls, loop, host, port)
100+
cor = init_tcp_client(proto_cls, loop, host, port, **kwargs)
102101
future = asyncio.run_coroutine_threadsafe(cor, loop=loop)
103102
client = future.result()
104103

pymodbus/client/asynchronous/factory/tls.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
LOGGER = logging.getLogger(__name__)
1414

1515
def async_io_factory(host="127.0.0.1", port=Defaults.TLSPort, sslctx=None,
16-
server_hostname=None, framer=None, source_address=None,
17-
timeout=None, **kwargs):
16+
server_hostname=None, framer=None, **kwargs):
1817
"""
1918
Factory to create asyncio based asynchronous tls clients
2019
:param host: Host IP address
@@ -34,11 +33,11 @@ def async_io_factory(host="127.0.0.1", port=Defaults.TLSPort, sslctx=None,
3433
if not loop.is_running():
3534
asyncio.set_event_loop(loop)
3635
cor = init_tls_client(proto_cls, loop, host, port, sslctx, server_hostname,
37-
framer)
36+
framer, **kwargs)
3837
client = loop.run_until_complete(asyncio.gather(cor))[0]
3938
else:
4039
cor = init_tls_client(proto_cls, loop, host, port, sslctx, server_hostname,
41-
framer)
40+
framer, **kwargs)
4241
future = asyncio.run_coroutine_threadsafe(cor, loop=loop)
4342
client = future.result()
4443

pymodbus/client/asynchronous/factory/udp.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ def io_loop_factory(host="127.0.0.1", port=Defaults.Port, framer=None,
5252
return protocol, future
5353

5454

55-
def async_io_factory(host="127.0.0.1", port=Defaults.Port, framer=None,
56-
source_address=None, timeout=None, **kwargs):
55+
def async_io_factory(host="127.0.0.1", port=Defaults.Port, **kwargs):
5756
"""
5857
Factory to create asyncio based asynchronous udp clients
5958
:param host: Host IP address
@@ -68,7 +67,7 @@ def async_io_factory(host="127.0.0.1", port=Defaults.Port, framer=None,
6867
from pymodbus.client.asynchronous.async_io import init_udp_client
6968
loop = kwargs.get("loop") or asyncio.get_event_loop()
7069
proto_cls = kwargs.get("proto_cls", None)
71-
cor = init_udp_client(proto_cls, loop, host, port)
70+
cor = init_udp_client(proto_cls, loop, host, port, **kwargs)
7271
if not loop.is_running():
7372
client = loop.run_until_complete(asyncio.gather(cor))[0]
7473
else:

pymodbus/client/asynchronous/mixins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __init__(self, host="127.0.0.1", port=Defaults.Port, framer=None,
5555
self.host = host
5656
self.port = port
5757
self.source_address = source_address or ("", 0)
58-
self.timeout = timeout if timeout is not None else Defaults.Timeout
58+
self._timeout = timeout if timeout is not None else Defaults.Timeout
5959

6060

6161
class AsyncModbusSerialClientMixin(BaseAsyncModbusClient):

0 commit comments

Comments
 (0)