fastapi-tips icon indicating copy to clipboard operation
fastapi-tips copied to clipboard

Lifespan question

Open cardosofede opened this issue 1 year ago • 7 comments

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?

cardosofede avatar Apr 28 '24 19:04 cardosofede

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

Praveenstein avatar Apr 29 '24 03:04 Praveenstein

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

Kludex avatar Apr 29 '24 09:04 Kludex

I'll reopen if you reply.

Kludex avatar Apr 30 '24 12:04 Kludex

Sorry I just saw the message, will create one tomorrow

cardosofede avatar May 02 '24 00:05 cardosofede

@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

cardosofede avatar May 10 '24 20:05 cardosofede