From f111f2e0ac623da4cf5c59ed131ef49b604e3020 Mon Sep 17 00:00:00 2001 From: rahul Date: Fri, 24 Nov 2017 16:23:03 +0530 Subject: [PATCH] New helper function to help stop Asynchronous server are being added. Calling this helper function will kill the main thread --- pymodbus/server/async.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pymodbus/server/async.py b/pymodbus/server/async.py index b72eb3b53..c64fa6d8a 100644 --- a/pymodbus/server/async.py +++ b/pymodbus/server/async.py @@ -274,9 +274,23 @@ def StartSerialServer(context, identity=None, SerialPort(protocol, port, reactor, baudrate) reactor.run() + +def _stop_server(is_mainthread=True): + from twisted.internet import reactor + if _is_main_thread(): + reactor.stop() + else: + reactor.callFromThread(reactor.stop) + _logger.debug("Stopping main thread") + + +StopTcpServer = StopSerialServer = _stop_server + + #---------------------------------------------------------------------------# # Exported symbols #---------------------------------------------------------------------------# __all__ = [ - "StartTcpServer", "StartUdpServer", "StartSerialServer", + "StartTcpServer", "StartUdpServer", "StartSerialServer", "StopTcpServer", + "StopSerialServer" ]