9

I want to get the app instance in my router file, what should I do ?

My main.py is as follows:

# ...
app = FastAPI()
app.machine_learning_model = joblib.load(some_path)
app.include_router(some_router)
# ...

Now I want to use app.machine_learning_model in some_router's file , what should I do ?

Chris
  • 18,724
  • 6
  • 46
  • 80
DachuanZhao
  • 1,181
  • 3
  • 15
  • 34

1 Answers1

18

Since FastAPI is actually Starlette underneath, you could store the model on the app instance using the generic app.state attribute, as described in Starlette's documentation (see State class implementation too). Example:

app.state.ml_model = joblib.load(some_path)

As for accessing the app instance (and subsequently, the model) from outside the main file, you can use the Request object. As per Starlette's documentation, where a request is available (i.e., endpoints and middleware), the app is available on request.app. Example:

from fastapi import Request

@router.get('/')
def some_router_function(request: Request):
    model = request.app.state.ml_model
Chris
  • 18,724
  • 6
  • 46
  • 80
  • May I add other request params in ```some_router_function``` ? – DachuanZhao Mar 01 '22 at 12:10
  • Yes. You can define params as usual. – Chris Mar 01 '22 at 12:20
  • Thank you for your answer! I'm quite shocked that FastAPI does not provide any streamlined possibility to store Singletons for the service lifecycle. – tafaust Apr 05 '22 at 11:13
  • Does this load model into memory only once and then pass the reference? I assume there is, since there is only one instance of the app running in the process? – ruslaniv Jul 27 '23 at 11:10
  • 1
    @ruslaniv It does, and it would be best to initialise that variable inside a startup/lifespan event hanlder, as described [here](https://stackoverflow.com/a/76322910/17865804). If you plan on having multiple workers active at the same time, you might want to have a look at [this](https://stackoverflow.com/a/71613757/17865804) and [this](https://stackoverflow.com/a/71537393/17865804) as well. – Chris Jul 27 '23 at 12:19