简体   繁体   中英

Python WebSockets using socketio and eventlet - cannot kill eventlet server

Im adding a websocket server to my app so it can communicate with a web-based version of itself. For this I am using eventlet.

The problem i am running in to is once I start the server, I cannot get it to die. Even if i close my application, the process remains running in the background while the server stays alive. I have been googling and testing random things for days now and just cannot get this to happen. Im hoping someone here can help me.

Currently, i have a singleton class with a function that starts the listening:

    def run_server(self, addr, port):
        eventlet.wsgi.server(eventlet.listen((addr, port)), self.APP)

and for info, the app is:

socketio.WSGIApp(SIO, static_files={'/': {'content_type': 'text/html',
                                                    'filename': 'index.html'}})

and then I am starting this server on a thread in the app:

def run_on_thread(addr, port):
    obj = WebSocket()
    t = threading.Thread(target=obj.run_server, args=(addr, port))
    t.setDaemon(True)
    return obj, t

The thread gets started in my application, and as mentioned - everything works fine. All my messages are sent and received.

But nothing I can find will kill this server until i End Task on the python process.

The eventlet web server does not currently have a good stop mechanism when using WebSocket. The following warning is in the documentation :

Warning: At the moment server() will always wait for active connections to finish before exiting, even if there's an exception raised inside it (all exceptions are handled the same way, including greenlet.GreenletExit and those inheriting from BaseException).

While this may not be an issue normally, when it comes to long running HTTP connections (like eventlet.websocket) it will become problematic and calling wait() on a thread that runs the server may hang, even after using kill(), as long as there are active connections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM