iT邦幫忙

0

HEROKU部署flask失敗的問題code=H10

使用Flask在HEROKU部屬,首先遇到上傳失敗(remote rejected)的問題,不過後來已解決,接著,上傳成功後又遇到應用程式錯誤(application error)的問題,以下詳列我的步驟,想請教問題的所在 :) 。

  1. 建立一個資料夾python-flask2,
    在此資料夾中建立app.py、Procfile、requirements.txt、runtime.txt(但後來發現runtime.txt刪除後才能順利上傳,因此runtime.txt沒有寫!)

  2. python-flask2資料夾中新增app.py,其中程式碼如下:

from flask import Flask #載入Flask
app=Flask(__name__) #建立application 物件

#建立網站首頁的回應方式
@app.route("/") 
def index(): #用來回應網頁首頁連線的函式
    return "hello flask!!!!!吃早餐了"

@app.route("/page1")
def info():
    return "相關資訊"
#啟動伺服器
app.run()
  1. python-flask2資料夾中新增Procfile,內容如下:
web: gunicorn app:app
  1. python-flask2資料夾中新增requirements.txt,內容如下:
Flask
gunicorn
  1. Git下載; HEROKU下載CLI、建立APP
  2. 按照HEROKU的部屬指令:
$ heroku login
$ git init
$ heroku git:remote -a python-chiao
$ git add .
$ git commit -am "make it better"
$ git push heroku master

我的終端機執行內容

PS D:\python-flask2> heroku login
heroku: Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/cli/browser/c5ce8641-790c-4f4c-8f5b-3f60eeb5568c?requestor=SFMyNTY.g2gDbQAAAA01OC4xMTUuMTE5LjM2bgYA7u_74ncBYgABUYA.k1M26UBLr2UxfzSHP9gq57Fmr5CKIhn0Y1InaREgGpc
Logging in... done
Logged in as elsa2700@gmail.com
PS D:\python-flask2> git init
Reinitialized existing Git repository in D:/python-flask2/.git/
PS D:\python-flask2> heroku git:remote -a python-chiao
set git remote heroku to https://git.heroku.com/python-chiao.git
PS D:\python-flask2> git add .
PS D:\python-flask2> git commit -am "make it better"
On branch master
nothing to commit, working tree clean
PS D:\python-flask2> git push heroku master
Enumerating objects: 20, done.
Counting objects: 100% (20/20), done.
Delta compression using up to 4 threads
Compressing objects: 100% (14/14), done.
Writing objects: 100% (20/20), 1.83 KiB | 374.00 KiB/s, done.
Total 20 (delta 4), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Building on the Heroku-20 stack
remote: -----> Python app detected
remote: -----> Installing python-3.6.13
remote: -----> Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2    
remote: -----> Installing SQLite3
remote: -----> Installing requirements with pip
remote:        Collecting Flask
remote:          Downloading Flask-1.1.2-py2.py3-none-any.whl (94 kB)       
remote:        Collecting gunicorn
remote:          Downloading gunicorn-20.0.4-py2.py3-none-any.whl (77 kB)   
remote:        Collecting Jinja2>=2.10.1
remote:          Downloading Jinja2-2.11.3-py2.py3-none-any.whl (125 kB)    
remote:        Collecting itsdangerous>=0.24
remote:          Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
remote:        Collecting Werkzeug>=0.15
remote:          Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)   
remote:        Collecting click>=5.1
remote:          Downloading click-7.1.2-py2.py3-none-any.whl (82 kB)       
remote:        Collecting MarkupSafe>=0.23
remote:          Downloading MarkupSafe-1.1.1-cp36-cp36m-manylinux2010_x86_64.whl (32 kB)
remote:        Installing collected packages: MarkupSafe, Jinja2, itsdangerous, Werkzeug, click, Flask, gunicorn
remote:        Successfully installed Flask-1.1.2 Jinja2-2.11.3 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 gunicorn-20.0.4 itsdangerous-1.1.0
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 48.3M
remote: -----> Launching...
remote:        Released v3
remote:        https://python-chiao.herokuapp.com/ deployed to Heroku       
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/python-chiao.git
 * [new branch]      master -> master
  1. 結果
    開啟https://python-chiao.herokuapp.com ,出現Application error。
    檢查logs內容發現每次出現的錯誤代碼都不同,主要是H12、H10、H13,查詢到這些錯誤大致跟啟動超時有關,想請教不曉得針對超時的問題,是否相關經驗的人可提供建議的解決方法?或是根據上述的步驟是否有哪裡出了問題? /images/emoticon/emoticon41.gif
