LoginSignup
0
4

More than 3 years have passed since last update.

HerokuにPython+Flaskをデプロイ時,R10 Errorが出た場合の解決法

Posted at

現象

以下ようなPython+FlaskコードをHerokuにデプロイ時,R10 Error (Boot timeout)が出てしまった.

import os
import requests
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["GET", 'POST'])
def download():
    .
    .
    .
    return jsonify(response)

if __name__ == '__main__':
    app.run(debug=False, host='0.0.0.0', port=5000)

問題点

ポート番号があっていなかったのがR10 Errorの原因だった.

Herokuは動的にポート番号を割り当てるようで,ポートオプションをport=5000と書いていたのがまずい点.

参考サイト:Heroku + node.js error (Web process failed to bind to $PORT within 60 seconds of launch)

解決法

os.environ.get()を使用して,以下のような表記とする.
これなら,Herokuが5000以外のポート番号を割り当てた時にも対応でき,R10 Errorを回避できる.

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

処理の説明

os.environ.get('PORT', 5000)は,環境変数'PORT'を取得を試みる.
├> 取得できた場合:その取得した値が返される.
└> 取得できない場合:5000が返される.

参考サイト

0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4