我试图做一个后端服务器的基础上快速。
我的后端服务器运行良好,几乎没有错误,但我发现了错误情况。
我使用api.route分配两项服务给fastapi应用。
如果您查看下面的代码,您可以看到分配了detected_images和filtered_images服务。
这是我的密码:
import uvicorn
from fastapi import FastAPI
app = FastAPI()
from api.routes import detected_images
app.include_router(detected_images.router)
from api.routes import filtered_images
app.include_router(filtered_images.router)
if __name__ == '__main__':
uvicorn.run(app, port='8000', host="127.0.0.1")但如果我按原样运行这段代码,我将面临一个错误。
在我看来,估计GPU内存在运行的同时会被暂时超过。
这是我的错误信息:
RuntimeError: CUDA out of memory. Tried to allocate 2.00 MiB (GPU 0; 8.00 GiB total capacity; 6.23 GiB already allocated; 0 bytes free; 6.25 GiB reserved in total by PyTorch)
但是,如果没有在一个python代码中执行,并将其分为两个,并按顺序执行,则不会出现错误。
下面的代码是我试图避免错误的方法。
这是我的process1:
import uvicorn
from fastapi import FastAPI
app = FastAPI()
from api.routes import detected_images
app.include_router(detected_images.router)
if __name__ == '__main__':
uvicorn.run(app, port='8000', host="127.0.0.1")这是我的process2:
import uvicorn
from fastapi import FastAPI
app = FastAPI()
from api.routes import filtered_images
app.include_router(filtered_images.router)
if __name__ == '__main__':
uvicorn.run(app, port='8001', host="127.0.0.1")此外,我尝试了下面的方法,但它没有工作。
import uvicorn
from fastapi import FastAPI
app = FastAPI()
from api.routes import detected_images
app.include_router(detected_images.router)
import torch, gc
gc.collect()
torch.cuda.empty_cache()
from api.routes import filtered_images
app.include_router(filtered_images.router)
if __name__ == '__main__':
uvicorn.run(app, port='8000', host="127.0.0.1")有什么好办法解决这个问题吗?
发布于 2022-05-12 21:05:50
你可以试试:
if __name__ == "__main__":
uvicorn.run("agent:app", host="127.0.0.1", port=5000, reload=False, log_level="info", workers=0)--重新加载必须为False,并且设置--工作者=0。
https://stackoverflow.com/questions/71433373
复制相似问题