python Gunicorn服务器使用方法详解
作者:jihite 时间:2021-09-06 00:35:08
目录
1. 简介
2. 示例代码1
3. 示例代码2
4. 启动异常
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 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来关闭进程
来源:https://www.cnblogs.com/kaituorensheng/p/6161128.html
标签:python,gunicorn,服务器
0
投稿
猜你喜欢
PHP中SimpleXML函数用法分析
2023-06-23 11:52:09
关于Pytorch中模型的保存与迁移问题
2023-08-11 04:05:25
Python谱减法语音降噪实例
2023-07-26 05:19:55
Python 3.8新特征之asyncio REPL
2023-10-08 02:59:58
启发式评估(heuristic evaluation)
2009-08-27 13:03:00
PHP结构型模式之装饰器模式
2023-05-30 08:43:07
python读取json文件并将数据插入到mongodb的方法
2021-03-22 20:30:22
PHP中MVC模式的模板引擎开发经验分享
2023-11-18 14:28:08
Oracle学习笔记(六)
2012-01-05 18:55:27
解决PHP mysql_query执行超时(Fatal error: Maximum execution time …)
2023-11-17 08:13:30
怎么样用xmlhttp读取远程xml的数据
2008-10-11 13:52:00
使用url_helper简化Python中Django框架的url配置教程
2023-01-10 14:44:51
网页特效文字之—银箔字
2013-08-07 00:21:39
PHP count()函数讲解
2023-06-04 11:46:41
JavaScript解决Joseph问题
2008-06-21 17:11:00
基于网格的网页设计概念及实际应用案例
2010-03-30 14:59:00
setInterval 和 setTimeout 会产生内存溢出
2008-03-08 13:10:00
计划备份mysql数据库
2009-03-09 14:34:00
python批量从es取数据的方法(文档数超过10000)
2022-03-23 01:21:48
python-yml文件读写与xml文件读写
2022-06-16 06:43:50