杜郎俊赏 - dujun.io

开源实例之Gunicorn

前言

本篇介绍《开源实例之FlaskBB》中提到的 Gunicorn。

Gunicorn介绍

Gunicorn is a Python WSGI HTTP Server for UNIX.

It's a pre-fork worker model ported from Ruby's Unicorn project. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy.

Gunicorn(Green Unicorn)是一个 Python WSGI(Web Server Gateway Interface)HTTP 服务器。因为 Nginx 不能直接支持 WSGI 协议,所以 Python WEB 项目可以选择 Gunicorn 作为服务器。

官网地址:https://gunicorn.org
仓库地址:https://github.com/benoitc/gunicorn

Gunicorn安装

Gunicorn 可以用 pip 安装:

pip install gunicorn

Gunicorn使用

#myapp.py
def app(environ, start_response):
    data = b"Hello, World!\n"
    start_response("200 OK", [
        ("Content-Type", "text/plain"),
        ("Content-Length", str(len(data)))
    ])
    return iter([data])

gunicorn -w 4 myapp:app

[2014-09-10 10:22:28 +0000] [30869] [INFO] Listening at: http://127.0.0.1:8000 (30869)
[2014-09-10 10:22:28 +0000] [30869] [INFO] Using worker: sync
[2014-09-10 10:22:28 +0000] [30874] [INFO] Booting worker with pid: 30874
[2014-09-10 10:22:28 +0000] [30875] [INFO] Booting worker with pid: 30875
[2014-09-10 10:22:28 +0000] [30876] [INFO] Booting worker with pid: 30876
[2014-09-10 10:22:28 +0000] [30877] [INFO] Booting worker with pid: 30877

后记

体验更多开源实例

标签: 开源实例
日期:2023-03-27