1. Overview
WSGI, uWSGI and uwsgi are three related concepts. They are different tools and protocols used in web application development. The following is their detailed introduction:
- WSGI (Web Server Gateway Interface) : WSGI is a Python web application and a web server. Interface specification , which defines the standard interface between applications and servers so that applications can run on different Web servers. The WSGI specification specifies the interface methods that applications must implement and that the server needs to support. WSGI protocol enables different Python web frameworks (such as Flask, Django, etc.) to run on different web servers, which can be Apache, Nginx, etc.
- uWSGI: uWSGI is a Web server. It is a Web application container written in C language and supports running Python and Ruby. , Perl and other programming languages. The uWSGI server can serve as a standalone application server or can be used with other web servers (such as Nginx, Apache) to communicate with Python applications through the WSGI protocol.
- uwsgi: uwsgi is a protocol related to the uWSGI server. The uwsgi protocol is a binary protocol that defines the communication protocol between the uWSGI server and applications. Using the uwsgi protocol, a uWSGI server can communicate with Python applications without starting a new process to handle each request like CGI does. The uwsgi protocol allows bidirectional communication between the uWSGI server and applications, thereby improving performance.
So, uWSGI is a web server that can communicate with Python applications through the WSGI protocol and communicate using the uwsgi protocol. WSGI is an interface specification between Python web applications and web servers, defining a standard interface between applications and servers. uwsgi is a binary communication protocol between the uWSGI server and applications.
uWSGI is a Web Server Gateway Interface, which can be used For integrating Python web applications with web servers such as Nginx or Apache.
- When using the uWSGI module, you need to install the uwsgi module and import it in the Python web applicationuwsgi module, and use the functions provided by the uwsgi module to configure and manage the running of web applications. Common uwsgi module functions include uwsgi.optin(), uwsgi.route(), uwsgi.applications(), etc.
- In addition, the uWSGI module also provides some advanced features, such as Master/Worker mode, process management, load balancing, automatic expansion, etc., so that web applications can better adapt to high concurrency and large traffic Case.
Domestic source address:
- pypi Tsinghua University source: https://pypi.tuna.tsinghua.edu.cn/simple ##pypi Tencent source: http://mirrors.cloud.tencent.com/pypi/simple
- pypi Ali source: https://mirrors.aliyun.com/pypi/simple/
-
mkdir~/.pip/ cat >~/.pip/pip.conf<<EOF [global] index-url = https://repo.huaweicloud.com/repository/pypi/simple trusted-host = repo.huaweicloud.com timeout = 120 EOF
2) Install uwsgi module
# 安装python3 yum -y install python3 yum -y install gcc-c++ -y yum -y install python3-devel -y # 安装 uwsgi flask 模块 pip3 install uwsgi flask # 查看版本 uwsgi --version
3. Example demonstration (uWSGI Nginx configuration)
1) Install nginx
yum update -y yum install epel-release yum -y install nginx
2) Create app. py file
Create a file namedapp.py and add the following code: from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
3) Create uWSGI configuration file
uwsgi.ini, which contains the following information: [uwsgi]
module = app:app
# 相当于命令下面两行
#wsgi-file = app.py # 项目入口文件
#callable = app #flask应用对象
# 支持http+socket两种方式,这里选用socket,也可以选择http-socket,下面会讲解这三种区别
# http = 127.0.0.1:8000
socket = 0.0.0.0:8000
# 也可以使用socket文件,在nginx配置文件中配置也对应,仅限本机通信,一般也很少使用
# socket = /app/myapp.sock
# 注意记得提前创建目录
chdir = /opt/myapp
pidfile=/opt/myapp/myapp.pid
processes = 4
threads = 2
master = true
vacuum = true
py-autoreload = 1
daemonize = /tmp/uwsgi.log
在命令行中启动 uWSGI: 【温馨提示】其实也可以通过一条命令带上对应的参数即可启动,但是不推荐,测试可以。一般使用配置文件的方式启动服务。 使用http协议启动uwsgi的命令为: 将 Web 服务器配置为反向代理 uWSGI,例如,在 Nginx 中,可以使用以下配置文件: 其中,uwsgi_params 文件包含以下内容: 【特别注意】uwsgi_params 在nginx conf文件夹下自带,uwsgi_pass一定要跟uwsgi_conf.ini中写的地址完全一致。 重启 Web 服务器以使配置生效。 访问(浏览器访问,curl访问也行) Nginx上游(upstream)是指一组后端服务器,Nginx可以与其通信并将客户端请求转发到这些服务器。换句话说,上游服务器是Nginx代理请求的后端服务器。 Nginx的upstream支持5种 分配方式,其中 轮询(默认)、权重、IP散列这三种为Nginx原生支持的分配方式,fair 和 url_hash 为第三方支持的分配方式。 轮询是upstream的默认分配方式,即每个请求按照时间顺序轮流分配到不同的后端服务器,如果某个后端服务器 down 掉后,能自动剔除。 轮询的加强版,既可以指定轮询比率,weight 和访问几率成正比,主要应用于后端服务器异质的场景下。 每个请求按照访问 Ip(即Nginx的前置服务器或客户端IP)的 hash结果分配,这样每个访客会固定访问一个后端服务器,可以解决 session 一致问题。 先在另外一个节点上再起一个uWSGI服务,将上面示例配置修改: 192.168.182.110 节点 app.py 192.168.182.111 节点 app.py 验证 从上图可知,请求轮询调度,这才是企业一般想要的效果,负载均衡。 【1】socket 示例(uwsgi.ini): nginx配置 【2】http 示例(uwsgi.ini): nginx配置 【3】http-socket 示例(uwsgi.ini): nginx配置 TCP和Unix套接字(Unix domain socket)是两种不同类型的套接字。 因此,TCP套接字用于在网络上进行通信,而Unix套接字用于在同一台计算机上进行通信。虽然TCP套接字可以通过网络连接到不同的计算机,但是Unix套接字提供了更高效的进程间通信机制,并且更适合于需要在同一台计算机上运行的进程间通信。 【TCP 示例】常用uwsgi.ini: 【unix 示例】仅限于本机通信,很少使用。uwsgi.ini: nginx配置 Python 中 web开发中的 WSGI、uWSGI 和 uwsgi 三者介绍就先到这里了
4)启动 uWSGI
uwsgi --ini uwsgi.ini
###或者
uwsgi uwsgi.ini
### 重启
uwsgi --reload /opt/myapp/myapp.pid
###关闭
uwsgi --stop /opt/myapp/myapp.pid
uwsgi --http :8000 --ini uwsgi_conf.ini -d ./uwsgi.log --pidfile=uwsgi.pid
5)配置 Web 服务器
# vi /etc/nginx/conf.d/myapp.conf
server {
listen 8080;
server_name myapp.com;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
}
}
uwsgi_paramQUERY_STRING $query_string;
uwsgi_paramREQUEST_METHOD $request_method;
uwsgi_paramCONTENT_TYPE $content_type;
uwsgi_paramCONTENT_LENGTH $content_length;
uwsgi_paramREQUEST_URI$request_uri;
uwsgi_paramPATH_INFO$document_uri;
uwsgi_paramDOCUMENT_ROOT$document_root;
uwsgi_paramSERVER_PROTOCOL$server_protocol;
uwsgi_paramREQUEST_SCHEME $scheme;
uwsgi_paramHTTPS$https if_not_empty;
uwsgi_paramREMOTE_ADDR$remote_addr;
uwsgi_paramREMOTE_PORT$remote_port;
uwsgi_paramSERVER_PORT$server_port;
uwsgi_paramSERVER_NAME$server_name;
6)重启 Web 服务器
# 重启
systemctl restart nginx
# 如果是之前nginx服务已经存在,只是修改了配置,可建议使用reload加载
nginx -t && nginx -s reload
# 或者
systemctl reload nginx
7)Nginx upstream 负载均衡
1、轮询(默认)
upstream backend {
server 192.168.182.110:8000;
server 192.168.182.111:8000;
}
2、权重(weight)
upstream backend {
server 192.168.182.110:8000 weight=1;
server 192.168.182.111:8000 weight=2;
}
3、IP散列(ip_hash)
upstream backend {
ip_hash;
server 192.168.182.110:8000 weight=1;
server 192.168.182.111:8000 weight=2;
}
# vi /etc/nginx/conf.d/myapp.conf
upstream backend {
server 192.168.182.110:8000;
server 192.168.182.111:8000;
}
server {
listen 8080;
server_name myapp.com;
location / {
include uwsgi_params;
uwsgi_pass backend;
}
}
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World 192.168.182.110!n'
if __name__ == '__main__':
app.run()
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World 192.168.182.111!n'
if __name__ == '__main__':
app.run()
curl127.0.0.1:8080
8)http、http-socket 和 socket 区别
[uwsgi]
module = app:app
#socket = 127.0.0.1:8000
socket = 0.0.0.0:8000
chdir = /opt/myapp
pidfile=/opt/myapp/myapp.pid
processes = 4
threads = 2
master = true
vacuum = true
py-autoreload = 1
daemonize = /tmp/uwsgi.log
upstream backend {
server 192.168.182.110:8000;
server 192.168.182.111:8000;
}
server {
listen 8080;
server_name myapp.com;
location / {
include uwsgi_params;
uwsgi_pass backend;
}
}
[uwsgi]
module = app:app
socket = 0.0.0.0:8000
chdir = /opt/myapp
pidfile=/opt/myapp/myapp.pid
processes = 4
threads = 2
master = true
vacuum = true
py-autoreload = 1
daemonize = /tmp/uwsgi.log
upstream backend {
server 192.168.182.110:8000;
server 192.168.182.111:8000;
}
server {
listen 8080;
server_name myapp.com;
location / {
include uwsgi_params;
proxy_pass http://backend;
}
}
[uwsgi]
module = app:app
http = 0.0.0.0:8000
chdir = /opt/myapp
pidfile=/opt/myapp/myapp.pid
processes = 4
threads = 2
master = true
vacuum = true
py-autoreload = 1
daemonize = /tmp/uwsgi.log
upstream backend {
server 192.168.182.110:8000;
server 192.168.182.111:8000;
}
server {
listen 8080;
server_name myapp.com;
location / {
include uwsgi_params;
proxy_pass http://backend;
}
}
9)TCP 与 uinx 区别
[uwsgi]
module = app:app
socket = 127.0.0.1:8000
chdir = /opt/myapp
pidfile=/opt/myapp/myapp.pid
processes = 4
threads = 2
master = true
vacuum = true
py-autoreload = 1
daemonize = /tmp/uwsgi.log
[uwsgi]
module = app:app
socket = /opt/myapp/myapp.socket
chdir = /opt/myapp
pidfile=/opt/myapp/myapp.pid
processes = 4
threads = 2
master = true
vacuum = true
py-autoreload = 1
daemonize = /tmp/uwsgi.log
server {
listen 8080;
server_name myapp.com;
location / {
include uwsgi_params;
proxy_pass unix:///opt/myapp/myapp.sock;
}
}
The above is the detailed content of Introduction to WSGI, uWSGI and uwsgi in Python. For more information, please follow other related articles on the PHP Chinese website!

