Kill Thread which runs Modbus Server

392 Views Asked by At

I want to kill a Thread that runs a server via pymodbus. When I use the .join() method, the server will continue running if a client is connected.

        address = ("ip.of.my.pi", 5020)
        server = Thread(target=StartTcpServer, args=(context, identity, address))
        server.start()
        
        def threaded(context, server):
            if master.poll:
                thread = Thread(target=server_loop, args=(context,))
                thread.start()
                time.sleep(5)
                master.after(100, lambda: threaded(context, server))
            else:
                server.join()

        threaded(context, server)

The function server_loop runs a measurement, which will stop in this constellation. I cannot use a normal loop, because, as you see at master.after, I'm using Tkinter as a GUI.

1

There are 1 best solutions below

0
On

To stop a pymodbus server inside a thread, you have to start the server.join From another thread.