0

I have a flask_restful api. I have divided the structure in few parts like:

    app:
     -api.py
     -resources
       --resource1.py
       --resource2.py

resource1 file have structure like:

class Func(Resource):
    def __init__(self):
        model = some_module.from_pretrained("xyz")
        model.eval()
        model.to(self.device)
        self.model = model
    def post(self):
        #taking some input to be eavaluated
        output = fun1(self.model, input//) //fun1 is already defined function 
        return processed output

api file is like this:

#importing requirements
app = Flask(__name__)
api = Api(app)
cache.init_app(app)
api.add_resource(Func, '/output')
if __name__ == "__main__":
    app.run(port='5000', debug = True)

I want to use cache for the model. I followed several articles but I'm not able to implement it correctly, so if anyone can give a solution, it will be helpful.

fluorine
  • 31
  • 7
  • The most easy way to store an object process-wide is at a place where the code is execute only once. In the case of Flask that is every module or class namespace or the `app` object. But please remember that in a production environment with multiple workers you will have one cached object per process. – Klaus D. Apr 13 '21 at 09:07
  • @KlausD. yeah right. Can I use caching for model in __init__ function above. – fluorine Apr 13 '21 at 19:51

0 Answers0