【问题标题】:Deploy aiohttp on heroku在heroku上部署aiohttp
【发布时间】:2018-12-28 14:39:19
【问题描述】:

我在 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)

过程文件

web: gunicorn application:app

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

【问题讨论】:

    标签: python heroku aiohttp


    【解决方案1】:

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

    更新: 等等,你试图同时使用 gunicorn 和 web.run_app 来服务它,这是错误的,你需要要么只使用 web: python application.py 之类的东西,要么删除 @ 987654324@.

    【讨论】:

    • 我尝试了这两种情况,但都没有工作。同样的错误
    • 抱歉之前的消息:它适用于web: python application.pyweb.run_app(app, port=os.getenv('PORT', 5000))。需要手动创建一个新应用程序,因为以前的一个由于某种原因没有看到 Procfile 中的更改。 gunicorn application:app --bind localhost:8080 --worker-class aiohttp.GunicornWebWorker 不工作。
    • 第二种情况不起作用,因为来自--bind localhost:8080的端口错误
    • 这是通过python application.py 为生产提供服务的好方法吗?
    • 简而言之,“是的”。 heroku 将负责 1. aiohttp 前面的反向代理 2. 如果应用程序崩溃或内存泄漏,则重新启动 dyno 并每天重新启动一次。
    【解决方案2】:

    如果你在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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 2017-06-24
      • 1970-01-01
      • 2014-09-12
      • 2019-11-19
      相关资源
      最近更新 更多