gunicorn是什么:
gunicorn是一種unix上被廣泛使用的Python WSGI UNIX HTTP Server
WSGI是什么:
先說下 WSGI 的表面意思,Web Server Gateway Interface 的縮寫,即 Web 服務器網關接口。
WSGI是一種規范,定義了 Web服務器 如何與 Python應用程序 進行交互
如何使用gunicorn:
1.下載
pip3 install gunicore
gunicore的作用是使用命令行來啟動服務
如果是這樣一個python 文件:
arvin.py:
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'hello world' if __name__ == '__main__': app.debug = True app.run()
1.最簡單的啟動:
gunicore arvin:app
此時,gunicore默認監聽一個127.0.0.1:8000的web server,
2.設置0.0.0.0可以監聽所有的ip請求:
gunicorn -b 0.0.0.0:8080 arvin:app
3.在多核服務器上,為了支持更多的並發訪問並充分利用資源,可以使用更多的 gunicorn 進程:
gunicorn -w 4 arvin:app
4.兩者結合到一起就是:
gunicorn -w 4 -b 0.0.0.0:8080 arvin:app
-b 表示 gunicorn 開發的訪問地址
-w 表示開啟多少個線程
arvin:python文件名
app:變量名,python文件中可調用的wsgi接口名稱
Gunicorn 服務器作為wsgi app的容器,能夠與各種Web框架兼容(flask,django等),大幅度提高wsgi app的性能。
而python 的web框架本質就是一種wsgi app