Gunicorn快速入门
Gunicorn (独角兽)是一个高效的Python WSGI Server,通常用它来运行 wsgi application(由我们自己编写遵循WSGI application的编写规范) 或者 wsgi framework(如Django,Paster),地位相当于Java中的Tomcat。
安装Gunicorn
方式一:最简单的使用 easy_install 安装或者更新
sudo easy_install -U gunicorn
方式二:下载源码安装
git clone git://github.com/benoitc/gunicorn.git
cd gunicorn
sudo python setup.py install
如果想让Gunicorn支持异步 workers 的话需要安装一下三个python包
easy_install -U greenlet
easy_install -U eventlet
easy_install -U gevent
说明:如果安装 greenlet 失败的话,你需要安装 Python headers
sudo apt-get install python-dev
gunicorn还需要库函数 libevent(1.4.x or 2.0.4)
运行Gunicorn
成功安装 gunicorn 之后有以下三个指令你可以直接使用,用来启动 gunicorn 运行 wsgi application或者 wsgi frameworks
gunicorn
gunicorn_django
gunicorn_paster
gunicorn
Gunicorn server的最基本的命令,直接用来运行最基本的 wsgi application 。
用法:
gunicorn [OPTIONS] APP_MODULE
OPTIONS 可选参数 运行gunicorn的配置选项,后面会讲到。
APP_MODULE 指定 wsgi application文件,书写格式 $(MODULE_NAME):$(VARIABLE_NAME)。其中 module_name用来制定将要运行的 wsgi application文件,可是一个完整的点缀名。比如当前目录 myapp 目录下有个 Python 包 gunicorn_app, gunicorn_app包下有一个wsgi application文件 test.py 则 module_name可以直接写成 gunicorn_app.test。viriable_name表示在 module_name 文件中要调用的对象(是一个WSGI callable, 可以是一个函数,类详情参看WSGI规格说明书)名。
按照上面的例子,当前目录为 /home/user/myapp, myapp中有一个包 gunicorn_app,test.py代码如下:
def app(environ, start_response):
"""Simplest possible application object"""
data = 'Hello, World!\n'
status = '200 OK'
response_headers = [
('Content-type','text/plain'),
('Content-Length', str(len(data)))
]
start_response(status, response_headers)
return iter([data])
我们将要运行 test.py文件中的 app(当然名字由你决定,可以是myapp,demo等等)
gunicorn gunicorn_app.test:app
命令中 module_name 为 gunicorn_app.test ;viriable_name为 app。当然如果我们这样直接运行Gunicorn的话,Gunicorn的所有配置都是默认值,后面会讲到如何配置Gunicorn。
gunicorn_django
guniorn_django命令是用来将 Django app部署到 Gunicorn Server上的。
其实也非常简单,原理和 gunicorn一样,只是gunicorn_django做了特殊处理,使得更加适合Django
基本用法
gunicorn_django [OPTIONS] [SETTINGS_PATH]
OPTIONS 前面已经说过了。
SETTINGS_PATH django app中 settings.py文件所在的目录,不写的话默认在当前目录下查找。比如:
gunicorn_django
这种用法适用以 Django 1.4 以前。
对于Django 1.4的版本以后推荐使用 gunicorn 命令(强烈推荐)
django_admin.py startproject mysite
cd mysite
gunicorn mysite.wsgi:application
还有一种方法是使用 Gunicorn内嵌的Djangohttps://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/gunicorn/不推荐。
至于 gunicorn_paster 有兴趣的可以研究 Gunicorn或者Paster官方文档。
Gunicorn配置
Gunicorn从三个不同的地方读取配置信息。
第一个地方:从framework定义的配置信息中读取,目前只对 Paster 框架有效。
第二个地方:在命令行中定义,命令行中定义的配置信息将会覆盖掉框架中定义的相同的参数名的值。
最后:将所有的参数信息,放到一个文件中,只要是在命令行中可以定义的参数中,在配置文件中都可以定义。(是一个Python源文件,所以你就像在写Python代码一样)
第一个地方不不介绍了,不实用。重点介绍第二种和第三种,其实这两种方式都是相同的。
显示说有配置选项:
gunicorn -h
使用命令行配置:
在上面的 myapp 例子的基础上
gunicorn --workers= --bind=127.0.0.1: myapp.test:app
上面的命令 启动 4个 workers ,绑定到 127.0.0.1:8000
使用配置文件
配置文件 config.py 源码
import multiprocessing bind = "127.0.0.1:8001"
workers = multiprocessing.cpu_count() * 2 + 1
使用配置文件启动Gunicorn
gunicorn --config=config.py myapp.test:app
和上面用命令行配置的效果完全一样。
当然两者还可以结合起来用:
gunicorn --config=gunicorn_conf.py --worker-class=eventlet myapp.test:app
worker-class默认是sync(同步),我们配置成了 eventlet(并发的)
Gunicorn的架构
服务模型(Server Model)
Gunicorn是基于 pre-fork 模型的。也就意味着有一个中心管理进程( master process )用来管理 worker 进程集合。Master从不知道任何关于客户端的信息。所有的请求和响应处理都是由 worker 进程来处理的。
Master(管理者)
主程序是一个简单的循环,监听各种信号以及相应的响应进程。master管理着正在运行的worker集合,通过监听各种信号比如TTIN, TTOU, and CHLD. TTIN and TTOU响应的增加和减少worker的数目。CHLD信号表明一个子进程已经结束了,在这种情况下master会自动的重启失败的worker。
Worker类型:
Sync Workers
The most basic and the default worker type is a synchronous worker class that handles a single request at a time. This model is the simplest to reason about as any errors will affect at most a single request. Though as we describe below only processing a single request at a time requires some assumptions about how applications are programmed.
Async Workers
The asynchronous workers available are based on Greenlets (via Eventlet and Gevent). Greenlets are an implementation of cooperative multi-threading for Python. In general, an application should be able to make use of these worker classes with no changes.
Tornado Workers
There’s also a Tornado worker class. It can be used to write applications using the Tornado framework. Although the Tornado workers are capable of serving a WSGI application, this is not a recommended configuration.
Choosing a Worker Type
The default synchronous workers assume that your application is resource bound in terms of CPU and network bandwidth. Generally this means that your application shouldn’t do anything that takes an undefined amount of time. For instance, a request to the internet meets this criteria. At some point the external network will fail in such a way that clients will pile up on your servers.
This resource bound assumption is why we require a buffering proxy in front of a default configuration Gunicorn. If you exposed synchronous workers to the internet, a DOS attack would be trivial by creating a load that trickles data to the servers. For the curious, Slowloris is an example of this type of load.
Some examples of behavior requiring asynchronous workers:
- Applications making long blocking calls (Ie, external web services)
- Serving requests directly to the internet
- Streaming requests and responses
- Long polling
- Web sockets
- Comet
How Many Workers?
DO NOT scale the number of workers to the number of clients you expect to have. Gunicorn should only need 4-12 worker processes to handle hundreds or thousands of requests per second.
Gunicorn relies on the operating system to provide all of the load balancing when handling requests. Generally we recommend (2 x $num_cores) + 1 as the number of workers to start off with. While not overly scientific, the formula is based on the assumption that for a given core, one worker will be reading or writing from the socket while the other worker is processing a request.
Obviously, your particular hardware and application are going to affect the optimal number of workers. Our recommendation is to start with the above guess and tune using TTIN and TTOU signals while the application is under load.
Always remember, there is such a thing as too many workers. After a point your worker processes will start thrashing system resources decreasing the throughput of the entire system。
Gunicorn快速入门的更多相关文章
- Web Api 入门实战 (快速入门+工具使用+不依赖IIS)
平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...
- SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=》提升)
SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=>提升,5个Demo贯彻全篇,感兴趣的玩才是真的学) 官方demo:http://www.asp.net/si ...
- 前端开发小白必学技能—非关系数据库又像关系数据库的MongoDB快速入门命令(2)
今天给大家道个歉,没有及时更新MongoDB快速入门的下篇,最近有点小忙,在此向博友们致歉.下面我将简单地说一下mongdb的一些基本命令以及我们日常开发过程中的一些问题.mongodb可以为我们提供 ...
- 【第三篇】ASP.NET MVC快速入门之安全策略(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- 【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- Mybatis框架 的快速入门
MyBatis 简介 什么是 MyBatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结果 ...
- grunt快速入门
快速入门 Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. Grunt 0.4.x 必须配合Node.js >= 0.8.0版本使用.:奇数版本 ...
- 【第一篇】ASP.NET MVC快速入门之数据库操作(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- 【第四篇】ASP.NET MVC快速入门之完整示例(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
随机推荐
- linux操作系统cron详解
Linux操作系统定时任务系统 Cron 入门 cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动 ...
- Oracle char 查询问题
近期碰到一个问题,数据库有个字段设置类型是char(2),然后数据库保存的数据位1,当使用hibernate 查询时 我使用query.setString("permissionLevel& ...
- SDL Game Development InputHandler类的一处bug
个人十分推荐SDL Game Development 这本书,它并不是死抠SDL的api,而是一步步带着我们如何对一个游戏进行构架. 虽然我没用过游戏引擎,也基本不会写游戏,但是我认为这本书本身就是在 ...
- [转] Linux抓包工具tcpdump详解
http://www.ha97.com/4550.html PS:tcpdump是一个用于截取网络分组,并输出分组内容的工具,简单说就是数据包抓包工具.tcpdump凭借强大的功能和灵活的截取策略,使 ...
- windows下jboss启动、配置、访问
window 下的jboss启动.配置.访问 1.进入jboss\server\default\deploy\jboss-web.deployer 执行run命令 2.jboss访问地址:http:/ ...
- instanceof的用法①
public class typeof1{ private String a="zzw"; public void instance(){ if(a instanceof Stri ...
- hibernate之增删改查demo
package dao; import java.util.ArrayList; import java.util.List; import org.hibernate.Query; import o ...
- 层叠样式优先级CSS
按照W3School网站(点这里直达)的说法,当同一个 HTML 元素被不止一个样式定义时,它们是有优先级之分的,如下,将优先级从小到大排列出来,其中4的优先级最高: 1.浏览器缺省设置2.外部样式表 ...
- 数据的动态合并和导出至EXCEL
最近一段时间都在处理数据的动态合并和导出EXCEL的问题,写个demo记录下,希望和我碰到同样问题的博友可以顺利解决:后面会提供demo下载链接. (VS2012,ASP.NET) 一.主要解决以下问 ...
- 将MVC中的Controllers、Model和View分别放到单独的项目中
Model: 新建-项目-Windows-类库 MVCTest.Model Controller:新建-项目-Windows-控制台应用程序 MVCTest.Bussiness Views:新建-项目 ...