Python Flask-1分鐘快速構(gòu)造你的Web程序(python搭建web)
在技術(shù)橫沖直撞、框架滿天飛的這個時代,快速運(yùn)行起一個Web應(yīng)用已經(jīng)是零門檻的事情,這篇文章主要記錄一下用Python Flask框架來快速構(gòu)造“HelloWorld”的經(jīng)歷;既然是基于Python的框架,那么首先默認(rèn)你已經(jīng)安裝了Python;
筆者在學(xué)習(xí)的過程中就用了這個鏈接進(jìn)行了對Flask的入門學(xué)習(xí):http://www.pythondoc.com/flask/quickstart.html#id2,初學(xué)者不用太追求過于官方、過于面面俱到的文檔,這么說也不是排斥去系統(tǒng)地學(xué)習(xí)鉆研一門技術(shù),而是想說明當(dāng)新入門一個技術(shù)的時候,只有先讓程序快速地跑起來,快速地看到效果,才能保證自己興趣的濃厚度,如果一個框架或是一個開源項目,要花費(fèi)你好幾天的時間才能跑,那么可能的情況是你的興趣已經(jīng)被磨滅了一大半;
用Flask構(gòu)造一個簡單的程序如下:
1.pip安裝flask包 :
pip install flask
2.你的第一個應(yīng)用程序基本是這樣的:
from flask import Flaskapp = Flask(__name__)@app.route('/')def hello_world(): return 'hello'if __name__ == '__main__': app.run()
好了,可以運(yùn)行了,你會看到如下的輸出:
* Running on http://127.0.0.1:5000/ (Press CTRL C to quit)* Restarting with stat* Debugger is active!* Debugger PIN: 517-355-210
就這樣,已經(jīng)在本地啟動了一個web服務(wù),監(jiān)聽的是5000端口,直接在瀏覽器訪問http://127.0.0.1:5000/,你就能看到以下的頁面:
到此為止,一個簡單的web程序已經(jīng)構(gòu)造完成。
接下來再記錄幾個常見問題:
1.如果你要部署到服務(wù)器,怎么讓網(wǎng)絡(luò)上的其他人訪問我的服務(wù),并且注意你的服務(wù)器上的相關(guān)端口(如這里的5000)一定是對外開放的
app.run(host='0.0.0.0')
只要改成0.0.0.0,你的服務(wù)器監(jiān)控的所有ip地址,即任何人都能訪問你的服務(wù)啦
2.目前跑起來的這個應(yīng)用是多線程跑的嗎?
app.run(threaded=True)
我用的是python3.6,默認(rèn)是多線程的,為了更加明顯,我顯式地加上threaded=True,反之如果threaded=False,則是單線程;我也進(jìn)行了如下的測試:
場景一:Flask單線程模式代碼
from flask import Flaskapp = Flask(__name__)import threadingcount = 1@app.route("/")def hello(): print(threading.current_thread().name) global count for i in range(0, 10000): count = count 1 count = count - 1 count = count 1 count = count - 1 count = count 1 count = count - 1 count = count 1 count = count - 1 count = count 1 count = count - 1 count = count 1 count = count - 1 print("hello:%dn" % count) return "hello"if __name__ == '__main__': app.run(threaded=False)
運(yùn)行一下,然后用ab測試命令,去跑一個小的壓測:
ab -c 10 -n 10 "http://127.0.0.1:5000/"
這里是10個進(jìn)程并發(fā)發(fā)送10個請求,輸出如下:
* Running on http://127.0.0.1:5000/ (Press CTRL C to quit)MainThread127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -hello:1MainThreadhello:1127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -MainThread127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -hello:1MainThreadhello:1MainThread127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -hello:1MainThread127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -hello:1MainThread127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -hello:1MainThreadhello:1MainThread127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -hello:1MainThread127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -hello:1
可以看到每次都是同一個線程,而每次輸出的值也都是hello:1
場景二:Flask多線程模式代碼
from flask import Flaskapp = Flask(__name__)import threadingcount = 1@app.route("/")def hello(): print(threading.current_thread().name) global count for i in range(0, 10000): count = count 1 count = count - 1 count = count 1 count = count - 1 count = count 1 count = count - 1 count = count 1 count = count - 1 count = count 1 count = count - 1 count = count 1 count = count - 1 print("hello:%dn" % count) return "hello"if __name__ == '__main__': app.run(threaded=True)
同樣Run一下, 用相同ab測試命令去壓測,可以看到如下輸出:
* Running on http://127.0.0.1:5000 (Press CTRL C to quit)Thread-1hello:1127.0.0.1 - - [20/May/2022 23:00:29] "GET / HTTP/1.0" 200 -Thread-2hello:1127.0.0.1 - - [20/May/2022 23:00:29] "GET / HTTP/1.0" 200 -Thread-3Thread-4hello:1hello:0127.0.0.1 - - [20/May/2022 23:00:29] "GET / HTTP/1.0" 200 -127.0.0.1 - - [20/May/2022 23:00:29] "GET / HTTP/1.0" 200 -Thread-5Thread-6hello:0hello:0127.0.0.1 - - [20/May/2022 23:00:29] "GET / HTTP/1.0" 200 -127.0.0.1 - - [20/May/2022 23:00:29] "GET / HTTP/1.0" 200 -Thread-7Thread-8hello:0Thread-9Thread-10hello:0hello:0hello:0
可以看到每次的線程名都不一樣,顯然開啟了多線程的模式;
上述兩種場景的唯一區(qū)別就在于threaded這個參數(shù)的值,可以動手測試一下,一旦涉及到多線程,對于一個全局變量就會出現(xiàn)互相競爭的情況,也會涉及到加鎖、線程上下文切換等行為, 所以這里打印出的hello后面的數(shù)值也不一樣了,有時候等于1,有時候等于0。