PS D:\python-flask2> heroku logs --tail
2021-02-27T09:49:36.177329+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-02-27T09:49:36.177330+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2021-02-27T09:49:36.177330+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2021-02-27T09:49:36.177330+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
2021-02-27T09:49:36.177331+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
2021-02-27T09:49:36.177331+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 678, in exec_module
2021-02-27T09:49:36.177331+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-02-27T09:49:36.177332+00:00 app[web.1]: File "/app/app.py", line 13, in <module>
2021-02-27T09:49:36.177332+00:00 app[web.1]: app.run()
2021-02-27T09:49:36.177333+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 990, in run
2021-02-27T09:49:36.177333+00:00 app[web.1]: run_simple(host, port, self, **options)
2021-02-27T09:49:36.177334+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/serving.py", line 1052, in run_simple       
2021-02-27T09:49:36.177334+00:00 app[web.1]: inner()
2021-02-27T09:49:36.177335+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/serving.py", line 1005, in inner
2021-02-27T09:49:36.177335+00:00 app[web.1]: fd=fd,
2021-02-27T09:49:36.177336+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/serving.py", line 848, in make_server       
2021-02-27T09:49:36.177336+00:00 app[web.1]: host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
2021-02-27T09:49:36.177337+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/serving.py", line 740, in __init__
2021-02-27T09:49:36.177337+00:00 app[web.1]: HTTPServer.__init__(self, server_address, handler)
2021-02-27T09:49:36.177337+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/socketserver.py", line 456, in __init__
2021-02-27T09:49:36.177338+00:00 app[web.1]: self.server_bind()
2021-02-27T09:49:36.177338+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/http/server.py", line 136, in server_bind
2021-02-27T09:49:36.177338+00:00 app[web.1]: socketserver.TCPServer.server_bind(self)
2021-02-27T09:49:36.177339+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/socketserver.py", line 470, in server_bind
2021-02-27T09:49:36.177339+00:00 app[web.1]: self.socket.bind(self.server_address)
2021-02-27T09:49:36.177403+00:00 app[web.1]: OSError: [Errno 98] Address already in use
2021-02-27T09:49:36.178153+00:00 app[web.1]: [2021-02-27 09:49:36 +0000] [9] [INFO] Worker exiting (pid: 9)
2021-02-27T09:50:06.423160+00:00 app[web.1]: [2021-02-27 09:50:06 +0000] [4] [INFO] Shutting down: Master
2021-02-27T09:50:06.423290+00:00 app[web.1]: [2021-02-27 09:50:06 +0000] [4] [INFO] Reason: Worker failed to boot.
2021-02-27T09:50:06.558888+00:00 heroku[web.1]: Process exited with status 32021-02-27T09:50:06.648695+00:00 heroku[web.1]: State changed from up to crashed
2021-02-27T09:50:06.652218+00:00 heroku[web.1]: State changed from crashed to starting
2021-02-27T09:50:09.649468+00:00 heroku[web.1]: Starting process with command `gunicorn app:app`
2021-02-27T09:50:11.987951+00:00 app[web.1]: [2021-02-27 09:50:11 +0000] [4] [INFO] Starting gunicorn 20.0.4
2021-02-27T09:50:11.988962+00:00 app[web.1]: [2021-02-27 09:50:11 +0000] [4] [INFO] Listening at: http://0.0.0.0:14574 (4)
2021-02-27T09:50:11.989162+00:00 app[web.1]: [2021-02-27 09:50:11 +0000] [4] [INFO] Using worker: sync
2021-02-27T09:50:11.994651+00:00 app[web.1]: [2021-02-27 09:50:11 +0000] [9] [INFO] Booting worker with pid: 9
2021-02-27T09:50:12.074522+00:00 app[web.1]: [2021-02-27 09:50:12 +0000] [10] [INFO] Booting worker with pid: 10
2021-02-27T09:50:12.173481+00:00 app[web.1]: * Serving Flask app "app" (lazy loading)
2021-02-27T09:50:12.173548+00:00 app[web.1]: * Environment: production
2021-02-27T09:50:12.173615+00:00 app[web.1]: WARNING: This is a development 
server. Do not use it in a production deployment.
2021-02-27T09:50:12.173674+00:00 app[web.1]: Use a production WSGI server instead.
2021-02-27T09:50:12.173723+00:00 app[web.1]: * Debug mode: off
2021-02-27T09:50:12.176693+00:00 app[web.1]: * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
2021-02-27T09:50:12.246982+00:00 app[web.1]: * Serving Flask app "app" (lazy loading)
2021-02-27T09:50:12.247014+00:00 app[web.1]: * Environment: production      
2021-02-27T09:50:12.247053+00:00 app[web.1]: WARNING: This is a development 
server. Do not use it in a production deployment.
2021-02-27T09:50:12.247125+00:00 app[web.1]: Use a production WSGI server instead.
2021-02-27T09:50:12.247187+00:00 app[web.1]: * Debug mode: off
2021-02-27T09:50:12.252818+00:00 app[web.1]: [2021-02-27 09:50:12 +0000] [10] [ERROR] Exception in worker process
2021-02-27T09:50:12.252819+00:00 app[web.1]: Traceback (most recent call last):
2021-02-27T09:50:12.252827+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker      
2021-02-27T09:50:12.252828+00:00 app[web.1]: worker.init_process()
2021-02-27T09:50:12.252828+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 119, in init_process 
2021-02-27T09:50:12.252828+00:00 app[web.1]: self.load_wsgi()
2021-02-27T09:50:12.252828+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi    
2021-02-27T09:50:12.252829+00:00 app[web.1]: self.wsgi = self.app.wsgi()    
2021-02-27T09:50:12.252829+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2021-02-27T09:50:12.252829+00:00 app[web.1]: self.callable = self.load()    
2021-02-27T09:50:12.252829+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
2021-02-27T09:50:12.252830+00:00 app[web.1]: return self.load_wsgiapp()     
2021-02-27T09:50:12.252830+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp   
2021-02-27T09:50:12.252830+00:00 app[web.1]: return util.import_app(self.app_uri)
2021-02-27T09:50:12.252830+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 358, in import_app
2021-02-27T09:50:12.252830+00:00 app[web.1]: mod = importlib.import_module(module)
2021-02-27T09:50:12.252830+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
2021-02-27T09:50:12.252831+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-02-27T09:50:12.252831+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2021-02-27T09:50:12.252831+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2021-02-27T09:50:12.252831+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
2021-02-27T09:50:12.252831+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
2021-02-27T09:50:12.252832+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 678, in exec_module
2021-02-27T09:50:12.252832+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-02-27T09:50:12.252832+00:00 app[web.1]: File "/app/app.py", line 13, in <module>
2021-02-27T09:50:12.252832+00:00 app[web.1]: app.run()
2021-02-27T09:50:12.252832+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 990, in run
2021-02-27T09:50:12.252833+00:00 app[web.1]: run_simple(host, port, self, **options)
2021-02-27T09:50:12.252833+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/serving.py", line 1052, in run_simple       
2021-02-27T09:50:12.252833+00:00 app[web.1]: inner()
2021-02-27T09:50:12.252833+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/serving.py", line 1005, in inner
2021-02-27T09:50:12.252833+00:00 app[web.1]: fd=fd,
2021-02-27T09:50:12.252833+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/serving.py", line 848, in make_server       
2021-02-27T09:50:12.252834+00:00 app[web.1]: host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
2021-02-27T09:50:12.252834+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/serving.py", line 740, in __init__
2021-02-27T09:50:12.252834+00:00 app[web.1]: HTTPServer.__init__(self, server_address, handler)
2021-02-27T09:50:12.252834+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/socketserver.py", line 456, in __init__
2021-02-27T09:50:12.252834+00:00 app[web.1]: self.server_bind()
2021-02-27T09:50:12.252835+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/http/server.py", line 136, in server_bind
2021-02-27T09:50:12.252835+00:00 app[web.1]: socketserver.TCPServer.server_bind(self)
2021-02-27T09:50:12.252835+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/socketserver.py", line 470, in server_bind
2021-02-27T09:50:12.252835+00:00 app[web.1]: self.socket.bind(self.server_address)
2021-02-27T09:50:12.252839+00:00 app[web.1]: OSError: [Errno 98] Address already in use
2021-02-27T09:50:12.253238+00:00 app[web.1]: [2021-02-27 09:50:12 +0000] [10] [INFO] Worker exiting (pid: 10)
2021-02-27T09:50:12.543269+00:00 heroku[web.1]: State changed from starting 
to up
2021-02-27T09:50:42.377371+00:00 app[web.1]: [2021-02-27 09:50:42 +0000] [4] [INFO] Shutting down: Master
2021-02-27T09:50:42.377481+00:00 app[web.1]: [2021-02-27 09:50:42 +0000] [4] [INFO] Reason: Worker failed to boot.
2021-02-27T09:50:42.457277+00:00 heroku[web.1]: Process exited with status 32021-02-27T09:50:42.518202+00:00 heroku[web.1]: State changed from up to crashed
2021-02-27T09:51:11.163513+00:00 heroku[router]: at=error code=H10 desc="App2021-02-27T09:50:12.252835+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/socketserver.py", line 470, in server_bind
2021-02-27T09:50:12.252835+00:00 app[web.1]: self.socket.bind(self.server_address)
2021-02-27T09:50:12.252839+00:00 app[web.1]: OSError: [Errno 98] Address already in use
2021-02-27T09:50:12.253238+00:00 app[web.1]: [2021-02-27 09:50:12 +0000] [10] [INFO] Worker exiting (pid: 10)
2021-02-27T09:50:12.543269+00:00 heroku[web.1]: State changed from starting 
to up
2021-02-27T09:50:42.377371+00:00 app[web.1]: [2021-02-27 09:50:42 +0000] [4] [INFO] Shutting down: Master
2021-02-27T09:50:42.377481+00:00 app[web.1]: [2021-02-27 09:50:42 +0000] [4] [INFO] Reason: Worker failed to boot.
2021-02-27T09:50:42.457277+00:00 heroku[web.1]: Process exited with status 32021-02-27T09:50:42.518202+00:00 heroku[web.1]: State changed from up to crashed
2021-02-27T09:51:11.163513+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=python-chiao.herokuapp.com request_id=913059d3-6d39-45f1-aafc-9a832b6161f8 fwd="58.115.119.36" dyno= connect= service= status=503 bytes= protocol=https
2021-02-27T09:51:11.567397+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=python-chiao.herokuapp.com request_id=f84e101c-fdf8-4adb-ae32-c9f8b11f3b51 fwd="58.115.119.36" dyno= connect= service= status=503 bytes= protocol=https
2021-02-27T10:08:52.216020+00:00 heroku[web.1]: State changed from crashed to down
增廣建文 iT邦研究生 5 級 ‧ 2021-06-22 19:38:13 檢舉
猜port問題
https://stackoverflow.com/questions/42019551/python-flask-heroku-application-error
arguskao iT邦新手 4 級 ‧ 2022-04-28 10:28:46 檢舉
我也是一樣,但是錯誤是其他
也找不到人問
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答