Lukas Scholz
Lukas Scholz

Reputation: 772

FastAPI - Repeat PUT-Endpoint every X seconds

I want to execute a PUT-Endpoint every 15 seconds. I already tried to use repeated_task from fastapi_utils.tasks, but when I implemented it this way:

@app.put('/fuellstand', response_model=Fuellstand)
@app.on_event("startup")
@repeat_every(seconds=5)
def return_fuellstand():
    liste = get_fuellstand(lanes)

    return Fuellstand(
        __root__=liste
    )

I just got Null, when executing the endpoint via the openapi-docs. I also tried to print out something to see, if the repetition works, but I also didn't got any message in the console.

Is this the right way to implement repeated_task?

Upvotes: 0

Views: 3048

Answers (1)

Lukas Scholz
Lukas Scholz

Reputation: 772

I now created a additional function that gets executed repeatedly:

@app.put('/loadFactor', response_model=Load_Factor)
def put_load_factor():
    load_factor = get_load_factor(lanes)

    return Load_Factor(
        __root__ = load_factor
    )

@app.on_event("startup")
@repeat_every(seconds=15)
def return_load_factor():
    put_load_factor() 

Upvotes: 2

Related Questions