如何在Heroku中运行UVICORN? [英] How to run UVICORN in Heroku?
问题描述
因此,我已经弄清楚了如何编写fastAPI的代码,并准备将我的脚本部署到与fastAPI一起使用过的heroku中(
So I have figured out how to code a fastAPI and I am ready to deploy my script to heroku that I have worked with fastAPI (https://fastapi.tiangolo.com/) however the problem is that when I do a request to heroku it will just return:
<html>
<head>
<title>Internal Server Error</title>
</head>
<body>
<h1><p>Internal Server Error</p></h1>
</body>
</html>
这意味着脚本已打开,但我看不到错误,我会说它在本地完全正常.
Which means the script is on but I can't see the error and locally it works totally fine I would say.
我看不到问题所在的任何日志,但是我会说我的问题可能是我不确定我的 procfile 是否正确,因为我根本没有编辑过该文件,我对此很陌生,我在这里问我如何在heroku中运行我的fastapi脚本?
I am not able to see any logs where the problem is however I would say my problem might be that I am not sure if my procfile is correct because I haven't edited it at all and I am quite new at this and I am here to ask how I am able to run my fastapi script in heroku?
我所知道的是,要能够运行脚本,您必须使用命令 uvicorn main:app --reload
,如果您执行其他操作则将不起作用 py main.py
我在做什么错了?
What I know is that to be able to run the script, you have to use command uvicorn main:app --reload
and it won't work if you do etc py main.py
What am I doing wrong?
推荐答案
我已经测试了您的设置,经过一番检查(之前从未使用过Heroku),我猜您的uvicorn永远不会绑定到指定的端口(是heroku-cli命令 heroku local
为您工作?)
I've tested your setup and after some checking (never used Heroku before) I'm guessing your uvicorn never binds to the appointed port (was the heroku-cli command heroku local
working for you?)
您的Procfile可能看起来像这样;
Your Procfile could look like this;
web: uvicorn src.main:app --host=0.0.0.0 --port=${PORT:-5000}
此示例假定您将源代码放在名为"src"的子文件夹中,该子文件夹中有一个空的 __ init __.py
(指示Python模块,您可能想将src添加到PYTHONPATH中,请参见app.json)和包含您的fastapi应用的 main.py
;
This example assumes you have your source code within a subfolder named 'src' which has an empty __init__.py
(indicating a Python module, you probably want to add src to the PYTHONPATH instead, see app.json) and main.py
containing your fastapi app;
import socket
import sys
from fastapi import FastAPI
app = FastAPI()
hostname = socket.gethostname()
version = f"{sys.version_info.major}.{sys.version_info.minor}"
@app.get("/")
async def read_root():
return {
"name": "my-app",
"host": hostname,
"version": f"Hello world! From FastAPI running on Uvicorn. Using Python {version}"
}
我将我的工作示例添加到 github 上,您可以在
I have added my working example to github which you can view on heroku (for now)
这篇关于如何在Heroku中运行UVICORN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!