1
2

More than 3 years have passed since last update.

herokuでのProcfileの書き方

Posted at

要約

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とかで行けると思われる。

1
2
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
1
2