gpt4 book ai didi

heroku - 简单的 Flask 应用程序在本地运行,但当我部署到 heroku 时崩溃

转载 作者:行者123 更新时间:2023-12-03 00:43:53 25 4
gpt4 key购买 nike

这是一个简单的 flask 应用程序,代码如下:

from flask import Flask, render_template, url_for, flash, redirect
from forms import InviteForm
import requests
import os

app = Flask(__name__)

app.config['SECRET_KEY'] = os.environ["SECRET_KEY"]

@app.route("/home")
def landing_page():
return render_template('home.html')

@app.route("/", methods=['GET', 'POST'])
@app.route("/invite", methods=['GET', 'POST'])
def invite():
form = InviteForm()
if form.validate_on_submit():
invite_slack_result = invite_to_slack(form.email.data)
print(invite_slack_result)
if invite_slack_result["ok"]:
flash('Invite send at {}'.format(form.email.data, 'success'))
else:
flash(invite_slack_result)
return redirect(url_for('landing_page'))
return render_template('invite.html', title='Invite', form=form)

def invite_to_slack(user_email):
slack_request = 'https://slack.com/api/users.admin.invite?token='
slack_token = os.environ["SLACK_API_TOKEN"]
user_email = '&email=' + str(user_email)
url = slack_request.strip() + slack_token.strip() + user_email.strip()
r = requests.get(url)
data = r.json()
print(data["ok"])
return data

if __name__ == '__main__':
app.run()

我尝试了所有指南,但是当我部署到 heroku 应用程序时总是崩溃。这是日志:

