使用uwsgi 部署python web服务
uwsgi, wsgi协议的一个很好的实现,源码在这里:https://github.com/unbit/uwsgi
c语言编写,有兴趣可以研究下。
上DEMO:
wsgi_server.py
def application(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return 'hello world'
应用:
使用uwsgi部署以上应用:
uwsgi --http 0.0.0.0:9090 -p 4 -l 100 -M -R 100000 -z30 -L --wsgi-file wsgi_server.py --max-apps 65535 --stats 127.0.0.1:1717 --post-buffering 100M --cpu-affinity --buffer-size 65535 --daemonize /tmp/uwsgi --pidfile /tmp/uwsgi.pid --memory-report --threads 4
然后浏览器访问: http://localhost:9090/ 即可。
优势:
提高并发访问支持(-p 进程数, --threads 线程数)
提高服务运行稳定性(--daemonize)
安装
pip install uwsgi
pip install uwsgitop
uwsgi--uwsgi服务器
uwsgitop--uwsgi服务器性能查看工具,用法:
配合以上例子
uwsgitop 127.0.0.1:1717
参数详细说明
官方文档:http://uwsgi-docs.readthedocs.io/en/latest/Options.html
不错的中文文档:http://www.cnblogs.com/zhouej/archive/2012/03/25/2379646.html
挑几个重点:
--wsgi-file , 指定wsgi入口文件
-p , workers个数,也是进程数, 按照惯例可默认设为核数,但是不是最优配置需要通过 uwsgitop来查看(个人觉得uwsgitop没啥用)。
--threads , 线程数, 每个进程的线程数,进程的任务用线程的模式完成。由于用c编写,因此不用担心GIL的问题, 但linux上不存在线程,线程本质来讲是伪进程(且存在上下文切换成本),因此不建议使用。
(用了后,再用uwsgitop监控时,可通过键盘的“A”键查看线程的资源占用情况)
--listen (-l), 内核监听(listen)网络队列的长度,受文件操作系统最大的网络连接数(net.core.somaxconn) 的限制, 长度越大意味着在高并发的环境下,丢失请求越少。
--cpu-affinity, cpu友好,即进程在运行时不切换核(切换意味着时间成本)
--stats, 监控程序的url,只有设置了这个参数以后才能用 uwsgitop 1717来观看监控
--memory-report, 开启内存占用报告(uwsgitop中可以看到)
--master(-M), 启动主进程,方便管理所有进程, 可以配合--pidfie 使用。方便停止(uwsgi --stop /tmp/uwsgi.pid)/重启uwsgi ( uwsgi --reload /tmp/uwsgi.pid)
--daemonize, 增加守护进程,使web服务更加稳定。参数为日志文件的路径。
--disable-loggin, 不记录请求信息的日志。只记录错误以及uWSGI内部消息到日志中。
其他略,可以自己逐一尝试。
用途
flask必需搭配使用咯。
django建议使用,默认支持,有默认的wsgi.py文件生成。
1. flask
uwsgi -s /tmp/uwsgi.sock --manage-script-name --mount /=xxx_project:app --http 0.0.0.0:9091
xxx_project换为具体的项目文件顶层文件夹。
2. django
django官方介绍。
即
uwsgi --chdir=/path/to/project/site_app --module=site_app.wsgi:application --env DJANGO_SETTINGS_MODULE=site_app.settings
uwsgi官方介绍:http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html#test-your-django-project。
即
uwsgi --http :8000 --module web_app.wsgi
相对来说后者简单粗暴。
进一步的并发提升
+nginx,
为啥呢?看这里:http://www.oschina.net/translate/serving-python-web-applications?lang=eng
即:
I was pretty proud when we got API response times down to 40ms on average. When I say API I’m only talking about the time it takes from it hitting the Python server, to the server returning it’s response to the proxy.
Unfortunately, it quickly became apparent that there were capacity issues when we started getting more traffic for larger spikes. We’d hit bumpy response times that were no longer consistent, but we still had about 30% memory and 60% cpu to spare on the web nodes.
即:实测发现,uwsgi似乎不能充分的利用cpu和内存来达到无上限的并发。有一定瓶颈,并且到达瓶颈后,cpu和内存还剩下很多!
那为了充分利用资源不妨多开几个uwsgi服务咯。
老外实测发现: 与其让一个uwsgi服务跑10个进程,不如开10个uwsgi服务,然后用nginx做负载均衡!
After quite a few tweaks, what we eventually settled on was managing a larger amount of uwsgi processes, and letting nginx load balance them (vs letting uwsgi itself load balance). What this means, is that instead of doing uwsgi –processes=10, we ran 10 separate uwsgi processes. The result was a beautiful, consistent 20ms average response time.
当然这个有待验证。
简单说,就是uwsgi本身的负载均衡没有nginx牛逼。所以阉割掉不用。
因此uwsgi退化成了wsgi服务器。
nginx 咋配置,略。
转载请注明来自:http://www.cnblogs.com/Tommy-Yu/p/5647730.html,谢谢!
使用uwsgi 部署python web服务的更多相关文章
- nginx+uwsgi部署python web(web.py)
1.nginx: nginx 是一个 http 服务器,与 apache.lighttpd.Microsoft IIS 等属于同类产品. 2.uWSGI: uWSGI 是一个快速的.纯C语言开发的.自 ...
- nginx上部署python web
nginx上部署python web http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
- 戏说WSGI(Python Web服务网关接口)--[转载]
戏说WSGI(Python Web服务网关接口) 当你在Python的世界中冒险,突然遭遇一只Web怪兽,你会选择什么武器对付它?在兵器谱上,下列兵器可谓名列前茅: Zope,厚重的长枪.较早出现的武 ...
- Python Web 服务开发者: 第 1 部分
Python Web 服务开发者: 第 1 部分 Python Web 服务世界 Python 的座右铭一向是“装备齐全”,这是指在安装该语言时会附带一大套标准库和功能程序.本文概述了在 Python ...
- 踩坑踩坑之Flask+ uWSGI + Tensorflow的Web服务部署
一.简介 作为算法开发人员,在算法模块完成后,拟部署Web服务以对外提供服务,从而将算法模型落地应用.本文针对首次基于Flask + uWSGI + Tensorflow + Nginx部署Web服务 ...
- 使用Nginx+Uwsgi部署Python Flask项目
第一次用Flask做Web(也是第一次用Python做Web),在部署的时候遇到了不少问题,现在将过程就下来,供在这方面也有疑惑的人参考.(PS:使用Apache+mod_wsgi部署模式的可以参考另 ...
- 【JVM】linux上tomcat中部署的web服务,时好时坏,莫名其妙宕机,报错:There is insufficient memory for the Java Runtime Environment to continue.
=========================================================================================== 环境: linu ...
- supervisor+gunicorn部署python web项目
有了Nginx,对于Tomcat没有必要详细了解. 有了supervisor,再也没有必要把一个程序设置成服务.驻留进程,supervisor真是一个相见恨晚的好工具. 在Tomcat中,所有的web ...
- Nginx与python web服务配置(Uwsgi& FastCGI)
Uwsgi start uswgi uwsgi --harakiri 360000 --body-read-warning=10000 --max-fd=65536 -b 1000000 --http ...
随机推荐
- JS判断是否是IE浏览器
前最短的IE判定借助于IE不支持垂直制表符的特性搞出来的. var ie = !+"\v1"; 仅仅需要7bytes!参见这篇文章,<32 bytes, ehr ... 9, ...
- C#:String.Format数字格式化输出
int a = 12345678; //格式为sring输出// Label1.Text = string.Format("asdfadsf{0}adsfasdf",a);// ...
- Orchard源码分析(5.1):Host初始化(DefaultOrchardHost.Initialize方法)
概述 Orchard作为一个可扩展的CMS系统,是由一系列的模块(Modules)或主题(Themes)组成,这些模块或主题统称为扩展(Extensions).在初始化或运行时需要对扩展进行安装:De ...
- Java并发编程核心方法与框架-ScheduledExecutorService的使用
类SchedukedExecutorService的主要作用是可以将定时任务与线程池功能结合. 使用Callable延迟运行(有返回值) public class MyCallableA implem ...
- Elasticsearch-PHP 索引操作(转)
索引操作 本节通过客户端来介绍一下索引API的各种操作.索引操作包含任何管理索引本身(例如,创建索引,删除索引,更改映射等等). 我们通过一些常见的操作的代码片段来介绍,然后在表格中列出剩下的方法.R ...
- 【8-15】Markdown语法学习
学习Markdown语法 来源简书URL #,支持六级标题 列表 用-或*(指无序列表),有序列表直接1. 2. 3. 这样,中间有空格,可乱序(-+*都可,不能混合使用,混合使用为嵌套) 这是一个无 ...
- 导入excel错误:外部表不是预期的格式 解决方案
环境:win7+iis7+Office2007 在asp.net网站中导出Excel文件后,再把文件导入到数据库中. 读取Excel文件时,打开连接出错. 错误为:外部表不是预期的格式 解决:检查了一 ...
- systemctl 取代 service
要使用systemd, linux内核版本要高于: 2.6.39 systemctl的命令格式: systemctl 动作命令(如start stop restart status) 服务名称.ser ...
- 【CISP笔记】数据库及应用安全
数据库安全特性检查工具美国应用安全公司的App Detective英国下一代软件公司的NGS SQuirrel 常见应用安全威胁 网络层面拒绝服务.电子欺骗.嗅探.……系统层面Web服务漏洞.配置错误 ...
- mac安装mongodb
一,安装方法1 ,下载mongodb 1,官网下载mongodb程序 https://www.mongodb.org/downloads#production 2,解压后启动mongodb服务 下载 ...