3

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?

Andrew
  • 735
  • 1
  • 8
  • 19

2 Answers2

4

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).

SColvin
  • 11,584
  • 6
  • 57
  • 71
  • I tried both cases and none of them is working. the same error – Andrew Jul 20 '18 at 11:37
  • Sorry for the previous message: it works with `web: python application.py` and `web.run_app(app, port=os.getenv('PORT', 5000))`. It was needed to create a new app manually, beacuse previos one for some reason didn't see the changes in Procfile. `gunicorn application:app --bind localhost:8080 --worker-class aiohttp.GunicornWebWorker` was not working. – Andrew Jul 20 '18 at 11:57
  • Second case didn't work because the port from `--bind localhost:8080` is wrong – SColvin Jul 20 '18 at 12:48
  • Is this a good approach to serve production via just ```python application.py```? – Marat Mkhitaryan Nov 14 '19 at 04:34
  • In short, "yes". heroku will take care of 1. the reverse proxy in front of aiohttp 2. restarting the dyno if the application crashed or had a memory leak and restart once a day. – SColvin Nov 14 '19 at 16:07
1

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
qix
  • 7,228
  • 1
  • 55
  • 65