当我通过脚本执行 Uvicorn 时,它没有运行

发布于 2025-01-10 01:23:34 字数 326 浏览 3 评论 0原文

如果我从终端 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自找没趣 2025-01-17 01:23:34

您可以改为添加 main 方法(更具体地说,if __name__ == "__main__":) 在现有的 Python 脚本(即,在您的情况下为 main.py)或新的 Python 脚本中,您可以在其中 运行 uvicorn服务器,允许设置参数,如hostportreloadworkers等(如图< a href="https://www.uvicorn.org/#running-programmatically" rel="nofollow noreferrer">此处),然后只需从 bash 脚本内部执行该 Python 脚本即可。请查看此处了解所有可用的选项

import uvicorn

if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)

编辑:自从您提到下面“在 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 you run the uvicorn server, allowing you to set parameters, such as host, 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 available options.

import uvicorn

if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)

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.

故人的歌 2025-01-17 01:23:34

您可以从 bash 脚本中删除 exes。也许这是你失败的一个原因。

you can remove the exes from bash script. might be it is a cause of your failure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文