问题
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