Home > OS >  Bad: FastApi response problems
Bad: FastApi response problems

Time:03-04

i create api using fastapi framework and i have deployed on heroku, the main function of this api is i have to send (post method) element as query parameter every 10s and i create path to show all elements sent but i facing problem when i refresh page i didn't get all element sent for example if i send [1, 2, 3, 4, 5, 6, 7, 8, 9] sometimes i get [1, 3, 4, 5, 7, 9] and sometimes i get [2, 6, 8], but in local i have api run perfectly.

from fastapi import FastAPI

app = FastAPI()

hr = []


@app.get("/")
async def api_status():
    return {"message": "api running"}


@app.get("/hr")
async def show_hr_values():
    if hr:
        return hr
    return {"message": "hr empty"}


@app.post("/hr")
async def add_hr_value(hr_value: int):
    hr.append(hr_value)
    return {"message": f"hr = {hr_value} added"}

CodePudding user response:

Heroku will not run your application as just one process. Since you're keeping this data in memory, it will only be updated and kept in that process that receives your request.

Use a database or KV store to store your data instead, so that it's stored in a centralized location that all processes can talk to. Heroku supports managed postgres and redis that you can use for either of these use cases.

  • Related