python - Heroku 的 Gunicorn 和 Bottle 设置

标签 python heroku wsgi bottle gunicorn

我在 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 应用程序中尝试了设置或不设置工作人员数量的 Procfile

在本地计算机上,只有在应用程序中我有 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.

任何想法将不胜感激! )

最佳答案

您正在启动gunicorn来运行该应用程序,然后使用Bottle's '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

应该是您所需要的。

关于python - Heroku 的 Gunicorn 和 Bottle 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14666267/

相关文章:

python - Google App Engine 应用程序缓存

python - CherryPy 和并发

python - 获取列表列表的 powerset

python - 将 kwargs 添加到带有字典参数的 pandas apply 语句

python - 将 Django 应用程序部署到 Heroku 会导致错误worker failed to boot

ruby-on-rails - Rails 配置 keystore 返回 nil

python - 在 python 中从 .dat 文件读取和计算

Python basemap 模块无法导入

ruby-on-rails-3 - Sqlite3 引用部署简单的 heroku Rails 应用程序

python - Pyramid 如何设置api中间件?