--reload 标志不应在 Windows 上的生产中使用。 Uvicorn服务器警告

发布于 2025-01-12 09:20:44 字数 632 浏览 3 评论 0原文

我运行 uvicorn main:app --reload 来启动 FastAPI 服务器。服务器正在运行,但我在控制台上看到一条警告:

WARNING:  The --reload flag should not be used in production on Windows.

以下是我的代码:

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware


app = FastAPI()


origins = ["https://localhost:8080"]

app.add_middleware(
  CORSMiddleware,
  allow_origins=origins,
  allow_credentials=True,
  allow_methods=["*"],
  allow_headers=["*"])

@app.get("/")
def create_todo():
  return {"Ping":"Pong"}

我想知道为什么我会看到这个?该警告的原因是什么?

I run uvicorn main:app --reload to start a FastAPI server. the server is running, but I see a warning at the console that is:

WARNING:  The --reload flag should not be used in production on Windows.

Below is my code:

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware


app = FastAPI()


origins = ["https://localhost:8080"]

app.add_middleware(
  CORSMiddleware,
  allow_origins=origins,
  allow_credentials=True,
  allow_methods=["*"],
  allow_headers=["*"])

@app.get("/")
def create_todo():
  return {"Ping":"Pong"}

I would like to know that Why am I seeing that? What is the reason for that warning?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

别理我 2025-01-19 09:20:44

根据 FastAPI 文档

警告

如果您正在使用--reload选项,请记住删除它。

--reload选项消耗更多资源,更不稳定,
等等

它在开发过程中很有帮助,但你不应该
生产

因此,FastAPI 会显示该警告,提醒您不要生产中使用--reload 标志,因为服务器会消耗更多资源定期查找所有 *.py 文件的更改。

但是,当您在自己的计算机上开发测试应用程序时,使用--reload 标志是完全可以的。您还可以指定要监视 python 文件更改的目录,以及指定一个 glob 模式来匹配将包含或排除在监视范围之外的文件或目录 - 请参阅 Uvicorn 文档

As per FastAPI documentation:

Warning

Remember to remove the --reload option if you were using it.

The --reload option consumes much more resources, is more unstable,
etc.

It helps a lot during development, but you shouldn't use it in
production.

Thus, FastAPI displays that warning as a reminder for you not to use the --reload flag in production, as the server would consume much more resources to periodically look for changes to all *.py files.

It is, however, perfectly fine to use the --reload flag as you develop and test your application on your own machine. You can also specify which directories to watch for python file changes, as well as specify a glob pattern to match files or directories which will included or excluded from watching - see Uvicorn documentation.

你列表最软的妹 2025-01-19 09:20:44

我们通常谈论应用程序的使用方式有多个“阶段”(开发、测试、登台、生产等);在这种情况下,只有开发和生产是相关的。

开发是指您(开发人员)在自己的计算机上运行应用程序并积极开发应用程序。在这种情况下,使用 --reload 完全没问题 - 这就是它的用途!这也是它真正有用的用例,因为代码会随着您开发应用程序和编写代码而发生变化。

生产是指您的应用程序可供其他人使用的阶段,通常在辅助位置(服务器或其他服务上),其中代码不再主动更改(就在您进行更改并决定之后)是时候更新其他人看到和使用的应用程序版本了)。

当您将应用程序部署到生产环境时,代码在应用程序运行时不会主动更改 - 您在自己的计算机上进行开发,但在将其上传或部署到服务器之前,服务器上的代码不会更改。在该阶段更改代码是一个更加深思熟虑的决定,当发生这种情况时,您可以在部署新代码后手动重新启动应用程序。在这种情况下,使用 --reload 标志运行只会增加不必要的开销,因为服务器必须监视应用程序中的所有文件是否发生更改 - 这些更改永远不会发生。

如果文件数量较多,则在 Windows 上尤其如此,因此该消息明确提及 Windows。我也会跳过在其他平台上使用它,但性能影响并不那么大。

There are multiple "stages" that we usually talk about in how an application is used (development, testing, staging, production, etc.); in this case, only development and production is relevant.

development refers to you (the developer) running the application on your own computer and actively developing the application. In this situation using --reload is perfectly fine - it's the usage it is intended for! It's also the use case when it's actually useful, since the code changes as you develop your application and write code.

production refers to the stage where your application is made available to other people, usually in a secondary location - on a server or some other service - where the code doesn't actively change any longer (just after you've made your changes and decided that it's time to update the application version that other people see and use).

When you deploy your application to production the code doesn't actively change while the application is running - you develop on your own computer, but on the server code doesn't change before you upload or deploy it to the server. Changing the code at that stage is a more deliberate decision, and when that happens, you restart the application manually after you've deployed the new code. In that case running with the --reload flag just add unnecessary overhead, since the server has to watch all the files in the application for changes - changes that never happen.

This is particularly the case on Windows if the number of files are high, and therefor the message mentions Windows explicitly. I'd skip using it on other platforms as well, but the performance hit isn't as large there.

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