在 heroku 上部署 aiohttp

Deploy aiohttp on heroku

我在 aiohttp 上构建了一个简单的 Web 服务器并尝试将其部署在 heroku 上,但部署后我收到一条错误消息:

at=error code=H14 desc="No web processes running" dyno= connect= service= status=503 bytes= protocol=https

项目结构:

├── application.py
├── Procfile
├── requirements.txt
├── routes.py
└── views
    ├── bot_team_oranizer.py
    ├── index.py
    └── __init__.py

application.py

from aiohttp import web
from routes import setup_routes

app = web.Application()
setup_routes(app)
web.run_app(app)

Procfile:

web: gunicorn application:app

为什么网络服务器没有在 heroku 上启动?

可能 aiohttp 没有在正确的端口上侦听。你需要像 web.run_app(app, port=os.getenv('PORT')).

这样的东西

更新: 等等,你正试图用 gunicorn 和 web.run_app 来服务它,这是错误的,你需要要么有类似的东西web: python application.py 或删除 web.run_app(app).

如果您在 myapp.py 中有这样的应用程序,

import os
from aiohttp import web

#...define routes...

async def create_app():
    app = web.Application()
    app.add_routes(routes)
    return app

# If running directly https://docs.aiohttp.org/en/stable/web_quickstart.html
if __name__ == "__main__":
    port = int(os.environ.get('PORT', 8000))
    web.run_app(create_app(), port=port)

您可以 运行 它既可以通过 python CLI 在本地使用,也可以作为由 gunicorn 管理的工作进程使用 Procfile 类似于:

# use if you wish to run your server directly instead of via an application runner
#web: python myapp.py

# see https://docs.aiohttp.org/en/stable/deployment.html#nginx-gunicorn
#     https://devcenter.heroku.om/articles/python-gunicorn
#     http://docs.gunicorn.org/en/latest/run.html
web: gunicorn --bind 0.0.0.0:$PORT -k aiohttp.worker.GunicornWebWorker myapp:create_app