Skip to content Skip to sidebar Skip to footer

Closing Flask-socketio Gracefully In Python

I'm using flask-socketio for server side in python. When running on windows10 the .stop() function of the flask_socketio.SocketIO works and closes the socket which terminates my sc

Solution 1:

If you look at the documentation for the stop() function, it has the following note:

This method must be called from a HTTP or SocketIO handler function.

What this means is that you have to define an event that stops the server, and then invoke this event from a client. You can't just call the stop function from another thread like you are doing it.

Solution 2:

I think stop from another thread is possible. This is what I have done:

Define a shutdown hanlder:

@g_app.route("/shutdown", methods=['GET'])defshutdown():
    g_socket_io.stop()
    return"Shutting down..."

Call shutdown from another threaad:

import requests
    try:
        requests.get("http://localhost:9572/shutdown")
    except requests.exceptions.ConnectionError as e:
        print("Shutdown with Connection Error" + e.__str__())
    except BaseException as e:
        print("Shutdown Error " + e.__str__())

There would print a connection error message as

ShutdownwithConnectionError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))

But surely it is closed.

Post a Comment for "Closing Flask-socketio Gracefully In Python"