1. 簡(jiǎn)介
Gunicorn(Green Unicorn)是給Unix用的WSGI HTTP 服務(wù)器,它與不同的web框架是非常兼容的、易安裝、輕、速度快。
2. 示例代碼1
def app(environ, start_response): data = b"Hello World\n" start_response("200 OK", [ ("Content-Type", "test/plain"), ("Content-Length", str(len(data))) ]) return iter([data])
啟動(dòng)
gunicorn -w 4 myapp:app
起來后顯示
[2016-12-12 00:20:12 +0000] [11755] [INFO] Starting gunicorn 19.6.0 [2016-12-12 00:20:12 +0000] [11755] [INFO] Listening at: http://127.0.0.1:8000 (11755) [2016-12-12 00:20:12 +0000] [11755] [INFO] Using worker: sync [2016-12-12 00:20:12 +0000] [11760] [INFO] Booting worker with pid: 11760 [2016-12-12 00:20:12 +0000] [11761] [INFO] Booting worker with pid: 11761 [2016-12-12 00:20:12 +0000] [11762] [INFO] Booting worker with pid: 11762 [2016-12-12 00:20:12 +0000] [11763] [INFO] Booting worker with pid: 11763
此時(shí),調(diào)用http://127.0.0.1:8000
$curl http://127.0.0.1:8000 Hello World
參數(shù)說明
-w 處理HTTP請(qǐng)求的worker進(jìn)程數(shù),以下兩種啟動(dòng)方式等價(jià)
gunicorn -w 4 myapp:app gunicorn --workers=4 myapp:app
參考:
-w INT, --workers INT The number of worker processes for handling requests.
問題:為何調(diào)用 http://ip:8000不行呢, 這個(gè)是什么請(qǐng)求呢?
默認(rèn)有-b參數(shù),參考
-b ADDRESS, --bind ADDRESS The socket to bind. [['127.0.0.1:8000']]
以下方式啟動(dòng)就可以用ip的方式啟動(dòng)了
sudo gunicorn -w 2 -b 0.0.0.0:4000 myapp:app
3. 示例代碼2
之前簡(jiǎn)單的flask方法
from flask import Flask app = Flask(__name__) @app.route('/hello.world') def check(): return 'hello world!' if __name__ == '__main__': app.run()
啟動(dòng)
$sudo gunicorn -b 0.0.0.0:300 -w 4 myapp3:app [2016-12-18 19:19:51 +0000] [21005] [INFO] Starting gunicorn 19.6.0 [2016-12-18 19:19:51 +0000] [21005] [INFO] Listening at: http://0.0.0.0:300 (21005) [2016-12-18 19:19:51 +0000] [21005] [INFO] Using worker: sync [2016-12-18 19:19:51 +0000] [21010] [INFO] Booting worker with pid: 21010 [2016-12-18 19:19:51 +0000] [21011] [INFO] Booting worker with pid: 21011 [2016-12-18 19:19:51 +0000] [21014] [INFO] Booting worker with pid: 21014 [2016-12-18 19:19:51 +0000] [21017] [INFO] Booting worker with pid: 21017
測(cè)試
$curl localhost:300/hello.world hello world!
4. 啟動(dòng)異常
[ERROR] Connection in use: ('127.0.0.1', 8000)
原因之一是之前啟動(dòng)的進(jìn)程沒有殺死。
注:ctrl+z 是掛起進(jìn)程,但沒有終止。ctrl+c是終止進(jìn)程。
如果使用了ctrl+z再回到進(jìn)程中可使用fg命令,這樣可以用ctrl+c來關(guān)閉進(jìn)程
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
