Kludex / fastapi-tips

FastAPI Tips by The FastAPI Expert!
1.96k stars 74 forks source link

Lifespan question #2

Open cardosofede opened 5 months ago

cardosofede commented 5 months ago

Hey, First of all thanks for this initiative, it's great! I have a question regarding the lifespan to manage objects. Let's say that I would like to provide realtime data, and I have an object that subscribes via ws and keeps certain data updated, so I can expose a route to provide this data aggregated. Should I create the object with the lifespan approach? Or which is your recommendation?

Praveenstein commented 5 months ago

Great question, I'm curious for the reply by experts, been working on something similar.

Kludex commented 5 months ago

Can you give me an MRE? (GitHub repository with minimal setup would be great.)

Kludex commented 5 months ago

I'll reopen if you reply.

cardosofede commented 5 months ago

Sorry I just saw the message, will create one tomorrow

cardosofede commented 4 months ago

@Kludex hey, I have an example here: https://github.com/hummingbot/backend-api/blob/feat/add_market_data/routers/manage_market_data.py

I'm thinking about creating an endpoint to start a candles and stop the candles from a collection and if you ask for the realtime candles instead of creating and initializing the object will be just using the active candles.

As a reference, this is the class that we use in Hummingbot to keep the most updated length required of candles that are running: https://github.com/hummingbot/hummingbot/blob/master/hummingbot/data_feed/market_data_provider.py