這個問題很有意思。
症狀是,如果使用Python啟動Flask,沒有任何問題;但是使用uWSGI帶着gevent啟動app,到一定時間后,這個uWSGI就收不到任何請求了。
這是使用的命令:
uwsgi3 --gevent 8 --gevent-monkey-patch --http :1234 -s /tmp/uwsgi.sock --pythonpath=~/api/ -w new_api:app
uwsgi3 --gevent 8 --gevent-monkey-patch --http :1234 -s /tmp/uwsgi.sock --pythonpath=~/api/ -w new_api:app
有意思的地方在於,剛開始在排查的時候,由於uWSGI沒有任何錯誤提示,很容易誤以為是Python3和Python2兼容的問題,或者是gevent的問題。
但其實是由於啟動時參數不對。應該是--http-socket。
uwsgi3 --gevent 8 --gevent-monkey-patch --http-socket :1234 -s /tmp/uwsgi.sock --pythonpath=~/api/ -w new_api:app
這樣就對了。
http://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html
在官方文檔里也寫了這個問題。
Do not use --http when you have a frontend webserver or you are doing some form of benchmark, use --http-socket. Continue reading the quickstart to understand why.