1,介绍
2,安装
conda install -c conda-forge uwsgi
2.1 报错,gcc版本问题
linux-gnu/libpython3.6m.a -lutil -lcrypt
lto1: fatal error: bytecode stream generated with LTO version 6.0 instead of the expected 4.2
compilation terminated.
lto-wrapper: fatal error: gcc returned 1 exit status
compilation terminated.
/home/hayley/anaconda3/envs/envUntitled/compiler_compat/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status
*** error linking uWSGI ***
解决方法
#查看当前系统安装所有版本的gcc
ls /usr/bin/gcc* -l
#如果gcc有5以下的版本,则不用在安装
sudo apt-get install gcc-4.8
#更改gcc系统默认版本
sudo rm /usr/bin/gcc #删除已有软连接
sudo ln -s /usr/bin/gcc-4.8 /usr/bin/gcc #创建指向gcc4.8的软连接
2.2 uswgi使用问题
uwsgi: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解决方法,添加虚拟环境的软链接
#找到所有的系统中libpcre
find / -name libpcre.so.*
#创建libpcre.so.1软链到/lib下
ln -s /home/anaconda3/lib/libpcre.so.1 /lib
#测试一下是否好用了
which uwsgi
#Out:/home/anaconda3/envs/***/bin/uwsgi
3,uwsgi的并发和监控
默认情况下,uWSGI 启动一个单一的进程和一个单一的线程。
可以用–processes选项添加更多的进程,或者使用 --threads 选项添加更多的线程 ,也可以两者同时使用。
uwsgi --http :9090 --wsgi-file foobar.py --master --processes 4 --threads 2
以上命令将会生成 4 个进程, 每个进程有 2 个线程。
如果你要执行监控任务,可以使用 stats 子系统,监控的数据格式是 JSON:
uwsgi --http :9090 --wsgi-file foobar.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191
我们可以安装 uwsgitop(类似 Linux top 命令) 来查看监控数据:
pip install uwsgitop
4,部署flask
创建一个flaskapp.py的文件
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "<span style='color:red'>I am app 1</span>"
部署
uwsgi --http 127.0.0.1:3031 --wsgi-file flaskapp.py --callable app --processes 4 --threads 2 --stats 127.0.0.1:9191
5,读取配置文件
uwsgi.ini
[uwsgi]
# 接口地址
http = 127.0.0.1:3031
# 如果使用nginx就用socket
# socket=0.0.0.0:8000
# 如果程序中有文件下载,图片、视频等就需要就上这个配置
sgi-disable-file-wrapper=true
# flask程序在你服务器上的目录
chdir=/home/rose/PycharmProjects/test
# flask项目的启动文件
wsgi-file=manage.py
# 启动文件中的flask app(即第一部分中的app)
callable=app
# 进程数
processes = 4
# 线程数
threads = 2
# 是否启动多线程
enable-threads=True
# 是否将uwsgi的运行线程设置为主线程,如果设置为主进程,在开启的其他的子进程会随着主进程关闭而关闭
master=True
# 设置用来记录uwsgi进程号的文件
pidfile=uwsgi.pid
# 设置uwsgi运行的日志文件(可以记录程序运行的)
daemonize=uwsgi.log
#在每个worker而不是master中加载应用
lazy-apps=true
# 设置编码(不是必须)
env LANG="en_US.UTF-8"
env LANGUAGE="en_US.UTF-8"
stats = 127.0.0.1:9191
启动
uwsgi --ini uwsgi.ini
uwsgi --reload uwsgi/uwsgi.pid # 重启
uwsgi --stop uwsgi/uwsgi.pid # 停止
uwsgi --version # 查看版本