Implementing factory pattern in Python can create different types of objects by creating a unified interface. The specific steps are as follows: 1. Define a basic class and multiple inheritance classes, such as Vehicle, Car, Plane and Train. 2. Create a factory class VehicleFactory and use the create_vehicle method to return the corresponding object instance according to the type parameter. 3. Instantiate the object through the factory class, such as my_car=factory.create_vehicle("car","Tesla"). This pattern improves the scalability and maintainability of the code, but it needs to be paid attention to its complexity

In Python, the r or R prefix is used to define the original string, ignoring all escaped characters, and letting the string be interpreted literally. 1) Applicable to deal with regular expressions and file paths to avoid misunderstandings of escape characters. 2) Not applicable to cases where escaped characters need to be preserved, such as line breaks. Careful checking is required when using it to prevent unexpected output.

In Python, the __del__ method is an object's destructor, used to clean up resources. 1) Uncertain execution time: Relying on the garbage collection mechanism. 2) Circular reference: It may cause the call to be unable to be promptly and handled using the weakref module. 3) Exception handling: Exception thrown in __del__ may be ignored and captured using the try-except block. 4) Best practices for resource management: It is recommended to use with statements and context managers to manage resources.

The pop() function is used in Python to remove elements from a list and return a specified position. 1) When the index is not specified, pop() removes and returns the last element of the list by default. 2) When specifying an index, pop() removes and returns the element at the index position. 3) Pay attention to index errors, performance issues, alternative methods and list variability when using it.

