gpt4 book ai didi

python-3.x - Uvicorn 服务器意外关闭

转载 作者:行者123 更新时间:2023-12-01 03:06:16 35 4
gpt4 key购买 nike

我正在使用由 Uvicorn 服务器提供服务的 FastAPI 框架。
我的应用程序应该在给定的端点 (/run) 上运行一些耗时的数值计算。为此,我使用来自 fastAPI 的“background_task”(基本上是来自 Starlette 的“background_task”)。

运行应用程序时,经过一段时间的正常行为后,服务器由于某种原因关闭。

应用程序的日志如下所示:

INFO: Started server process [922]
INFO: Waiting for application startup.
DEBUG: None - ASGI [1] Started
DEBUG: None - ASGI [1] Sent {'type': 'lifespan.startup'}
DEBUG: None - ASGI [1] Received {'type': 'lifespan.startup.complete'}
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
DEBUG: ('10.0.2.111', 57396) - Connected
DEBUG: ('10.0.2.111', 57397) - Connected
DEBUG: ('10.0.2.111', 57396) - ASGI [2] Started
DEBUG: ('10.0.2.111', 57396) - ASGI [2] Received {'type': 'http.response.start', 'status': 200, 'headers': '<...>'}
INFO: ('10.0.2.111', 57396) - "GET /run HTTP/1.1" 200
DEBUG: ('10.0.2.111', 57396) - ASGI [2] Received {'type': 'http.response.body', 'body': '<32 bytes>'}
DEBUG: ('10.0.2.111', 57396) - ASGI [3] Started
DEBUG: ('10.0.2.111', 57396) - ASGI [3] Received {'type': 'http.response.start', 'status': 404, 'headers': '<...>'}
INFO: ('10.0.2.111', 57396) - "GET /favicon.ico HTTP/1.1" 404
DEBUG: ('10.0.2.111', 57396) - ASGI [3] Received {'type': 'http.response.body', 'body': '<22 bytes>'}
DEBUG: ('10.0.2.111', 57396) - ASGI [3] Completed

...

DEBUG: ('10.0.2.111', 57396) - Disconnected
... The background task is completed.
DEBUG: ('10.0.2.111', 57396) - ASGI [2] Completed
DEBUG: ('10.0.2.111', 57397) - Disconnected
DEBUG: ('10.0.2.111', 57405) - Connected

...
The application goes on, with requests and completed background tasks.
At some point, during the execution of a background task:

INFO: Shutting down
DEBUG: ('10.0.2.111', 57568) - Disconnected
DEBUG: ('10.0.2.111', 57567) - Disconnected
INFO: Waiting for background tasks to complete. (CTRL+C to force quit)
DEBUG: ('10.0.2.111', 57567) - ASGI [6] Completed
INFO: Waiting for application shutdown.
DEBUG: None - ASGI [1] Sent {'type': 'lifespan.shutdown'}
DEBUG: None - ASGI [1] Received {'type': 'lifespan.shutdown.complete'}
DEBUG: None - ASGI [1] Completed
INFO: Finished server process [922]

我真的不明白为什么会发生这种情况。我不知道要尝试什么来修复它。

我的代码看起来像这样。

#!/usr/bin/env python3.7
import time
from fastapi import FastAPI, BackgroundTasks
import uvicorn
from starlette.responses import JSONResponse
import my_imports_from_project

analysis_api = FastAPI()

@analysis_api.get("/")
def root():
return {"message": "root"}


@analysis_api.get("/test")
def test():
return {"message": "test"}

@analysis_api.get("/run")
def run(name: str, background_task: BackgroundTasks):
try:
some_checks(name)
except RaisedExceptions:
body = {"running": False,
"name": name,
"cause": "Not found in database"}
return JSONResponse(status_code=400, content=body)
body = {"running": True,
"name": name}
background_task.add_task(run_analysis, name)
return JSONResponse(status_code=200, content=body)


if __name__ == "__main__":
uvicorn.run("api:analysis_api", host="0.0.0.0", log_level="debug")

最佳答案

这就是我解决整个问题的方法。

我认为问题在于我的任务产生了一些进程以执行计算。
所以,而不是使用 FastApi background_task , 我现在用的是 multiprocessing.Process() .
这就解决了。

正如 FastApi 的人所指出的那样,如果项目变得庞大而复杂,这个解决方案可能无法很好地扩展。在这种情况下,强烈建议使用类似消息队列 + 正在运行的任务(如 FastApi site 上的建议)。

但是,对于小型项目,使用 multiprocessing.Process 的解决方案或 subprocess.Popen完全没问题。

关于python-3.x - Uvicorn 服务器意外关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57036856/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com