1. 简介

Gunicorn(Green Unicorn)是给Unix用的WSGI HTTP 服务器,它与不同的web框架是非常兼容的、易安装、轻、速度快。

2. 示例代码1

def app(environ, start_response):
data = b"Hello World\n"
start_response("200 OK", [
("Content-Type", "test/plain"),
("Content-Length", str(len(data)))
])
return iter([data])

启动

gunicorn -w 4 myapp:app

起来后显示

[2016-12-12 00:20:12 +0000] [11755] [INFO] Starting gunicorn 19.6.0
[2016-12-12 00:20:12 +0000] [11755] [INFO] Listening at: http://127.0.0.1:8000 (11755)
[2016-12-12 00:20:12 +0000] [11755] [INFO] Using worker: sync
[2016-12-12 00:20:12 +0000] [11760] [INFO] Booting worker with pid: 11760
[2016-12-12 00:20:12 +0000] [11761] [INFO] Booting worker with pid: 11761
[2016-12-12 00:20:12 +0000] [11762] [INFO] Booting worker with pid: 11762
[2016-12-12 00:20:12 +0000] [11763] [INFO] Booting worker with pid: 11763

此时,调用http://127.0.0.1:8000

$curl http://127.0.0.1:8000
Hello World

参数说明

-w  处理HTTP请求的worker进程数,以下两种启动方式等价

gunicorn -w 4 myapp:app
gunicorn --workers=4 myapp:app

参考:

  -w INT, --workers INT
The number of worker processes for handling requests.

问题:为何调用 http://ip:8000不行呢, 这个是什么请求呢?

默认有-b参数,参考

  -b ADDRESS, --bind ADDRESS
The socket to bind. [['127.0.0.1:8000']]

以下方式启动就可以用ip的方式启动了

sudo gunicorn -w  -b 0.0.0.0: myapp:app

3. 示例代码2

之前简单的flask方法

from flask import Flask
app = Flask(__name__) @app.route('/hello.world')
def check():
return 'hello world!' if __name__ == '__main__':
app.run()

启动

$sudo gunicorn -b 0.0.0.0:  -w  myapp3:app
[-- :: +] [] [INFO] Starting gunicorn 19.6.
[-- :: +] [] [INFO] Listening at: http://0.0.0.0:300 (21005)
[-- :: +] [] [INFO] Using worker: sync
[-- :: +] [] [INFO] Booting worker with pid:
[-- :: +] [] [INFO] Booting worker with pid:
[-- :: +] [] [INFO] Booting worker with pid:
[-- :: +] [] [INFO] Booting worker with pid:

测试

$curl localhost:/hello.world
hello world!

4. 启动异常:[ERROR] Connection in use: ('127.0.0.1', 8000)

原因之一是之前启动的进程没有杀死。

注:ctrl+z 是挂起进程,但没有终止。ctrl+c是终止进程。

如果使用了ctrl+z再回到进程中可使用fg命令,这样可以用ctrl+c来关闭进程

python Gunicorn的更多相关文章

  1. python gunicorn详解

    Gunicorn是一个unix上被广泛使用的高性能的Python WSGI UNIX HTTP Server.和大多数的web框架兼容,并具有实现简单,轻量级,高性能等特点. gunicorn 安装 ...

  2. gunicorn Python部署应用

    对于flask应用 启动命令为 python app.py 使用gunicorn启动 pip install gunicorn python gunicorn --workers=7 switch_a ...

  3. 利用Haproxy搭建 HTTP 请求走私(Request smuggling)环境

    Haproxy 介绍 HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性.负载均衡,以及基于TCP和HTTP的应用程序代理. 请求走私(Request smuggling)概念证 ...

  4. virtualenv 环境下 Nginx + Flask + Gunicorn+ Supervisor 搭建 Python Web

    在这篇文章里,我们将搭建一个简单的 Web 应用,在虚拟环境中基于 Flask 框架,用 Gunicorn 做 wsgi 容器,用 Supervisor 管理进程,然后使用 Python 探针来监测应 ...

  5. Python第十三天 django 1.6 导入模板 定义数据模型 访问数据库 GET和POST方法 SimpleCMDB项目 urllib模块 urllib2模块 httplib模块 django和web服务器整合 wsgi模块 gunicorn模块

    Python第十三天   django 1.6   导入模板   定义数据模型   访问数据库   GET和POST方法    SimpleCMDB项目   urllib模块   urllib2模块 ...

  6. python之gunicorn的配置

    https://www.cnblogs.com/cwp-bg/p/8780204.html python常见的web部署搭配nginx+gunicorn,下面记录一下gunicorn的配置使用. 安装 ...

  7. 【Python】Centos + gunicorn+flask 报错ImportError: No module named request

    今天用Python去下载图片,用到了 urllib.request,这个是python3的方法.python2 使用的是urllib2 遇到了这么个问题后台报错,ImportError: No mod ...

  8. python独角兽 Flask + Gunicorn

    1.构建程序运行所需的虚拟环境 安装Miniconda 创建虚拟环境 添加程序运行依赖包 添加Gunicorn依赖 方式一:最简单的使用 easy_install 安装或者更新 方式二:下载源码安装 ...

  9. python web开发c6——阿里云上ubuntu+flask+gunicorn+nginx服务器部署(一)简单测试

    简述 Nginx在服务器部署中的作用 请求通过Nginx实现反向代理,将请求提交给代理服务器.本文中只用了一台服务器,所以是代理到本机. gunicorn的作用 作为服务器代码的容器.接收Nginx的 ...

随机推荐

  1. python环境变量自动配置脚本(setx使用)

    前言 setx不是windows系统自带的工具,需要到微软官网下载,但是有的系统也会自带.(是官方提供的,可放心食用) set和setx都可以用来配置环境变量.他们的不同点在于,set只是临时的修改环 ...

  2. pyhon 模块与库

    引用“http://www.iplaypython.com/module/” 引用“http://codingnow.cn/language/265.html” Python模块是什么? 玩蛇网:一个 ...

  3. 学习微信小程序之css2

    一:伪类的使用 .class-btn:active{ color: red;} <button class="class-btn">并集选择器</button&g ...

  4. php实现数据库数据读取生成缓存文件

    有些时候我们希望减少对数据库的 查询来提高程序的性能,因为这些数据不是经常变更的,而是会在很长一段时间内都不会变化,因此,我们每连接一次数据库,都会把相应的结果用文件的形式保存 起来.比如对于一个商城 ...

  5. nodejs review-04

    79 Secure your projects with HTTPS Express 生成SSL证书 openssl genrsa -out privkey.pem 1023 openssl req ...

  6. PHP-Redis扩展使用手册(三)

    /* 序列化key对应的value,如果key不存在则返回false * @param key * @return 序列化后的val或者false */ $redis->set('key_1', ...

  7. django 过滤器、日日期格式化参数

    转载:http://blog.csdn.net/xyp84/article/details/7945094 django1.4 html页面从数据库中读出DateTimeField字段时,显示的时间格 ...

  8. css3动态旋转魔方练习

    图片素材: html部分: <!DOCTYPE html><html><head> <meta charset="utf-8" /> ...

  9. cout中的执行顺序_a++和++a

    printf和cout从右到左计算: int main() { /* char* str = NULL; setmemory(&str, 100); strcpy(str, "hel ...

  10. iOS 循环引用

    1.循环引用一般是指:A持有B,B同时持有A,从而导致死循环无法释放对象. 2.一般循环引用出现在block和delegate中,而一般解决方法就是将self变成weakSelf(强引用变成弱引用), ...