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 (where) to close the server? #49

Open
GOKOP opened this issue Apr 23, 2023 · 3 comments
Open

How (where) to close the server? #49

GOKOP opened this issue Apr 23, 2023 · 3 comments

Comments

@GOKOP
Copy link

GOKOP commented Apr 23, 2023

I want to retrieve some information through a web server and then close it, but I don't really understand how am I supposed to do that. server.run() is blocking, so no code past this call ever executes. The only place I see where I could call server.close() is the request handler function, but that seems ugly because then it can't return a response

@cauyeung
Copy link

cauyeung commented Jun 5, 2023

I had the same issue, I solved this by adding in server.py at the top:

shutdown_paths = []

Then at the bottom of _handle_request add:

  if uri in shutdown_paths:
    logging.info("Shutting down web server")
    stop()
    close()

You can then just add to your main.py:

server.shutdown_paths = ["/shutdown"]

Or whatever path you want to shutdown the server once it finishes. Then just visit /shutdown, and it should close the web server after it displays the page content.

@optimal-system
Copy link

I manage to close the server with the above method but with that code
if uri in shutdown_paths: logging.info("Shutting down web server") uasyncio.Loop.stop() uasyncio.Loop.close()

I did not like to modify "server.py" so instead I put in my main loop
server.add_route("/shutdown", handler = self.app_shutdown, methods = ["GET"])

def app_shutdown(self,request): import asyncio print("Shutting down web server") asyncio.Loop.stop() asyncio.Loop.close() return "SERVER CLOSED"

It's working and I can put code after server.run
server.run() while True : print("The program is still running...")

The message "SERVER CLOSED" appears when I stop my progran.
To be more clean I could return to a page of confirmation with a button to go to app_shutdown

@optimal-system
Copy link

optimal-system commented May 27, 2024

I'm running a program that gathers every 15mn. During the time.sleep, the Phew server must be running to display the collected data. Thus the server must stop automatically after 14mn. I did it that way :

def get_meteo(self,request):
        stoptime = time.time() - self.starttime
        if stoptime > 60*14 :
            uasyncio.Loop.stop()
            uasyncio.Loop.close()
            uasyncio.Server.close()
            return 
        return meteo.loop()

def loop(self):
        self.wifi_networks = find_wifi()
        connect_wifi()
        self.starttime = time.time()
        server.add_route("/", handler = self.index, methods = ["GET"])
        server.add_route("/meteo", handler = self.get_meteo, methods = ["GET"])
        server.set_callback(self.catch_all)
        server.run()

The code ts not very clean because the "Task exception wasn't retrieved" but it works.
Unfortunately it works IF AND ONLY IF there is a request to the server.
Otherwise the server run forever (that's the bottom line of server.py).
I tried to modify the server.py code :
def run .......
# loop.run_forever()
loop.run_until_complete(awaitable)

But I don't understand what is the parameter to use.
It would be very nice to have a method def stopserver() :

I need help to solve that problem. Thanks in advance

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