Skip to content

Commit 4eccc75

Browse files
authored
Split async_io.py and simplify server start/stop. (#2528)
1 parent 46879e0 commit 4eccc75

File tree

11 files changed

+741
-714
lines changed

11 files changed

+741
-714
lines changed

examples/contrib/serial_forwarder.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pymodbus.client import ModbusSerialClient
1212
from pymodbus.datastore import ModbusServerContext
1313
from pymodbus.datastore.remote import RemoteSlaveContext
14-
from pymodbus.server.async_io import ModbusTcpServer
14+
from pymodbus.server import ModbusTcpServer
1515

1616

1717
_logger = logging.getLogger(__file__)

examples/server_async.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,15 @@ def setup_server(description=None, context=None, cmdline=None):
136136
return args
137137

138138

139-
async def run_async_server(args):
139+
async def run_async_server(args) -> None:
140140
"""Run server."""
141141
txt = f"### start ASYNC server, listening on {args.port} - {args.comm}"
142142
_logger.info(txt)
143-
server = None
144143
if args.comm == "tcp":
145144
address = (args.host if args.host else "", args.port if args.port else None)
146-
server = await StartAsyncTcpServer(
145+
await StartAsyncTcpServer(
147146
context=args.context, # Data storage
148147
identity=args.identity, # server identify
149-
# TBD host=
150-
# TBD port=
151148
address=address, # listen address
152149
# custom_functions=[], # allow custom handling
153150
framer=args.framer, # The framer strategy to use
@@ -160,7 +157,7 @@ async def run_async_server(args):
160157
args.host if args.host else "127.0.0.1",
161158
args.port if args.port else None,
162159
)
163-
server = await StartAsyncUdpServer(
160+
await StartAsyncUdpServer(
164161
context=args.context, # Data storage
165162
identity=args.identity, # server identify
166163
address=address, # listen address
@@ -173,7 +170,7 @@ async def run_async_server(args):
173170
elif args.comm == "serial":
174171
# socat -d -d PTY,link=/tmp/ptyp0,raw,echo=0,ispeed=9600
175172
# PTY,link=/tmp/ttyp0,raw,echo=0,ospeed=9600
176-
server = await StartAsyncSerialServer(
173+
await StartAsyncSerialServer(
177174
context=args.context, # Data storage
178175
identity=args.identity, # server identify
179176
# timeout=1, # waiting time for request to complete
@@ -190,9 +187,8 @@ async def run_async_server(args):
190187
)
191188
elif args.comm == "tls":
192189
address = (args.host if args.host else "", args.port if args.port else None)
193-
server = await StartAsyncTlsServer(
190+
await StartAsyncTlsServer(
194191
context=args.context, # Data storage
195-
host="localhost", # define tcp address where to connect to.
196192
# port=port, # on which port
197193
identity=args.identity, # server identify
198194
# custom_functions=[], # allow custom handling
@@ -210,10 +206,9 @@ async def run_async_server(args):
210206
# broadcast_enable=False, # treat slave 0 as broadcast address,
211207
# timeout=1, # waiting time for request to complete
212208
)
213-
return server
214209

215210

216-
async def async_helper():
211+
async def async_helper() -> None:
217212
"""Combine setup and run."""
218213
_logger.info("Starting...")
219214
run_args = setup_server(description="Run asynchronous server.")

examples/server_sync.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,15 @@
6262
_logger.setLevel("DEBUG")
6363

6464

65-
def run_sync_server(args):
65+
def run_sync_server(args) -> None:
6666
"""Run server."""
6767
txt = f"### start SYNC server, listening on {args.port} - {args.comm}"
6868
_logger.info(txt)
69-
server = None
7069
if args.comm == "tcp":
7170
address = ("", args.port) if args.port else None
72-
server = StartTcpServer(
71+
StartTcpServer(
7372
context=args.context, # Data storage
7473
identity=args.identity, # server identify
75-
# TBD host=
76-
# TBD port=
7774
address=address, # listen address
7875
# custom_functions=[], # allow custom handling
7976
framer=args.framer, # The framer strategy to use
@@ -83,7 +80,7 @@ def run_sync_server(args):
8380
)
8481
elif args.comm == "udp":
8582
address = ("127.0.0.1", args.port) if args.port else None
86-
server = StartUdpServer(
83+
StartUdpServer(
8784
context=args.context, # Data storage
8885
identity=args.identity, # server identify
8986
address=address, # listen address
@@ -96,7 +93,7 @@ def run_sync_server(args):
9693
elif args.comm == "serial":
9794
# socat -d -d PTY,link=/tmp/ptyp0,raw,echo=0,ispeed=9600
9895
# PTY,link=/tmp/ttyp0,raw,echo=0,ospeed=9600
99-
server = StartSerialServer(
96+
StartSerialServer(
10097
context=args.context, # Data storage
10198
identity=args.identity, # server identify
10299
# timeout=1, # waiting time for request to complete
@@ -113,9 +110,8 @@ def run_sync_server(args):
113110
)
114111
elif args.comm == "tls":
115112
address = ("", args.port) if args.port else None
116-
server = StartTlsServer(
113+
StartTlsServer(
117114
context=args.context, # Data storage
118-
host="localhost", # define tcp address where to connect to.
119115
# port=port, # on which port
120116
identity=args.identity, # server identify
121117
# custom_functions=[], # allow custom handling
@@ -133,14 +129,13 @@ def run_sync_server(args):
133129
# broadcast_enable=False, # treat slave 0 as broadcast address,
134130
# timeout=1, # waiting time for request to complete
135131
)
136-
return server
137132

138133

139-
def sync_helper():
134+
def sync_helper() -> None:
140135
"""Combine setup and run."""
141136
run_args = server_async.setup_server(description="Run synchronous server.")
142-
server = run_sync_server(run_args)
143-
server.shutdown()
137+
run_sync_server(run_args)
138+
# server.shutdown()
144139

145140

146141
if __name__ == "__main__":

pymodbus/server/__init__.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@
2222
"get_simulator_commandline",
2323
]
2424

25-
from pymodbus.server.async_io import (
25+
from pymodbus.server.server import (
2626
ModbusSerialServer,
2727
ModbusTcpServer,
2828
ModbusTlsServer,
2929
ModbusUdpServer,
30+
)
31+
from pymodbus.server.simulator.http_server import ModbusSimulatorServer
32+
from pymodbus.server.simulator.main import get_commandline as get_simulator_commandline
33+
from pymodbus.server.startstop import (
3034
ServerAsyncStop,
3135
ServerStop,
3236
StartAsyncSerialServer,
@@ -38,5 +42,3 @@
3842
StartTlsServer,
3943
StartUdpServer,
4044
)
41-
from pymodbus.server.simulator.http_server import ModbusSimulatorServer
42-
from pymodbus.server.simulator.main import get_commandline as get_simulator_commandline

0 commit comments

Comments
 (0)