Issue
I have deployed a model on web but for each call from user application creates a new instance an load it into memory, which hits memory limit when a few users are using application at the same time. A solution I found is that model has to be initialized at application level rather than function level and for that I'll have to create global variable. There is another option Application Context. The question is which one is suitable for my problem here. I am initializing model in different file and then importing it in app.py
file where there are flask functions to interact with user. If I have to use global variable, do I need to initialize model with global variable or use it where I am import that model?
Solution
You are on the right track and the methods you listed are valid - depending on usecase one might be better than the other. As a concept you need to use the Singleton pattern.
Singleton Pattern will ensure that at any given point only one instance of the class/function exits hence keeping your memory consumption in check. For doing so you will need to ensure that the function is stateless, i.e. given a set of input parameters, it should give the output without altering the output for a different request.
There are many threads on singleton in Flask, I won't repeat it here. Here's a starting point How to create a singleton object in Flask micro framework
Answered By - shoaib30
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.