Heroku应用程序在本地运行

Heroku应用程序在本地运行

本文介绍了Heroku应用程序在本地运行,但获取H12超时错误(使用包)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经提出了类似的问题,但H12似乎是由许多事情引起的,这里没有任何答案适用。我之前已经用heroku构建了Python应用程序,但是现在我正在使用Miguel Grinberg的包结构,我弄不清楚有什么不对。

我的应用程序是python / Flask / SQLAlchemy用Heroku postgres数据库。该应用程序在本地完全运作当我推到Heroku时,我得到了H12。



这是我的应用程序结构:

  rootappdirectory \ 
app \
static\
模板\
__init__.py
views.py
模型。 py
run.py
[plus flask / venv files]

运行。 py看起来像这样:

 进口os $ b $从瓶子进口Flask 
$ b $从应用程序进口app
app.run()

app / __ init__.py看起来像这样: / b>

$ $ p $ (一堆进口)

app = Flask(__ name__)
db = SQLAlchemy (app)

login_manager = LoginManager()
(一堆login_manager的东西)

从应用程序导入视图,模型

我的Procfile是 web:gunicorn run:app



我在本地和远程使用heroku数据库。该应用程序在我的本地机器上完美工作(在0.0.0.0:5000)。但是当我推送到heroku并运行应用程序,这里是日志:

  2013-04-15T06:50:27.165532 + 00 :00 heroku [router]:at = error code = H12 desc =Request timeoutmethod = GET path = / favicon.ico host = floating-driftwood-6203.herokuapp.com fwd =24.6.48.240dyno = web。 1 connect = 1ms service = 30007ms status = 503 bytes = 0 
2013-04-15T06:50:34.908756 + 00:00 app [web.1]:2013-04-15 06:50:34 [2] [CRITICAL]工作者超时(pid:65)
2013-04-15T06:50:34.914436 + 00:00 app [web.1]:2013-04-15 06:50:34 [2] [CRITICAL] WORKER TIMEOUT(pid:65)
2013-04-15T06:50:34.918114 + 00:00 app [web.1]:2013-04-15 06:50:34 [66] [INFO] pid:66
2013-04-15T06:50:35.083182 + 00:00 app [web.1]:*运行于http://127.0.0.1:5000/
2013-04-15T06: 51:04.216671 + 00:00 app [web.1]:2013-04-15 06:51:04 [2] [CRITICAL] WORKER TIMEOUT(pid:66)
2013-04-15T06:51:04.223440 +00:00 app [web.1]:2013-04-15 06:51:04 [2] [CRITICAL] WORKER TIMEOUT(pid:66)
2013-04-15T06:51:04.229350 + 00: 00 ap p [web.1]:2013-04-15 06:51:04 [67] [INFO]用pid启动worker:67

我玩过不同的选项 - 起初我得到了一个连接中使用中的错误,我通过debug = False来修复,但坦率地说,我宁愿在debug = True!好像有一个问题,我的主要应用程序不在根目录中?



预先感谢。

不经意地调用 app.run - 这实际上是调用 werkzeug.serving.run_simple ,它启动一个子进程来处理传入的请求...你不想在gunicorn下运行因为gunicorn会为你处理进程管理)。

只要添加一个 if __name__ ==__main __ guard在你的 app.run 调用之前,一切都应该工作:

 #run .py 
if __name__ ==__main__:
app.run()


Similar questions have been asked but the H12 seems to be caused by many things and none of the answers apply here. I have built python apps with heroku before but now I'm using a package structure per Miguel Grinberg's Flask Mega-Tutorial and I can't figure out what's wrong.

My app is python / Flask / SQLAlchemy with a Heroku postgres database. The app fully works locally. When I push to Heroku, I get an H12.

Here is my app structure:

  rootappdirectory\
      app\
        static\
        templates\
        __init__.py
        views.py
        models.py
      run.py
      [plus flask / venv files]

run.py looks like this:

import os
from flask import Flask

from app import app
app.run()

And app/__init__.py looks like this:

(a bunch of imports)

app = Flask(__name__)
db = SQLAlchemy(app)

login_manager = LoginManager()
(a bunch of login_manager stuff)

from app import views, models

My Procfile is web: gunicorn run:app

I'm using the heroku database locally and remotely. The app works perfectly on my local machine (on 0.0.0.0:5000). But when I push to heroku and run the app, here's the logs:

2013-04-15T06:50:27.165532+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path=/favicon.ico host=floating-driftwood-6203.herokuapp.com fwd="24.6.48.240" dyno=web.1 connect=1ms service=30007ms status=503 bytes=0
2013-04-15T06:50:34.908756+00:00 app[web.1]: 2013-04-15 06:50:34 [2] [CRITICAL] WORKER TIMEOUT (pid:65)
2013-04-15T06:50:34.914436+00:00 app[web.1]: 2013-04-15 06:50:34 [2] [CRITICAL] WORKER TIMEOUT (pid:65)
2013-04-15T06:50:34.918114+00:00 app[web.1]: 2013-04-15 06:50:34 [66] [INFO] Booting worker with pid: 66
2013-04-15T06:50:35.083182+00:00 app[web.1]:  * Running on http://127.0.0.1:5000/
2013-04-15T06:51:04.216671+00:00 app[web.1]: 2013-04-15 06:51:04 [2] [CRITICAL] WORKER TIMEOUT (pid:66)
2013-04-15T06:51:04.223440+00:00 app[web.1]: 2013-04-15 06:51:04 [2] [CRITICAL] WORKER TIMEOUT (pid:66)
2013-04-15T06:51:04.229350+00:00 app[web.1]: 2013-04-15 06:51:04 [67] [INFO] Booting worker with pid: 67

I've played with different options - at first I got a "connection in use" error which I fixed by going into debug=False, but frankly I'd rather be in debug=True! There seems to be a problem with the fact that my main app is not in the root directory?

Thanks in advance.

解决方案

The issue is that run.py unguardedly calls app.run - this actually calls werkzeug.serving.run_simple which starts a sub-process to handle incoming requests ... which you don't want to do when running under gunicorn (since gunicorn will handle the process management for you).

Simply add an if __name__ == "__main__" guard before your app.run call and everything should work:

# run.py
if __name__ == "__main__":
    app.run()

这篇关于Heroku应用程序在本地运行,但获取H12超时错误(使用包)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 21:53