Код: Выделить всё
# Global model variable
ml_model = None
# Async function to download model from S3
async def download_model_from_s3(path="integration-tests/artifacts/MULTI.joblib"):
global ml_model
s3_client = boto3.client("s3")
bucket = os.environ.get("BUCKET_BUCKET", "artifacts_bucket")
try:
local_model_path = './model.joblib'
download_coroutine = s3_client.download_file(bucket, path, local_model_path)
await download_coroutine
ml_model = joblib.load(local_model_path)
http://logging.info(f"Model updated.")
except Exception as e:
logging.exception(f"Error downloading or loading model: {e}")
# Asynchronous scheduler function that updates the model every interval
async def scheduler(bucket_name: str, model_key: str, interval=60):
while True:
# Sleep for the specified interval (in minutes)
await asyncio.sleep(interval * 60)
# Call the download function to update the model
await download_model_from_s3(bucket_name, model_key)
app = FastAPI()
# Startup event to start the scheduler
@app.on_event("startup")
async def startup_event():
# BLOCKING: Download the model once at startup to ensure it is available
download_model_from_s3() # Blocking, ensures model is available
# Start the scheduler to update the model every 60 minutes (async, non-blocking)
await scheduler(bucket_name, model_key, interval=60)
Я предпочел использовать оператор while true вместо явного планирования задания один раз в час. Однако я не уверен в целесообразности этого метода.
Подробнее здесь: https://stackoverflow.com/questions/790 ... in-fastapi