Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

375
Visualizações
How to share variables between HTTP requests in FastAPI?

I wonder how can I share the value of variables between HTTP requests in FastAPI. For instance, I have a POST request in which I get some audio files and then I convert their info into a Pandas Dataframe. I would like to send that Dataframe in a GET request, but I can't access the Dataframe on the GET request scope.

@app.post(
    path="/upload-audios/",
    status_code=status.HTTP_200_OK
)
async def upload_audios(audios: list[UploadFile] = File(...)):
    filenames = [audio.filename for audio in audios]
    audio_data = [audio.file for audio in audios]
    new_data = []
    final_data = []
    header = ["name", "file"]
    for i in range(len(audios)):
        new_data = [filenames[i], audio_data[i]]
        final_data.append(new_data)
    new_df = pd.DataFrame(final_data, columns=header)
    return f"You have uploaded {len(audios)} audios which names are: {filenames}"

@app.get("/get-dataframe/")
async def get_dataframe():
    pass
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If you need read-only access to that variable, and/or you never expect it to be changed by some other request before reading it (in other words, you never expect to serve more than one client), as well as your app does not use several workers at the same time (where each worker has its own memory), you could either (as mentioned by @MatsLindh in the comments) declare a dictionary foo = {} outside the endpoints and assign a key to it inside the endpoint foo['pd'] = new_df (which you can later retrieve), or declare your variable as global (as described here), or, preferably, store it on the app instance. For example:

app.state.new_df = new_df 

Inside get-dataframe endpoint retrieve the new_df as:

new_df = app.state.new_df

or, if the app instance is not available in the file from which you are working (let's say you have your endpoints defined in submodules, separately from the main module, as described here), you could get the app instance from the Request object:

from fastAPI import Request
@app.get("/get-dataframe/")
async def get_dataframe(request: Request):
    return request.app.state.new_df

Otherwise, if you need that variable/object to be shared among different clients, as well as among multiple processes/workers, that may also require read/write access to it, you should rather use a database storage, such as PostgreSQL, SQLite, MongoDB, etc., or Key-Value stores (Caches), such as Redis, Memcached, etc. You may want to have a look at this answer as well.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda