flask - Socket handshake error when using gunicorn
问题描述
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!
解决方案
现在没有能力对此进行测试,但也许可以尝试:
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
直接启动时运行)。
推荐阅读
- excel - 从电子邮件格式 firstname.lastname@email.com 中提取名字和姓氏的 VBA 代码
- php - 在运行测试套件之前只初始化一次变量
- typescript - How to transform union type into union of arrays in typescript?
- r - Horizontally shifting rows based on a value in R
- node.js - Ng serve for angular application is not starting the server
- laravel - how can install vue js in laravel 8 without any error when setup Laravel 8 and Vue Js page vue template not displaying
- python - Selenium Python Authenticating issue
- java - Get console parameters in ISuite class (TestNG)
- sql - Check if index exist when index is on multiple columns
- amazon-web-services - AWS CloudSearch failing for simple queries