1. 程式人生 > >Flask與WSGI

Flask與WSGI

剛開始接觸到python及Flask框架時,總是會聽到 wsgi等等相關的名詞,以及 專案部署時會用到nginx+gunicorn等等,但是對於一個請求從 nignx到gunicorn再到falsk框架中間經歷的卻一直似懂非懂。現在總結一下。

 

WSGI server:負責從客戶端接受請求,將request轉發給 WSGI application,再將  WSGI application返回的response傳給客戶端。

WSGI application:負責接受從WSGI server 傳送來的 request,出來完請求後,將response返回給WSGI server。此application可以包括多個棧結構的中介軟體(比如django)

WSGI:全名(Web Server Gateway Interface)是一種規範協議(和http協議一個級別),規定了WSGI serverWSGI application如何進行通訊。

uWSGI:一個web伺服器,實現了WSGI,uwsgi,http等協議。如下官網引用(有的教程害死人,大小寫不仔細區分,導致錯誤的理解,所以特意看了官網的解釋:https://uwsgi-docs.readthedocs.io/en/latest/index.html):

The uWSGI project aims at developing a full stack for building hosting services 

uwsgi:一種通訊協議,專服務於uWSGI伺服器

 

一般通過supervisor+nginx+gunicorn的部署結構的請求步驟如下:

其實Flask等web框架內部已經有了一個 WSGI server用來接受請求,只是因為其自帶的server在處理併發等情況時不夠優秀,畢竟這些框架的重點放在了WSGI applicaiton的層面上。所以在生產部署時,才需要 額外的gunicorn或uWSGI伺服器(實現WSGI server)來代替框架內部的WSGI server.

 

相關網址:

https://www.cnblogs.com/gdkl/p/6807667.html

https://www.cnblogs.com/wspblog/p/8575101.html

http://python.jobbole.com/88653/

https://blog.csdn.net/sinat_36651044/article/details/77462831