开始使用免费开始使用

记录模型加载耗时

您正在构建一个用于分类企鹅的 FastAPI 应用,想要测量加载模型所需的时间并将其记录到日志中。

本练习是课程的一部分

使用 FastAPI 在生产环境中部署 AI

查看课程

练习说明

  • 加载 uvicorn 的错误记录器。
  • 以 INFO 级别记录模型加载的处理耗时。

交互式实操练习

通过完成这段示例代码来试试这个练习。

from fastapi import FastAPI
import logging
import joblib 
import time

# Get the uvicorn error logger
logger = logging.getLogger('____')

start_time = time.perf_counter()
model = joblib.load('penguin_classifier.pkl')
process_time = time.perf_counter() - start_time
# Log the process time at the INFO level
logger.____(f"Process time was {____} seconds.")

app = FastAPI()
编辑并运行代码