Skip to content

Commit 1c8bbe3

Browse files
authored
Remove async factory. (#1001)
1 parent 8afab5c commit 1c8bbe3

File tree

14 files changed

+111
-276
lines changed

14 files changed

+111
-276
lines changed

doc/source/library/pymodbus.client.asynchronous.factory.rst

Lines changed: 0 additions & 36 deletions
This file was deleted.

doc/source/library/pymodbus.client.asynchronous.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Subpackages
1212
.. toctree::
1313

1414
pymodbus.client.asynchronous.async_io
15-
pymodbus.client.asynchronous.factory
1615

1716
Submodules
1817
----------

pymodbus/client/asynchronous/async_io/__init__.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ async def _connect(self):
296296
except Exception as exc: # pylint: disable=broad-except
297297
txt = f"Failed to connect: {exc}"
298298
_logger.warning(txt)
299-
asyncio.ensure_future(self._reconnect(), loop=self.loop)
299+
asyncio.ensure_future(self._reconnect())
300300
else:
301301
txt = f"Connected to {self.host}:{self.port}."
302302
_logger.info(txt)
@@ -324,7 +324,7 @@ def protocol_lost_connection(self, protocol):
324324
self.connected = False
325325
self.protocol = None
326326
if self.host:
327-
asyncio.ensure_future(self._reconnect(), loop=self.loop)
327+
asyncio.ensure_future(self._reconnect())
328328
else:
329329
_logger.error(TEST_FACTORY)
330330

@@ -384,7 +384,7 @@ async def connect(self):
384384
except Exception as exc: # pylint: disable=broad-except
385385
txt = f"Failed to connect: {exc}"
386386
_logger.warning(txt)
387-
# asyncio.asynchronous(self._reconnect(), loop=self.loop)
387+
# asyncio.asynchronous(self._reconnect())
388388

389389
def protocol_made_connection(self, protocol):
390390
"""Notify successful connection."""
@@ -408,7 +408,7 @@ def protocol_lost_connection(self, protocol):
408408
self.connected = False
409409
self.protocol = None
410410
# if self.host:
411-
# asyncio.asynchronous(self._reconnect(), loop=self.loop)
411+
# asyncio.asynchronous(self._reconnect())
412412
else:
413413
_logger.error(TEST_FACTORY)
414414

@@ -464,7 +464,7 @@ async def _connect(self):
464464
except Exception as exc: # pylint: disable=broad-except
465465
txt = f"Failed to connect: {exc}"
466466
_logger.warning(txt)
467-
asyncio.ensure_future(self._reconnect(), loop=self.loop)
467+
asyncio.ensure_future(self._reconnect())
468468
else:
469469
txt = f"Connected to {self.host}:{self.port}."
470470
_logger.info(txt)
@@ -564,7 +564,7 @@ async def _connect(self):
564564
except Exception as exc: # pylint: disable=broad-except
565565
txt = f"Failed to connect: {exc}"
566566
_logger.warning(txt)
567-
asyncio.ensure_future(self._reconnect(), loop=self.loop)
567+
asyncio.ensure_future(self._reconnect())
568568

569569
def protocol_made_connection(self, protocol):
570570
"""Notify successful connection."""
@@ -658,7 +658,7 @@ async def connect(self):
658658
except Exception as exc: # pylint: disable=broad-except
659659
txt = f"Failed to connect: {exc}"
660660
_logger.warning(txt)
661-
# asyncio.asynchronous(self._reconnect(), loop=self.loop)
661+
# asyncio.asynchronous(self._reconnect())
662662

663663
def protocol_made_connection(self, protocol):
664664
"""Protocol notification of successful connection."""
@@ -682,7 +682,7 @@ def protocol_lost_connection(self, protocol):
682682
self.connected = False
683683
self.protocol = None
684684
# if self.host:
685-
# asyncio.asynchronous(self._reconnect(), loop=self.loop)
685+
# asyncio.asynchronous(self._reconnect())
686686
else:
687687
_logger.error(TEST_FACTORY)
688688

@@ -784,7 +784,7 @@ def protocol_lost_connection(self, protocol):
784784
self._connected_event.clear()
785785
self.protocol = None
786786
# if self.host:
787-
# asyncio.asynchronous(self._reconnect(), loop=self.loop)
787+
# asyncio.asynchronous(self._reconnect())
788788
else:
789789
_logger.error(TEST_FACTORY)
790790

pymodbus/client/asynchronous/factory/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

pymodbus/client/asynchronous/factory/serial.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

pymodbus/client/asynchronous/factory/tcp.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

pymodbus/client/asynchronous/factory/tls.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

pymodbus/client/asynchronous/factory/udp.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

pymodbus/client/asynchronous/serial.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
"""SERIAL communication."""
2+
import asyncio
23
import logging
34

4-
from pymodbus.client.asynchronous.async_io import AsyncioModbusSerialClient as serialClient
5-
from pymodbus.client.asynchronous.factory.serial import async_io_factory
65
from pymodbus.factory import ClientDecoder
6+
from pymodbus.client.asynchronous.async_io import (
7+
AsyncioModbusSerialClient,
8+
ModbusClientProtocol,
9+
)
710

811
_logger = logging.getLogger(__name__)
912

1013

11-
class AsyncModbusSerialClient(serialClient):
14+
class AsyncModbusSerialClient(AsyncioModbusSerialClient):
1215
"""Actual Async Serial Client to be used.
1316
1417
To use do::
@@ -29,5 +32,18 @@ def __new__(cls, framer, port, **kwargs):
2932
:return:
3033
"""
3134
framer = framer(ClientDecoder())
32-
yieldable = async_io_factory(framer=framer, port=port, **kwargs)
33-
return yieldable
35+
try:
36+
loop = kwargs.pop("loop", None) or asyncio.get_running_loop()
37+
except RuntimeError:
38+
loop = asyncio.new_event_loop()
39+
40+
proto_cls = kwargs.get("proto_cls") or ModbusClientProtocol
41+
42+
client = AsyncioModbusSerialClient(port, proto_cls, framer, **kwargs)
43+
coro = client.connect
44+
if not loop.is_running():
45+
loop.run_until_complete(coro())
46+
else: # loop is not asyncio.get_event_loop():
47+
future = asyncio.run_coroutine_threadsafe(coro(), loop=loop)
48+
future.result()
49+
return client

0 commit comments

Comments
 (0)