Python 在heroku上部署aiohttp
我在aiohttp上构建了一个简单的web服务器,并尝试在heroku上部署它,但部署后我收到一条错误消息: at=错误代码=H14 desc=“没有正在运行的web进程”dyno=connect=service=status=503字节=protocol=https 项目结构:Python 在heroku上部署aiohttp,python,heroku,aiohttp,Python,Heroku,Aiohttp,我在aiohttp上构建了一个简单的web服务器,并尝试在heroku上部署它,但部署后我收到一条错误消息: at=错误代码=H14 desc=“没有正在运行的web进程”dyno=connect=service=status=503字节=protocol=https 项目结构: ├── application.py ├── Procfile ├── requirements.txt ├── routes.py └── views ├── bot_team_oranizer.py
├── 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应用程序:app
为什么web服务器没有在heroku上启动?可能aiohttp没有在正确的端口上侦听。您需要类似于
web.run\u应用程序(app,port=os.getenv('port'))
更新:等等,你试图用gunicorn和
web来提供它。运行应用程序
这是错误的,你要么只需要像web:python application.py
这样的东西,要么删除web.run\u app(app)
如果你在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
我试过两种情况,但都不起作用。对于上一条消息,同样的错误很抱歉:它适用于
web:python application.py
和web.run\u app(app,port=os.getenv('port',5000))
。需要手动创建一个新的应用程序,因为previos one由于某种原因没有看到Procfile中的更改gunicorn应用程序:app--bind localhost:8080--worker类aiohttp.GunicornWebWorker
不起作用。第二种情况不起作用,因为--bind localhost:8080
的端口错误。这是通过python application.py
为生产服务的好方法吗?简而言之,“是”。heroku会照顾1号。aiohttp 2前面的反向代理。如果应用程序崩溃或内存泄漏,请重新启动dyno,并每天重新启动一次。