Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to stop server from example code? #121

Open
William-Durand opened this issue Jul 12, 2022 · 2 comments
Open

How to stop server from example code? #121

William-Durand opened this issue Jul 12, 2022 · 2 comments

Comments

@William-Durand
Copy link

This is more a question than an issue.

I try to make a modbus server in a python thread but I don't know how to stop it.
I took the exact code I found in the example part of documentation.
I set up my app ("get_server(TCPServer, self.interface, RequestHandler)") and I ran this code inside my thread:

            try:
                app.serve_forever()
            finally:
                app.shutdown()
                app.server_close()

Everything works fine except I have to stop my program manually because my thread never stop.
The question is: how to stop the "app.serve_forever()" part when I don't need my server anymore?

Just in case, I created an event called "stop_event" which can be set whenever I want to.
But I don't really know how to use it to force the server to stop.

Best regards.

@William-Durand William-Durand changed the title How to stop server from exemple code? How to stop server from example code? Jul 12, 2022
@grgmm
Copy link

grgmm commented Dec 2, 2022

Hi, I have the same issue, please somebody can help us?

@tiagocoutinho
Copy link
Contributor

Here is an example adapted from scripts/examples/simple_tcp_server.py (you will need python >= 3.6):

import time
from socketserver import TCPServer
from collections import defaultdict
from threading import Thread

from umodbus import conf
from umodbus.server.tcp import RequestHandler, get_server

# A very simple data store which maps addresses against their values.
data_store = defaultdict(int)

TCPServer.allow_reuse_address = True
app = get_server(TCPServer, ("", 5020), RequestHandler)

@app.route(slave_ids=[1], function_codes=[1, 2], addresses=list(range(0, 10)))
def read_data_store(slave_id, function_code, address):
    """" Return value of address. """
    return data_store[address]

@app.route(slave_ids=[1], function_codes=[5, 15], addresses=list(range(0, 10)))
def write_data_store(slave_id, function_code, address, value):
    """" Set value for address. """
    data_store[address] = value

def serve(): 
    with app:
        app.serve_forever()

def main():
    server = Thread(target=serve)
    server.start()
    try:
        # whatever main loop you decide to run
        while True:
            time.sleep(1)
    finally:
        app.shutdown()
        server.join()

if __name__ == "__main__":
    main()

I'm not sure if this is what you're looking for but if you press Ctrl-C it should stop the server running on the other thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants