Search code examples
pythonherokuaiohttp

Deploy aiohttp web server on heroku


I'm trying to deploy a really simple aiohttp app to heroku, here's main.py file:

import os
from aiohttp import web

routes = web.RouteTableDef()

@routes.get('/')
async def handle(request):
    return web.Response(text='Welcome')


app = web.Application()
app.add_routes(routes)

if __name__ == '__main__':
    port = int(os.environ['PORT'])
    web.run_app(app, port=port)

And here's the Procfile

web: python main.py

It works fine in localhost, but when I upload it to heroku I get:

 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=******.herokuapp.com request_id=4e96418b-04bc-4bbb-bd4f-2b17320c3bc7 fwd="81.61.104.12" dyno= connect= service= status=503 bytes= protocol=https

Also, this question didn't help.


Solution

  • One line should help here, just try:

    web.run_app(app, port=os.getenv('PORT'))
    

    Instead of

    if __name__ == '__main__':
        port = int(os.environ['PORT'])
        web.run_app(app, port=port)