0

I have a flask app that processes a web socket stream of audio from Twilio.

The app works fine without gunicorn but when I start it with gunicorn I get only the first message of the socket (connect) and an unsuccessful handshake. Here is how the app looks:

from flask import Flask
from flask_sockets import Sockets
from geventwebsocket.handler import WebSocketHandler
from gevent import pywsgi
...

app = Flask(__name__)
sockets = Sockets(app)
...
@sockets.route('/media')
def media(ws):
    ...
if __name__ == '__main__':
    server = pywsgi.WSGIServer(('', HTTP_SERVER_PORT), app, handler_class=WebSocketHandler)
    server.serve_forever()

When I start the app directly using python flaskapp.py it works ok.

When I start it using gunicorn by writing:

gunicorn -k flask_sockets.worker --bind 0.0.0.0:5055 --log-level=bug flaskapp:app

this is where the connection "hangs" and carries no further than the initial connection, apparently due to the handshake failing.

It's important to note that I haven't "gevent monkey patched" the code, but I'm not sure if it has anything to do with the problem.

Any idea will much be appreciated!

4

1 回答 1

0

现在没有能力对此进行测试,但也许可以尝试:

from flask import Flask
from flask_sockets import Sockets
from geventwebsocket.handler import WebSocketHandler
from gevent import pywsgi
...

app = Flask(__name__)
sockets = Sockets(app)
...
@sockets.route('/media')
def media(ws):
    ...

server = pywsgi.WSGIServer(('', HTTP_SERVER_PORT), app, handler_class=WebSocketHandler)

if __name__ == '__main__':
    server.serve_forever()

然后将启动命令更改为:

gunicorn -k flask_sockets.worker --bind 0.0.0.0:5055 --log-level=bug flaskapp:server

(Gunicorn 应该导入server对象,该对象不能存在于最后的if语句中,因为该代码仅在python直接启动时运行)。

于 2021-01-03T15:19:26.693 回答