简体   繁体   English

Python WebSockets 使用 socketio 和 eventlet - 无法杀死 eventlet 服务器

[英]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.我将 websocket 服务器添加到我的应用程序中,以便它可以与基于 Web 的版本进行通信。 For this I am using eventlet.为此,我正在使用 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:目前,我有一个 singleton class 和一个 function 开始监听:

    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.但是在我结束 python 进程上的任务之前,我找不到任何东西会杀死这个服务器。

The eventlet web server does not currently have a good stop mechanism when using WebSocket. eventlet web 服务器当前在使用 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).警告:此时 server() 将始终等待活动连接完成后再退出,即使其中引发了异常(所有异常都以相同的方式处理,包括 greenlet.GreenletExit 和继承自 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.虽然这通常不是问题,但当涉及到长时间运行的 HTTP 连接(如 eventlet.websocket)时,它会出现问题,并且在运行服务器的线程上调用 wait() 可能会挂起,即使在使用 kill() 之后也是如此,因为只要有活动的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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