python 为什么我的fastapi或uvicorn被关闭?

n3h0vuf2  于 5个月前  发布在  Python
关注(0)|答案(6)|浏览(108)

我试图运行一个服务,使用简单的变压器罗伯塔模型做分类。推理脚本/功能本身的工作,如预期的测试时。当我包括与快速API其关闭服务器。

uvicorn==0.11.8
fastapi==0.61.1
simpletransformers==0.51.6
cmd : uvicorn --host 0.0.0.0 --port 5000 src.main:app

个字符
错误类型:

INFO:     Started server process [8262]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:5000 (Press CTRL+C to quit)
INFO:     127.0.0.1:36454 - "GET / HTTP/1.1" 200 OK
INFO:     127.0.0.1:36454 - "GET /favicon.ico HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:36454 - "GET /docs HTTP/1.1" 200 OK
INFO:     127.0.0.1:36454 - "GET /openapi.json HTTP/1.1" 200 OK
before
100%|████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 17.85it/s]
INFO:     Shutting down
INFO:     Finished server process [8262]


推理脚本:

model_name = "checkpoint-3380-epoch-20"
model = MultiLabelClassificationModel("roberta","src/outputs/"+model_name)
def inference(input_text,model_name="checkpoint-3380-epoch-20"):
    """Function to run inverence on one sample text"""
    #model = MultiLabelClassificationModel("roberta","src/outputs/"+model_name)
    all_tags =[]
    if isinstance(input_text,str):
        print("before")
        result ,output = model.predict([input_text])
        print(result)
        tags=[]
        for idx,each in enumerate(result[0]):
            if each==1:
                tags.append(classes[idx])
        all_tags.append(tags)
    elif isinstance(input_text,list):
        result ,output = model.predict(input_text)
        tags=[]
        for res in result : 
            for idx,each in enumerate(res):
                if each==1:
                    tags.append(classes[idx])
            all_tags.append(tags)

    return result,output,all_tags


更新:尝试与 flask 和服务是工作,但当添加uvicorn的 flask 顶部它陷入了重新启动循环。

yizd12fk

yizd12fk1#

虽然公认的解决方案可以工作,但我想建议使用uvicorn worker的解决方案。
您可能想尝试将--workers 4添加到CMD,以便它读取:

uvicorn --host 0.0.0.0 --port 5000 --workers 4 src.main:app

字符串

4ktjp1zp

4ktjp1zp2#

我已经通过显式地使用多处理来启动进程池解决了这个问题。

from multiprocessing import set_start_method
from multiprocessing import Process, Manager
try:
    set_start_method('spawn')
except RuntimeError:
    pass
@app.get("/article_classify")
def classification(text:str):
    """function to classify article using a deep learning model.
    Returns:
        [type]: [description]
    """
    manager = Manager()

    return_result = manager.dict()
    # as the inference is failing 
    p = Process(target = inference,args=(text,return_result,))
    p.start()
    p.join()
    # print(return_result)
    result = return_result['all_tags']
    return result

字符串

9rnv2umw

9rnv2umw3#

我最近遇到了类似的问题。我的情况可能有点不同,但想提供它作为参考。我使用的是需要下载大重量文件的转换器,下载过程需要O(10)秒。然而,默认的独角兽有一个设置timeout_notify=30。通过阅读源代码,这似乎是导致服务器不断重启的原因,因为下载需要很长时间(接近30秒)。
后来,我用了一种不同的方法来加快下载,然后重新启动的问题消失了。

zsbz8rwp

zsbz8rwp4#

根据https://github.com/ThilinaRajapakse/simpletransformers/issues/761,它与多处理有关。
我设置了args=“use_multiprocessing”:False},Web服务器不再关闭。

laik7k3q

laik7k3q5#

我最近遇到了这个问题,并以不同的方式解决了它。也许这对某人有用。
对我来说,问题是我运行FastAPI应用程序的虚拟机只是磁盘空间不足。我的应用程序处理相当大的视频文件,这导致它在试图将UploadFile写入硬盘时崩溃。

im9ewurl

im9ewurl6#

把整个函数放在一个try-except块下,并显示输出,这样我们就可以研究真实的问题。

import logging

@app.get("/article_classify")
def classification(text:str):
    """function to classify article using a deep learning model.
    Returns:
        [type]: [description]
    """
    try:
    _,_,result = inference(text)
    except:
        logging.exception("something bad happened")  # automatically print exception info

    return result

字符串

相关问题