python Gunicorn服务器使用方法详解
Python  /  管理员 发布于 7年前   220
Gunicorn(Green Unicorn)是给Unix用的WSGI HTTP 服务器,它与不同的web框架是非常兼容的、易安装、轻、速度快。
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:8000Hello World
参数说明
-w 处理HTTP请求的worker进程数,以下两种启动方式等价
gunicorn -w 4 myapp:appgunicorn --workers=4 myapp:app
参考:
-w INT, --workers INTThe number of worker processes for handling requests.
问题:为何调用 http://ip:8000不行呢, 这个是什么请求呢?
默认有-b参数,参考
-b ADDRESS, --bind ADDRESSThe socket to bind. [['127.0.0.1:8000']]
以下方式启动就可以用ip的方式启动了
sudo gunicorn -w 2 -b 0.0.0.0:4000 myapp:app
之前简单的flask方法
from flask import Flaskapp = 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.worldhello world!
[ERROR] Connection in use: ('127.0.0.1', 8000)
原因之一是之前启动的进程没有杀死。
注:ctrl+z 是挂起进程,但没有终止。ctrl+c是终止进程。
如果使用了ctrl+z再回到进程中可使用fg命令,这样可以用ctrl+c来关闭进程
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号