日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python Gunicorn

發布時間:2023/11/29 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python Gunicorn 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 簡介

Gunicorn(Green Unicorn)是給Unix用的WSGI HTTP 服務器,它與不同的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])

啟動

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

此時,調用http://127.0.0.1:8000

$curl http://127.0.0.1:8000 Hello World

參數說明

-w ?處理HTTP請求的worker進程數,以下兩種啟動方式等價

gunicorn -w 4 myapp:app gunicorn --workers=4 myapp:app

參考:

-w INT, --workers INTThe number of worker processes for handling requests.

問題:為何調用 http://ip:8000不行呢, 這個是什么請求呢?

默認有-b參數,參考

-b ADDRESS, --bind ADDRESSThe socket to bind. [['127.0.0.1:8000']]

以下方式啟動就可以用ip的方式啟動了

sudo gunicorn -w 2 -b 0.0.0.0:4000 myapp:app

3. 示例代碼2

之前簡單的flask方法

from flask import Flask app = Flask(__name__)@app.route('/hello.world') def check():return 'hello world!'if __name__ == '__main__':app.run()

啟動

$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

測試

$curl localhost:300/hello.world hello world!

4. 啟動異常:[ERROR] Connection in use: ('127.0.0.1', 8000)

原因之一是之前啟動的進程沒有殺死。

注:ctrl+z 是掛起進程,但沒有終止。ctrl+c是終止進程。

如果使用了ctrl+z再回到進程中可使用fg命令,這樣可以用ctrl+c來關閉進程

總結

以上是生活随笔為你收集整理的python Gunicorn的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。