Python FastAPI初始化中调用的数量让我感到困惑

Python FastAPI初始化中调用的数量让我感到困惑,python,python-3.x,fastapi,uvicorn,Python,Python 3.x,Fastapi,Uvicorn,例如,让我们以简单的FastAPI应用程序为例: import uvicorn from fastapi import FastAPI print("calling main!") app = FastAPI() @app.get('/test', status_code=200) async def test(): return True if __name__ == "__main__": uvicorn.run("m

例如,让我们以简单的FastAPI应用程序为例:

import uvicorn
from fastapi import FastAPI

print("calling main!")
app = FastAPI()

@app.get('/test', status_code=200)
async def test():
    return True


if __name__ == "__main__":
    uvicorn.run("main:app", use_colors=True, workers=2)

如果我们运行它,我们会得到以下输出:

calling main!
INFO:     Uvicorn running on http://0.0.0.0:8020 (Press CTRL+C to quit)
INFO:     Started parent process [143621]
calling main!
calling main!
INFO:     Started server process [143832]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Started server process [143829]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
calling main!
calling main!
因此,对于具有两个工作进程(父进程+2个服务器进程)的应用程序,我们有5(!)个主进程执行。为什么? 也许我遗漏了一些简单的东西,但这让我困惑。 提前感谢您的回答

基于对多媒体处理的回答

Uvicorn启动新流程以创建工人。它将导入父级,这将为每个工作进程提供1个“调用main!”。然后,工作进程将启动新服务器,这将为每个服务器多提供1个“调用主服务器”。工人

您可以尝试
print('worker pid:{},模块名称:{})。使用格式(os.getpid(),\uu name\uuu))
而不是打印来检查进程。

基于有关多进程的回答

Uvicorn启动新流程以创建工人。它将导入父级,这将为每个工作进程提供1个“调用main!”。然后,工作进程将启动新服务器,这将为每个服务器多提供1个“调用主服务器”。工人


您可以尝试
print('worker-pid:{},模块名:{})。格式(os.getpid(),\uu-name\uu))
而不是打印以检查进程。

谢谢!我怀疑这一行为,但找不到相关信息。非常感谢!我怀疑这一行为,但找不到相关信息。非常感谢