hi-nginx不僅讓python web 應用跑得飛快,而且讓相關開發變得簡單敏捷。
關於hi-nginx的安裝,請參考:https://www.cnblogs.com/hi-nginx/p/8622561.html
人們常說flask框架又快又好。但是更hi-nginx比起來,還是太慢。比如flask的最小應用是這樣的:
1 from flask import Flask 2 app = Flask(__name__) 3 4 @app.route('/') 5 def hello_world(): 6 return 'Hello World!' 7 8 if __name__ == '__main__': 9 app.run()
但是hi-nginx的最小python應用還要簡單些:
1 if hi_req.uri()=='/test.py': 2 hi_res.content('hello,world') 3 hi_res.status(200)
當然,你也可以把最小應用寫的更復雜些,,比如這樣:
1 def hello_world(): 2 hi_res.content('hello,world') 3 hi_res.status(200) 4 5 def run(): 6 if hi_req.uri()=='/': 7 hello_world() 8 9 if __name__ == '__main__': 10 run()
應用寫完了,怎樣訪問呢?很簡單,在hi-nginx配置文件里這樣寫就可以了:
server { listen 8080; server_name localhost; location / { hi_need_cache off; hi_python_script python/index.py; } }
reload或者restart nginx,然后訪問http://localhost:8080/即可看到hello,world的問候了。
現在用siege來測試下這個最小應用的性能:
siege -c 1000 -r 100 -b http://127.0.0.1:8080/
是不是很滿意!