Python mainly uses two major libraries Pillow and OpenCV for image processing. Pillow is suitable for simple image processing, such as adding watermarks, and the code is simple and easy to use; OpenCV is suitable for complex image processing and computer vision, such as edge detection, with superior performance but attention to memory management is required.

Implementing PCA in Python can be done by writing code manually or using the scikit-learn library. Manually implementing PCA includes the following steps: 1) centralize the data, 2) calculate the covariance matrix, 3) calculate the eigenvalues and eigenvectors, 4) sort and select principal components, and 5) project the data to the new space. Manual implementation helps to understand the algorithm in depth, but scikit-learn provides more convenient features.

Calculating logarithms in Python is a very simple but interesting thing. Let's start with the most basic question: How to calculate logarithm in Python? Basic method of calculating logarithm in Python The math module of Python provides functions for calculating logarithm. Let's take a simple example: importmath# calculates the natural logarithm (base is e) x=10natural_log=math.log(x)print(f"natural log({x})={natural_log}")# calculates the logarithm with base 10 log_base_10=math.log10(x)pri

To implement linear regression in Python, we can start from multiple perspectives. This is not just a simple function call, but involves a comprehensive application of statistics, mathematical optimization and machine learning. Let's dive into this process in depth. The most common way to implement linear regression in Python is to use the scikit-learn library, which provides easy and efficient tools. However, if we want to have a deeper understanding of the principles and implementation details of linear regression, we can also write our own linear regression algorithm from scratch. The linear regression implementation of scikit-learn uses scikit-learn to encapsulate the implementation of linear regression, allowing us to easily model and predict. Here is a use sc


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
