From 977551d6bcdc42a705bf9bc33bcad71652885207 Mon Sep 17 00:00:00 2001 From: Josh Kelley Date: Tue, 1 Sep 2015 14:42:57 -0400 Subject: [PATCH] Lazily initialize InstallManagementConsole Simply importing the management console takes ~0.3 sec on an 1GHz Atom. Since it is (I suspect) often not used, lazily importing it should help startup for many programs. --- pymodbus/server/async.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pymodbus/server/async.py b/pymodbus/server/async.py index b21976188..2d833a313 100644 --- a/pymodbus/server/async.py +++ b/pymodbus/server/async.py @@ -17,7 +17,6 @@ from pymodbus.exceptions import NoSuchSlaveException from pymodbus.transaction import ModbusSocketFramer, ModbusAsciiFramer from pymodbus.pdu import ModbusExceptions as merror -from pymodbus.internal.ptwisted import InstallManagementConsole #---------------------------------------------------------------------------# # Logging @@ -215,7 +214,9 @@ def StartTcpServer(context, identity=None, address=None, console=False, **kwargs address = address or ("", Defaults.Port) framer = ModbusSocketFramer factory = ModbusServerFactory(context, framer, identity, **kwargs) - if console: InstallManagementConsole({'factory': factory}) + if console: + from pymodbus.internal.ptwisted import InstallManagementConsole + InstallManagementConsole({'factory': factory}) _logger.info("Starting Modbus TCP Server on %s:%s" % address) reactor.listenTCP(address[1], factory, interface=address[0]) @@ -262,7 +263,9 @@ def StartSerialServer(context, identity=None, _logger.info("Starting Modbus Serial Server on %s" % port) factory = ModbusServerFactory(context, framer, identity, **kwargs) - if console: InstallManagementConsole({'factory': factory}) + if console: + from pymodbus.internal.ptwisted import InstallManagementConsole + InstallManagementConsole({'factory': factory}) protocol = factory.buildProtocol(None) SerialPort.getHost = lambda self: port # hack for logging