gunicorn的作用

gunicorn是什么:

gunicorn是一种unix上被广泛使用的Python WSGI UNIX HTTP Server

WSGI是什么:

先说下 WSGI 的表面意思,Web Server Gateway Interface 的缩写,即 Web 服务器网关接口。

WSGI是一种规范,定义了 Web服务器 如何与 Python应用程序 进行交互

如何使用gunicorn:

1.下载

pip3 install gunicore

gunicore的作用是使用命令行来启动服务

如果是这样一个python 文件:

arvin.py:

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

1.最简单的启动:

gunicore arvin:app

此时,gunicore默认监听一个127.0.0.1:8000的web server,

2.设置0.0.0.0可以监听所有的ip请求:

gunicorn -b 0.0.0.0:8080 arvin:app

3.在多核服务器上,为了支持更多的并发访问并充分利用资源,可以使用更多的 gunicorn 进程:

gunicorn -w 4 arvin:app 

4.两者结合到一起就是:

gunicorn -w 4 -b 0.0.0.0:8080 arvin:app

-b 表示 gunicorn 开发的访问地址

-w 表示开启多少个线程

arvin:python文件名

app:变量名,python文件中可调用的wsgi接口名称

Gunicorn 服务器作为wsgi app的容器,能够与各种Web框架兼容(flask,django等),大幅度提高wsgi app的性能。

而python 的web框架本质就是一种wsgi app

猜你喜欢

转载自www.cnblogs.com/Jokerguigui/p/11732530.html
今日推荐