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

n3h0vuf2  于 2024-01-05  发布在  Python
关注(0)|答案(6)|浏览(319)

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

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

个字符
错误类型:

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


推理脚本:

  1. model_name = "checkpoint-3380-epoch-20"
  2. model = MultiLabelClassificationModel("roberta","src/outputs/"+model_name)
  3. def inference(input_text,model_name="checkpoint-3380-epoch-20"):
  4. """Function to run inverence on one sample text"""
  5. #model = MultiLabelClassificationModel("roberta","src/outputs/"+model_name)
  6. all_tags =[]
  7. if isinstance(input_text,str):
  8. print("before")
  9. result ,output = model.predict([input_text])
  10. print(result)
  11. tags=[]
  12. for idx,each in enumerate(result[0]):
  13. if each==1:
  14. tags.append(classes[idx])
  15. all_tags.append(tags)
  16. elif isinstance(input_text,list):
  17. result ,output = model.predict(input_text)
  18. tags=[]
  19. for res in result :
  20. for idx,each in enumerate(res):
  21. if each==1:
  22. tags.append(classes[idx])
  23. all_tags.append(tags)
  24. return result,output,all_tags


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

yizd12fk

yizd12fk1#

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

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

字符串

4ktjp1zp

4ktjp1zp2#

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

  1. from multiprocessing import set_start_method
  2. from multiprocessing import Process, Manager
  3. try:
  4. set_start_method('spawn')
  5. except RuntimeError:
  6. pass
  7. @app.get("/article_classify")
  8. def classification(text:str):
  9. """function to classify article using a deep learning model.
  10. Returns:
  11. [type]: [description]
  12. """
  13. manager = Manager()
  14. return_result = manager.dict()
  15. # as the inference is failing
  16. p = Process(target = inference,args=(text,return_result,))
  17. p.start()
  18. p.join()
  19. # print(return_result)
  20. result = return_result['all_tags']
  21. 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块下,并显示输出,这样我们就可以研究真实的问题。

  1. import logging
  2. @app.get("/article_classify")
  3. def classification(text:str):
  4. """function to classify article using a deep learning model.
  5. Returns:
  6. [type]: [description]
  7. """
  8. try:
  9. _,_,result = inference(text)
  10. except:
  11. logging.exception("something bad happened") # automatically print exception info
  12. return result

字符串

展开查看全部

相关问题