GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.runoob.com — Python uWSGI 安装配置 | 菜鸟教程
Python 3 教程
Python3 教程
Python3 简介
Python3 环境搭建
Python3 Pycharm
Python3 VScode
Python3 基础语法
Python3 基本数据类型
Python3 数据类型转换
Python3 解释器
Python3 注释
Python3 运算符
Python3 数字(Number)
Python3 字符串
Python3 列表
Python3 元组
Python3 字典
Python3 集合
Python3 条件控制
Python3 循环语句
Python3 编程第一步
Python3 推导式
Python3 迭代器与生成器
Python3 with
Python3 函数
Python3 lambda
Python3 装饰器
Python3 数据结构
Python3 模块
Python __name__
Python3 输入和输出
Python3 File
Python3 OS
Python3 错误和异常
Python3 面向对象
Python3 命名空间/作用域
Python 虚拟环境的创建
Python 类型注解
Python3 标准库概览
Python3 实例
Python 测验
Python3 高级教程Python3 正则表达式 Python3 CGI编程 Python3 MySQL(mysql-connector) Python3 MySQL(PyMySQL) Python3 网络编程 Python3 SMTP发送邮件 Python3 多线程 Python3 XML 解析 Python3 JSON Python3 日期和时间 Python3 内置函数 Python3 MongoDB Python3 urllib Python uWSGI 安装配置 Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python 有用的资源 Python AI 绘画 Python statistics Python hashlib Python 量化 Python pyecharts Python selenium 库 Python 爬虫 Python Scrapy 库 Python Markdown Python sys 模块 Python Pickle 模块 Python subprocess 模块 Python queue 模块 Python StringIO 模块 Python logging 模块 Python datetime 模块 Python re 模块 Python csv 模块 Python threading 模块 Python asyncio 模块 Python PyQtPython uWSGI 安装配置本文主要介绍如何部署简单的 WSGI 应用和常见的 Web 框架。 以 Ubuntu/Debian 为例,先安装依赖包: apt-get install build-essential python-dev Python 安装 uWSGI1、通过 pip 命令: pip install uwsgi 2、下载安装脚本: curl http://uwsgi.it/install | bash -s default /tmp/uwsgi 将 uWSGI 二进制安装到 /tmp/uwsgi ,你可以修改它。 3、源代码安装: wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gz tar zxvf uwsgi-latest.tar.gz cd uwsgi-latest make 安装完成后,在当前目录下,你会获得一个 uwsgi 二进制文件。 第一个 WSGI 应用让我们从一个简单的 "Hello World" 开始,创建文件 foobar.py,代码如下: def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
uWSGI Python 加载器将会搜索的默认函数 application 。 接下来我们启动 uWSGI 来运行一个 HTTP 服务器,将程序部署在HTTP端口 9090 上: uwsgi --http :9090 --wsgi-file foobar.py 添加并发和监控默认情况下,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 结合 Web 服务器使用我们可以将 uWSGI 和 Nginx Web 服务器结合使用,实现更高的并发性能。 一个常用的nginx配置如下: location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3031;
}
以上代码表示使用 nginx 接收的 Web 请求传递给端口为 3031 的 uWSGI 服务来处理。 现在,我们可以生成 uWSGI 来本地使用 uwsgi 协议: uwsgi --socket 127.0.0.1:3031 --wsgi-file foobar.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191 如果你的 Web 服务器使用 HTTP,那么你必须告诉 uWSGI 本地使用 http 协议 (这与会自己生成一个代理的–http不同): uwsgi --http-socket 127.0.0.1:3031 --wsgi-file foobar.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191 部署 DjangoDjango 是最常使用的 Python web 框架,假设 Django 项目位于 /home/foobar/myproject: uwsgi --socket 127.0.0.1:3031 --chdir /home/foobar/myproject/ --wsgi-file myproject/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191 --chdir 用于指定项目路径。 我们可以把以上的命令弄成一个 yourfile.ini 配置文件: [uwsgi] socket = 127.0.0.1:3031 chdir = /home/foobar/myproject/ wsgi-file = myproject/wsgi.py processes = 4 threads = 2 stats = 127.0.0.1:9191 接下来你只需要执行以下命令即可: uwsgi yourfile.ini 部署 FlaskFlask 是一个流行的 Python web 框架。 创建文件 myflaskapp.py ,代码如下: from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "<span style='color:red'>I am app 1</span>"
执行以下命令: uwsgi --socket 127.0.0.1:3031 --wsgi-file myflaskapp.py --callable app --processes 4 --threads 2 --stats 127.0.0.1:9191
AI 思考中...
点我分享笔记取消
分类导航
Advertisement
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |