当我通过脚本执行 Uvicorn 时,它没有运行
如果我从终端 uvicorn main:app --reload
运行,一切正常。
当将其放入 my_script.sh
文件中时:
#!/bin/bash
exec uvicorn main:app --reload
那么什么也不会发生。为什么?
我发现了这个:Uvicorn/FastAPI可执行文件,但似乎没有答案。
If I run from the terminal uvicorn main:app --reload
everything works.
When putting it in a my_script.sh
file with:
#!/bin/bash
exec uvicorn main:app --reload
then nothing happens. Why?
I found this: Uvicorn/FastAPI executable, but it seems unanswered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以改为添加
main
方法(更具体地说,if __name__ == "__main__":
) 在现有的 Python 脚本(即,在您的情况下为main.py
)或新的 Python 脚本中,您可以在其中运行
uvicorn服务器,允许设置参数,如host
、port
、reload
、workers
等(如图< a href="https://www.uvicorn.org/#running-programmatically" rel="nofollow noreferrer">此处),然后只需从 bash 脚本内部执行该 Python 脚本即可。请查看此处了解所有可用的选项
。编辑:自从您提到下面您“在 docker-compose 的入口点部分定义了脚本”,请确保您给出了准确的bash 脚本的路径,以及它具有执行权限。查看此处的问题 和此处。我还建议您查看相关 FastAPI 文档。
You can instead add a
main
method (more specifically,if __name__ == "__main__":
) inside your existing Python script (i.e.,main.py
in your case) or a new Python script, where yourun
the uvicorn server, allowing you to set parameters, such ashost
,port
,reload
,workers
, etc. (as shown here), and simply execute that Python script from inside your bash script. Have a look here for all the availableoptions
.Edit: Since you mentioned below that you "have the script defined in the entrypoint section of docker-compose", make sure you give the exact path to the bash script, as well as it has permissions to execute. Have a look at the questions here and here. I would also suggest to take a look at the relevant FastAPI documentation.
您可以从 bash 脚本中删除
exes
。也许这是你失败的一个原因。you can remove the
exes
from bash script. might be it is a cause of your failure.