python Gunicorn服务器使用方法详解


Posted in Python onJuly 22, 2019

1. 简介

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

python Gunicorn服务器使用方法详解

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 2 -b 0.0.0.0:4000 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:300 -w 4 myapp3:app
[2016-12-18 19:19:51 +0000] [21005] [INFO] Starting gunicorn 19.6.0
[2016-12-18 19:19:51 +0000] [21005] [INFO] Listening at: http://0.0.0.0:300 (21005)
[2016-12-18 19:19:51 +0000] [21005] [INFO] Using worker: sync
[2016-12-18 19:19:51 +0000] [21010] [INFO] Booting worker with pid: 21010
[2016-12-18 19:19:51 +0000] [21011] [INFO] Booting worker with pid: 21011
[2016-12-18 19:19:51 +0000] [21014] [INFO] Booting worker with pid: 21014
[2016-12-18 19:19:51 +0000] [21017] [INFO] Booting worker with pid: 21017

测试

$curl localhost:300/hello.world
hello world!

4. 启动异常

[ERROR] Connection in use: ('127.0.0.1', 8000)

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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
使用python检测手机QQ在线状态的脚本代码
Feb 10 Python
python实现查询苹果手机维修进度
Mar 16 Python
Python中的sort()方法使用基础教程
Jan 08 Python
Python爬虫DNS解析缓存方法实例分析
Jun 02 Python
python flask中静态文件的管理方法
Mar 20 Python
[原创]Python入门教程2. 字符串基本操作【运算、格式化输出、常用函数】
Oct 29 Python
python RabbitMQ 使用详细介绍(小结)
Nov 08 Python
Flask中endpoint的理解(小结)
Dec 11 Python
使用pytorch实现论文中的unet网络
Jun 24 Python
python三引号如何输入
Jul 06 Python
详解如何使用Pytest进行自动化测试
Jan 14 Python
10张动图学会python循环与递归问题
Feb 06 Python
python实现按行分割文件
Jul 22 #Python
python UDP(udp)协议发送和接收的实例
Jul 22 #Python
linux环境下Django的安装配置详解
Jul 22 #Python
python判断一个对象是否可迭代的例子
Jul 22 #Python
树莓派使用python-librtmp实现rtmp推流h264的方法
Jul 22 #Python
python实现大文件分割与合并
Jul 22 #Python
cProfile Python性能分析工具使用详解
Jul 22 #Python
You might like
php 全文搜索和替换的实现代码
2008/07/29 PHP
PHP的可变变量名的使用方法分享
2012/02/05 PHP
phpcms模块开发之swfupload的使用介绍
2013/04/28 PHP
关于PHPDocument 代码注释规范的总结
2013/06/25 PHP
php inc文件使用的风险和注意事项
2013/11/12 PHP
TP框架实现上传一张图片和批量上传图片的方法分析
2020/04/23 PHP
javascript编程起步(第七课)
2007/01/10 Javascript
NiftyCube——轻松实现圆角边框
2007/02/20 Javascript
javascript appendChild,innerHTML,join性能比较代码
2009/08/29 Javascript
25个优雅的jQuery Tooltip插件推荐
2011/05/25 Javascript
jquery插件开发注意事项小结
2013/06/04 Javascript
JS使用ajax方法获取指定url的head信息中指定字段值的方法
2015/03/24 Javascript
angularjs学习笔记之简单介绍
2015/09/26 Javascript
JavaScript实现的简单烟花特效代码
2015/10/20 Javascript
详解基于Bootstrap扁平化的后台框架Ace
2015/11/27 Javascript
Javascript实现汉字和拼音互转的终极方案
2016/10/19 Javascript
Bootstrap中定制LESS-颜色及导航条(推荐)
2016/11/21 Javascript
简单实现js无缝滚动效果
2017/02/05 Javascript
AngularJS表格添加序号的方法
2017/03/03 Javascript
轻松学习JavaScript函数中的 Rest 参数
2019/05/30 Javascript
JS中自定义事件的使用与触发操作实例分析
2019/11/01 Javascript
python实现调用其他python脚本的方法
2014/10/05 Python
Python实现多进程共享数据的方法分析
2017/12/04 Python
python kmeans聚类简单介绍和实现代码
2018/02/23 Python
Python 获取numpy.array索引值的实例
2019/12/06 Python
django创建css文件夹的具体方法
2020/07/31 Python
html5读取本地文件示例代码
2014/04/22 HTML / CSS
WINDOWS域的具体实现方式是什么
2014/02/20 面试题
大学生村官工作感言
2014/01/10 职场文书
六月份红领巾广播稿
2014/02/03 职场文书
小学语文课后反思精选
2014/04/25 职场文书
法人授权委托书范本
2014/09/17 职场文书
银行贷款收入证明
2014/10/17 职场文书
董事会决议范本
2015/07/01 职场文书
文明和谐家庭事迹材料(2016精选版)
2016/02/29 职场文书
经典人生语录分享:不畏将来,不念过去,笑对当下
2019/12/12 职场文书