Heroku烧瓶 - 从教程部署'模块化'的应用程序不工作,工头开始在本地工作 [英] Heroku Flask - Deploy a 'modular' app from tutorial not working, foreman start works locally

查看:182
本文介绍了Heroku烧瓶 - 从教程部署'模块化'的应用程序不工作,工头开始在本地工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于这个结构: http://flask.pocoo.org/docs/patterns / packages /



我也试过这个帖子:将Flask应用程序部署到Heroku



我无法在heroku上使用这个工具。我通常在60秒内没有设置PORT错误。我读过其他SO帖子,只是不知道如果我的项目结构是错误的或我的proc文件。我尝试了其他5000以上的端口。



这是我目前的项目结构:



  / myapplication 
procfile
runserver.py
/ applicationfolder
__init__.py
views.py



这是我的Procfile



  web:python runserver .py $ PORT 



这是我的runserver.py



  from applicationfolder import app 
app.run()
$ b if if __name__ =='__main__':
import os
port = int(os.environ.get('PORT',5000))
app.run(host ='0.0.0.0',port = port)



这是我的 init .py



  import os 
$ b $ from flask flask Flask $ b $ from flask import render_template,jsonify,request
$ b app = Flask(__ name__)

app.config.from_object('config')

导入applicationfolder.views


$ b

从这里开始views.py运行。

python runserver.py,但不能与heroku一起使用。我已经尝试了很多与PORT的东西,但港口似乎并没有设置一个不同的端口比5000.我认为这与我的项目结构。

解决方案

app.run()在那里两次,正如你所指出的是什么东西搞砸了。 app.run()调用一个简单的纯Python开发服务器,以便您可以轻松地运行和/或调试您的脚本。

通过在模块级调用它就在你的runserver.py文件中),你可以在python代码被加载的时候有效地启动开发服务器,然后当它从Procfile中调用的时候运行它,开发服务器已经在运行了从默认值开始(Flask的最新版本是从SERVER_NAME环境变量中提取相关的默认值)。通过在这两个地方,你试图调用该方法两次。



你基本上要么直接模块加载(在这种情况下,杀死代码下 name ...,或者在 main 下调用时使用该代码,在这种情况下,不要在模块加载时启动服务。


based on this structure: http://flask.pocoo.org/docs/patterns/packages/

I also tried this post: Deploying Flask app to Heroku

I am having trouble getting this to work on heroku. I usually get the PORT does not set within 60 seconds error. I have read other SO posts and just can't figure out if my project structure is wrong or my procfile. I tried other ports than 5000 as well.

Here is my current project structure:

/myapplication
    Procfile
    runserver.py
    /applicationfolder
        __init__.py
        views.py

Here is my Procfile

web: python runserver.py $PORT

Here is my runserver.py

from applicationfolder import app
app.run()

if __name__ == '__main__':
    import os
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

Here is my init.py

import os

from flask import Flask
from flask import render_template, jsonify, request

app = Flask(__name__)

app.config.from_object('config')

import applicationfolder.views

From there views.py runs.

This works locally with foreman start and python runserver.py, but does not work with heroku. I have tried many things with PORT but port doesn't seem to set even with a different PORT than 5000. I think it has something to do with my project structure.

解决方案

The app.run() was in there twice, which as you noted is what's screwing things up. The app.run() invokes a simply pure-python development server so that you can easily run and/or debug your script.

By invoking it at the module level (right under your import in runserver.py), you were effectively trying to start the development server as the python code was loaded, and then when it went to run it when invoked from the Procfile, the development server was already in flight, having been starting with it's defaults (latest version of Flask is pulling relevant defaults from the SERVER_NAME environment variable). By having it in both places, you were trying to invoke that method twice.

You basically want either the straight up module load (in which case, kill off the code under "if name ...", or you use the code when invoking under main, in which case don't start the service at module load time.

这篇关于Heroku烧瓶 - 从教程部署'模块化'的应用程序不工作,工头开始在本地工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