›   Warning: heroku update available from 7.19.3 to 7.24.1
2019-05-03T09:55:43.480413+00:00 app[api]: Release v1 created by user myemail@gmail.com
2019-05-03T09:55:43.641038+00:00 app[api]: Enable Logplex by user myemail@gmail.com
2019-05-03T09:55:43.641038+00:00 app[api]: Release v2 created by user myemail@gmail.com
2019-05-03T09:55:43.480413+00:00 app[api]: Initial release by user myemail@gmail.com
2019-05-03T09:56:44.939393+00:00 heroku[router]: at=info code=H81 desc="Blank app" method=GET path="/" host=fast-temple-32581.herokuapp.com request_id=14db9962-9b66-4bf3-8745-ef09b221f655 fwd="46.176.119.138" dyno= connect= service= status=502 bytes= protocol=https
2019-05-03T09:56:46.072393+00:00 heroku[router]: at=info code=H81 desc="Blank app" method=GET path="/favicon.ico" host=fast-temple-32581.herokuapp.com request_id=44c5fe2b-2375-4139-955b-e7493847bf60 fwd="46.176.119.138" dyno= connect= service= status=502 bytes= protocol=https
2019-05-03T09:58:36.000000+00:00 app[api]: Build started by user myemail@gmail.com
2019-05-03T09:59:02.166369+00:00 app[api]: Release v3 created by user myemail@gmail.com
2019-05-03T09:59:02.166369+00:00 app[api]: Deploy 80d67888 by user myemail@gmail.com
2019-05-03T09:59:02.179942+00:00 app[api]: Scaled to web@1:Free by user myemail@gmail.com
2019-05-03T09:59:06.601864+00:00 heroku[web.1]: Starting process with command `gunicorn run:app`
2019-05-03T09:59:09.154085+00:00 heroku[web.1]: State changed from starting to crashed
2019-05-03T09:59:09.159463+00:00 heroku[web.1]: State changed from crashed to starting
2019-05-03T09:59:09.131387+00:00 heroku[web.1]: Process exited with status 3
2019-05-03T09:59:08.970428+00:00 app[web.1]: [2019-05-03 09:59:08 +0000] [4] [INFO] Starting gunicorn 19.9.0
2019-05-03T09:59:08.971230+00:00 app[web.1]: [2019-05-03 09:59:08 +0000] [4] [INFO] Listening at: http://0.0.0.0:38229 (4)
2019-05-03T09:59:08.977780+00:00 app[web.1]: [2019-05-03 09:59:08 +0000] [4] [INFO] Using worker: sync
2019-05-03T09:59:08.985422+00:00 app[web.1]: [2019-05-03 09:59:08 +0000] [10] [INFO] Booting worker with pid: 10
2019-05-03T09:59:08.992316+00:00 app[web.1]: [2019-05-03 09:59:08 +0000] [10] [ERROR] Exception in worker process
2019-05-03T09:59:08.992320+00:00 app[web.1]: Traceback (most recent call last):
2019-05-03T09:59:08.992323+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2019-05-03T09:59:08.992325+00:00 app[web.1]: worker.init_process()
2019-05-03T09:59:08.992326+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
2019-05-03T09:59:08.992328+00:00 app[web.1]: self.load_wsgi()
2019-05-03T09:59:08.992330+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
2019-05-03T09:59:08.992332+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2019-05-03T09:59:08.992333+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2019-05-03T09:59:08.992335+00:00 app[web.1]: self.callable = self.load()
2019-05-03T09:59:08.992337+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
2019-05-03T09:59:08.992339+00:00 app[web.1]: return self.load_wsgiapp()
2019-05-03T09:59:08.992340+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2019-05-03T09:59:08.992342+00:00 app[web.1]: return util.import_app(self.app_uri)
2019-05-03T09:59:08.992344+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
2019-05-03T09:59:08.992345+00:00 app[web.1]: __import__(module)
2019-05-03T09:59:08.992385+00:00 app[web.1]: ModuleNotFoundError: No module named 'run'
2019-05-03T09:59:08.992603+00:00 app[web.1]: [2019-05-03 09:59:08 +0000] [10] [INFO] Worker exiting (pid: 10)
2019-05-03T09:59:09.045407+00:00 app[web.1]: [2019-05-03 09:59:09 +0000] [4] [INFO] Shutting down: Master
2019-05-03T09:59:09.045727+00:00 app[web.1]: [2019-05-03 09:59:09 +0000] [4] [INFO] Reason: Worker failed to boot.
2019-05-03T09:59:10.000000+00:00 app[api]: Build succeeded
2019-05-03T09:59:12.048363+00:00 heroku[web.1]: Starting process with command `gunicorn run:app`
2019-05-03T09:59:14.458234+00:00 heroku[web.1]: State changed from starting to crashed
2019-05-03T09:59:14.447613+00:00 heroku[web.1]: Process exited with status 3
2019-05-03T09:59:14.324923+00:00 app[web.1]: [2019-05-03 09:59:14 +0000] [4] [INFO] Starting gunicorn 19.9.0
2019-05-03T09:59:14.325632+00:00 app[web.1]: [2019-05-03 09:59:14 +0000] [4] [INFO] Listening at: http://0.0.0.0:14930 (4)
2019-05-03T09:59:14.325973+00:00 app[web.1]: [2019-05-03 09:59:14 +0000] [4] [INFO] Using worker: sync
2019-05-03T09:59:14.331737+00:00 app[web.1]: [2019-05-03 09:59:14 +0000] [10] [INFO] Booting worker with pid: 10
2019-05-03T09:59:14.337117+00:00 app[web.1]: [2019-05-03 09:59:14 +0000] [10] [ERROR] Exception in worker process
2019-05-03T09:59:14.337120+00:00 app[web.1]: Traceback (most recent call last):
2019-05-03T09:59:14.337123+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2019-05-03T09:59:14.337124+00:00 app[web.1]: worker.init_process()
2019-05-03T09:59:14.337126+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
2019-05-03T09:59:14.337128+00:00 app[web.1]: self.load_wsgi()
2019-05-03T09:59:14.337129+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
2019-05-03T09:59:14.337131+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2019-05-03T09:59:14.337133+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2019-05-03T09:59:14.337135+00:00 app[web.1]: self.callable = self.load()
2019-05-03T09:59:14.337136+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
2019-05-03T09:59:14.337138+00:00 app[web.1]: return self.load_wsgiapp()
2019-05-03T09:59:14.337140+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2019-05-03T09:59:14.337141+00:00 app[web.1]: return util.import_app(self.app_uri)
2019-05-03T09:59:14.337143+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
2019-05-03T09:59:14.337145+00:00 app[web.1]: __import__(module)
2019-05-03T09:59:14.337146+00:00 app[web.1]: ModuleNotFoundError: No module named 'run'
2019-05-03T09:59:14.337279+00:00 app[web.1]: [2019-05-03 09:59:14 +0000] [10] [INFO] Worker exiting (pid: 10)
2019-05-03T09:59:14.365569+00:00 app[web.1]: [2019-05-03 09:59:14 +0000] [4] [INFO] Shutting down: Master
2019-05-03T09:59:14.365656+00:00 app[web.1]: [2019-05-03 09:59:14 +0000] [4] [INFO] Reason: Worker failed to boot.
2019-05-03T09:59:23.531289+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=fast-temple-32581.herokuapp.com request_id=23abb9dc-bfc5-4e3f-8460-3ac6367bc50c fwd="46.176.119.138" dyno= connect= service= status=503 bytes= protocol=https
2019-05-03T09:59:25.345486+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=fast-temple-32581.herokuapp.com request_id=ab5c577d-1ce1-4eb1-a2de-94840c8f13e5 fwd="46.176.119.138" dyno= connect= service= status=503 bytes= protocol=https

我几乎尝试了指南/视频/教程,但我总是遇到同样的问题,我不知道出了什么问题,此时我什至无法猜测

Here is thepastebin link to the error log

Here is the full project on github

最佳答案

Heroku 崩溃日志中有一些信息:

2019-05-03T09:59:06.601864+00:00 heroku[web.1]: Starting process with command `gunicorn run:app`
......
2019-05-03T09:59:08.992345+00:00 app[web.1]: __import__(module)
2019-05-03T09:59:08.992385+00:00 app[web.1]: ModuleNotFoundError: No module named 'run'

使用命令 `gunicorn run:app` 启动进程
看来您没有为gunicorn设置正确的WSGI应用

在项目根目录添加Procfile文件,
该文件的内容是:

web: gunicorn psgSlackInvite:app   

您项目的 wsgi 应用程序是“psgSlackInvite”模块中的“app”

关于heroku - 简单的 Flask 应用程序在本地运行,但当我部署到 heroku 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55967952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com