在Heroku上设置gunicorn和bottle
我在 Heroku 上玩 Bottle 框架,想换成一个更适合“生产环境”的 WSGI 服务器,叫做 gunicorn。以下是我的设置:
import os
import bottle
from bottle import route, run, template, request, static_file, error
class StripPathMiddleware(object):
def __init__(self, app):
self.app = app
def __call__(self, e, h):
e['PATH_INFO'] = e['PATH_INFO'].rstrip('/')
return self.app(e, h)
# ................................................................. #
@error(404)
def error404(error):
return template('view/404.tpl')
app = bottle.app()
run(app=StripPathMiddleware(app), server='gunicorn', host='0.0.0.0', port=int(os.environ.get("PORT", 5000)), debug=True, workers=3)
这是我的 Procfile:
web: gunicorn SimpleServer:app -w 3
我尝试过在 SimpleServer.py 应用中设置和不设置工作进程的数量。
在本地机器上,只有当我在应用中设置 workers=3,而且在启动 gunicorn 时不指定工作进程数量,它才能正常工作:
(bottleServ)caerus@Artem:~/bottleServ$ gunicorn SimpleServer:app
2013-02-02 23:23:48 [18133] [INFO] Starting gunicorn 0.17.2
2013-02-02 23:23:48 [18133] [INFO] Listening at: http://127.0.0.1:8000 (18133)
2013-02-02 23:23:48 [18133] [INFO] Using worker: sync
2013-02-02 23:23:48 [18138] [INFO] Booting worker with pid: 18138
Bottle v0.11.6 server starting up (using GunicornServer(workers=3))...
Listening on http://0.0.0.0:5000/
Hit Ctrl-C to quit.
2013-02-02 23:23:48 [18138] [INFO] Starting gunicorn 0.17.2
2013-02-02 23:23:48 [18138] [INFO] Listening at: http://0.0.0.0:5000 (18138)
2013-02-02 23:23:48 [18138] [INFO] Using worker: sync
2013-02-02 23:23:48 [18139] [INFO] Booting worker with pid: 18139
2013-02-02 23:23:48 [18140] [INFO] Booting worker with pid: 18140
2013-02-02 23:23:48 [18141] [INFO] Booting worker with pid: 18141
但是在使用 foreman start 时遇到了问题,不管我用什么设置,都会出现:
(bottleServ)caerus@Artem:~/bottleServ$ foreman start
23:31:57 web.1 | started with pid 18192
23:31:58 web.1 | 2013-02-02 23:31:58 [18195] [INFO] Starting gunicorn 0.17.2
23:31:58 web.1 | 2013-02-02 23:31:58 [18195] [INFO] Listening at: http://0.0.0.0:5000 (18195)
23:31:58 web.1 | 2013-02-02 23:31:58 [18195] [INFO] Using worker: sync
23:31:58 web.1 | 2013-02-02 23:31:58 [18200] [INFO] Booting worker with pid: 18200
23:31:58 web.1 | 2013-02-02 23:31:58 [18201] [INFO] Booting worker with pid: 18201
23:31:58 web.1 | 2013-02-02 23:31:58 [18202] [INFO] Booting worker with pid: 18202
23:31:58 web.1 | Bottle v0.11.6 server starting up (using GunicornServer(workers=3))...
23:31:58 web.1 | Listening on http://0.0.0.0:5000/
23:31:58 web.1 | Hit Ctrl-C to quit.
23:31:58 web.1 | 2013-02-02 23:31:58 [18202] [INFO] Starting gunicorn 0.17.2
23:31:58 web.1 | 2013-02-02 23:31:58 [18202] [ERROR] Connection in use: ('0.0.0.0', 5000)
23:31:58 web.1 | 2013-02-02 23:31:58 [18202] [ERROR] Retrying in 1 second.
23:31:58 web.1 | Bottle v0.11.6 server starting up (using GunicornServer(workers=3))...
23:31:58 web.1 | Bottle v0.11.6 server starting up (using GunicornServer(workers=3))...
23:31:58 web.1 | Listening on http://0.0.0.0:5000/
23:31:58 web.1 | Listening on http://0.0.0.0:5000/
23:31:58 web.1 | Hit Ctrl-C to quit.
23:31:58 web.1 | Hit Ctrl-C to quit.
23:31:58 web.1 | 2013-02-02 23:31:58 [18200] [INFO] Starting gunicorn 0.17.2
23:31:58 web.1 | 2013-02-02 23:31:58 [18201] [INFO] Starting gunicorn 0.17.2
23:31:58 web.1 | 2013-02-02 23:31:58 [18200] [ERROR] Connection in use: ('0.0.0.0', 5000)
23:31:58 web.1 | 2013-02-02 23:31:58 [18201] [ERROR] Connection in use: ('0.0.0.0', 5000)
23:31:58 web.1 | 2013-02-02 23:31:58 [18200] [ERROR] Retrying in 1 second.
23:31:58 web.1 | 2013-02-02 23:31:58 [18201] [ERROR] Retrying in 1 second.
如果有任何想法,我会很感激!)
1 个回答
3
你在用gunicorn启动应用程序,然后又用Bottle的'run'来启动另一个服务器(至少在Heroku上是这样)。你遇到错误的原因是,最开始的服务器占用了5000端口,而第二个服务器无法访问这个端口。试着直接运行你的Bottle应用(python SimpleServer.py),这样它应该会自己创建服务器。此外,当你把'app'传给run时,http服务器会再启动一个你应用的副本(这又会启动另一个gunicorn服务器),所以只需把这个去掉就可以了。
run(server='gunicorn', host='0.0.0.0', port=int(os.environ.get("PORT", 5000)), debug=True, workers=X)
python SimpleServer.py
这应该就是你需要的全部了。