Deploy aiohttp on heroku

心已入冬 提交于 2021-02-10 12:29:50

问题


I have built a simple web server on aiohttp and try to deploy it on heroku, but after deployment I get an error message:

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

project structure:

├── 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

Why is the web server not starting on heroku?


回答1:


Probably aiohttp isn't listening on the right port. You need something like web.run_app(app, port=os.getenv('PORT')).

Update: wait, you're trying to serve it both with gunicorn and with web.run_app which is wrong, you'll need to either just have something like web: python application.py or remove the web.run_app(app).




回答2:


If you have an app in myapp.py like so,

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)

You can run it both locally via the python CLI but also as a worker process managed by gunicorn with a Procfile similar to this:

# 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


来源:https://stackoverflow.com/questions/51439912/deploy-aiohttp-on-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!