要約
herokuでProcfileにweb: python run.py
と書くと動かなかったが、
web: gunicorn -k uvicorn.workers.UvicornWorker run:api
と書くと動いた。
以下詳細
responder
で簡単なアプリを作った。
run.py
import responder
import os
api = responder.API()
@api.route("/")
def index(req, resp):
resp.text = req.method
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5042))
api.run(port=port)
ルートに来たリクエストのメソッド(GET,POST...)を表示するだけの簡単なアプリである。
これをherokuに上げるため、次のようなProcfile
を書いた。
Procfile
web: python run.py
この状態でアプリケーションをherokuにアップロードしたとき、エラーが発生した。
log
2021-03-03T02:25:34.944654+00:00 heroku[web.1]: Starting process with command `python run.py`
2021-03-03T02:25:40.000000+00:00 app[api]: Build succeeded
2021-03-03T02:25:43.750097+00:00 heroku[web.1]: Process exited with status 1
2021-03-03T02:25:43.829241+00:00 heroku[web.1]: State changed from starting to crashed
2021-03-03T02:25:43.836710+00:00 heroku[web.1]: State changed from crashed to starting
2021-03-03T02:25:43.533551+00:00 app[web.1]: WARNING: You must pass the application as an import string to enable 'reload' or 'workers'.
2021-03-03T02:25:49.229908+00:00 heroku[web.1]: Starting process with command `python run.py`
2021-03-03T02:25:53.558366+00:00 heroku[web.1]: Process exited with status 1
2021-03-03T02:25:53.636099+00:00 heroku[web.1]: State changed from starting to crashed
2021-03-03T02:25:53.451857+00:00 app[web.1]: WARNING: You must pass the appli
cation as an import string to enable 'reload' or 'workers'.
Process exited with status 1
って言ってるが手元でpython run.py
をすると問題なく動く。
原理はよくわかっていないが、Procfileをgunicornを使う形式に変えたところ、動作した。
Procfile
web: gunicorn -k uvicorn.workers.UvicornWorker run:api
flaskだと単にgunicorn run:app
とかで行けると思われる。