问题
I have a working chat app on localhost, but it does not work on heroku (Question from yesterday). After some research I think the problem is how I start the app.
Procfile currently:
web: gunicorn adult_main:app
I need to use gevent
in combination with gunicorn
. This way I should be able to have max. 1000 socket.io connections, which is enough for me for the moment.
I already installed and added gevent to the requirements.txt
. Here an extract of the file:
Flask-SocketIO==2.9.6
gunicorn==19.7.1
gevent==1.2.2
gevent-socketio==0.3.6
gevent-websocket==0.10.1
But I do not understand how to start the app correctly. On a sidenote, I am using heroku's free tier to test everything.
I tried a few solutions, which I found online, but I do not fully understand them and I have not found a documentation on that yet. Here are some of my attempts:
web: gunicorn -k gevent-socketio adult_main:app
web: gunicorn -k gevent adult_main:app
I also tried to start without gunicorn, but I think that was completely wrong. Am I on the correct path here?
EDIT
I managed to make some progress with:
web: gunicorn -k gevent -w 1 adult_main:app
The app does not crash on start now, but in the web console I see an error:
socket.io.js:7 WebSocket connection to 'wss://my-
project.herokuapp.com/socket.io/?
EIO=3&transport=websocket&sid=0022c1937df648bbab836bfcb4c35' failed: Error
during WebSocket handshake: Unexpected response code: 500
In the heroku logs:
RuntimeError: You need to use the gevent-websocket server.
EDIT
Eventhough I see these errors, the chat works now on heroku. That is really weird. Probably I can establish now only 1 connection, how to continue now?
回答1:
The error is correct, you need to use the gevent-websocket webserver and not the native gevent web server that comes with gunicorn.
It seems you are searching for answers all over the place. I would suggest that you use the Flask-SocketIO documentation as the ultimate source of truth, as I keep that updated.
For this specific case, the Gunicorn section of the documentation shows you how you need to start your server:
gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 module:app
来源:https://stackoverflow.com/questions/50059102/python-heroku-configure-procfile-gunicorn-gevent-for-socket-io-chat-app-runt