如何在 gunicorn 上运行 python websocket

Joh*_*ton 3 python flask

我从 SO 找到了这个 0 依赖 python websocket 服务器:
https :
//gist.github.com/jkp/3136208 我正在为我的flask应用程序使用 gunicorn,我也想使用这个 websocket 服务器gunicorn。在代码的最后几行中,它使用以下命令运行服务器:

if __name__ == "__main__":
    server = SocketServer.TCPServer(
        ("localhost", 9999), WebSocketsHandler)
    server.serve_forever()
Run Code Online (Sandbox Code Playgroud)

我不知道如何websocketserver.py让它在gunicorn. 这是因为,人们会认为你想gunicorn运行server_forever()还有SocketServer.TCPServer(...。这可能吗?

Cil*_*yan 5

Gunicorn 期望 WSGI 应用程序(PEP 333)不仅仅是一个函数。您的应用程序必须接受一个environ变量和一个start_response回调并返回一个数据迭代器(粗略地说)。SocketServer.StreamRequestHandler 封装的所有机制都在 gunicorn 端。我想将这个要点修改为 WSGI 应用程序需要做很多工作(但这会很有趣!)。

或者,也许这个库会为你完成工作:https : //github.com/CMGS/gunicorn-websocket