Gunicorn的安装与启动

Gunicorn的安装与启动

Gunicorn是什么?

Gunicorn (独角兽)是一个高效的Python WSGI Server。

通常用它来运行 wsgi application(由我们自己编写遵循WSGI application的编写规范) 或者 wsgi framework(如Django,Flask)。
WSGI是一个Python程序和用户请求之间的接口。

WSGI服务器的作用就是接受并分析用户的请求,调用相应的python对象完成对请求的处理,然后返回相应的结果。
简单来说gunicorn封装了HTTP的底层实现,我们通过gunicorn启动服务,用户请求与服务相应都经过gunicorn传输。

安装Gunicorn
创建虚拟环境
cd /home/www/
# 拷贝项目到该文件夹,并解压
# 进入到项目根目录
cd /home/www/book
mkdir venv
python3 -m venv venv
解压zip文件
yum install -y unzip zip # 安装zip和unzip包
unzip book.zip # 解压文件
激活虚拟环境
source venv/bin/activate
安装依赖

由于开发阶段又添加了很多第三方库,所以上线之前需要在本地重新导出一次虚拟环境中的第三方库

# 在pycharm中,进入到虚拟环境
pip freeze > requirements.txt
# 重新上传,覆盖服务器中原有的requirements.txt文件
pip3 install -r requirements.txt
错误1
Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-b5grrrir/cryptography/setup.py", line 17, in <module>
        from setuptools_rust import RustExtension
    ModuleNotFoundError: No module named 'setuptools_rust'

解决

pip3 install -U pip setuptools
安装gunicorn第三方库
pip3 install gunicorn
启动服务
gunicorn -w 8 -b 0.0.0.0:8000 app:app
# -w 开启的子进程个数
# 启动日志如下
(venv) [root@iZbp15uy42u5znkvkxiyxdZ book]# gunicorn -w 8 -b 0.0.0.0:8000 app:app
[2022-09-24 17:46:18 +0800] [17700] [INFO] Starting gunicorn 20.1.0
[2022-09-24 17:46:18 +0800] [17700] [INFO] Listening at: http://0.0.0.0:8000 (17700) # 17700 主进程pid
[2022-09-24 17:46:18 +0800] [17700] [INFO] Using worker: sync
[2022-09-24 17:46:18 +0800] [17703] [INFO] Booting worker with pid: 17703 # 17703 子进程pid
[2022-09-24 17:46:18 +0800] [17704] [INFO] Booting worker with pid: 17704
[2022-09-24 17:46:18 +0800] [17705] [INFO] Booting worker with pid: 17705
[2022-09-24 17:46:18 +0800] [17706] [INFO] Booting worker with pid: 17706
[2022-09-24 17:46:18 +0800] [17707] [INFO] Booting worker with pid: 17707
[2022-09-24 17:46:18 +0800] [17709] [INFO] Booting worker with pid: 17709
[2022-09-24 17:46:18 +0800] [17710] [INFO] Booting worker with pid: 17710
[2022-09-24 17:46:18 +0800] [17712] [INFO] Booting worker with pid: 17712
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值