如何在用户属性更新时触发事件?

How can I fire an event when a user attribute updates?

我有一个用于游戏状态更新的 FastAPI 端点。每个用户都有一些猜测计数,如果它变成 0,那么在补充猜测之前会有 x 秒的冷却计时器。

那么服务器端应该如何支持呢?当用户猜测计数变为 0 时,服务器是否应该触发一个事件,它可以休眠冷却时间,直到补充猜测。如果是,那该怎么做? FastAPI 在这方面提供了什么吗?

是的,你可以做到,没有代码很难提供准确的答案,但你可以通过 Background Tasks

所以它可能看起来像这样:

from fastapi import BackgroundTasks

async def refresh_user_stats(user: User = Depends(get_current_user)):
    do something here


@app.update()
async def refresh_user(background_tasks: BackgroundTasks):
    background_tasks.add_task(refresh_user_stats())