in python , How to load just one time a ML model in a rest service like django or flask?

2024/11/14 22:35:23

I have a ML model saved in a pkl (pickel file), I have no problem loading this model and using it for prediction, even I have a rest service that expose it, the only problem is that I load the model in every request, something like this:

https://www.analyticsvidhya.com/blog/2017/09/machine-learning-models-as-apis-using-flask/

I really want that my model just load one time like a global variable and every request useing this variable without the necessity of load the model every request

is it possible?

Answer

You can assign the model variable in settings.py. Whenever the server will start/restart django will store the model variable globally. It can be accessed like

from django.conf import settings 
print settings.my_ml_model_variable
https://en.xdnf.cn/q/71900.html

Related Q&A

Adding a Title or Text to a Folium Map

Im wondering if theres a way to add a title or text on a folium map in python?I have 8 maps to show and I want the user to know which map theyre looking at without having to click on a marker. I attem…

Zombie SharedDataMiddleware on Python Heroku

Im setting up a Flask app on Heroku. Everything is working fine until I added static files. Im using this:from werkzeug import SharedDataMiddleware app = Flask(__name__) app.wsgi_app = SharedDataMiddle…

Python: Urllib.urlopen nonnumeric port

for the following codetheurl = "https://%s:%[email protected]/nic/update?hostname=%s&myip=%s&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG" % (username, password, hostname, theip)conn…

How to unittest the sequence of function calls made inside a python fuction?

I would like to unittest a fuction and assert if the sequence of function calls made inside the function workflow(). Something like,[1st called] fetch_yeargroup_ls()[2nd called] invoke_get_links().....…

OpenCV: Extract SURF Features from user-defined keypoints

I want to compute SURF Features from keypoints that I specify. I am using the Python wrapper of OpenCV. The following is the code I am trying to use, but I cannot find a working example anywhere.surf…

Realtime processing and callbacks with Python and C++

I need to write code to do some realtime processing that is fairly computationally complex. I would like to create some Python classes to manage all my scripting, and leave the intensive parts of the a…

How can I troubleshoot a segmentation fault when working with Python Ctypes and C++?

Lets say I have the following two function signatures in C++:BYTE* init( BYTE* Options, BYTE* Buffer )and:int next( BYTE* interface, BYTE* Buffer )The idea is that I first initialize an Interface class…

Undefined variable from import when using protocol buffers in PyDev

Ive got a PyDev project that uses protocol buffers. The protocol buffer files are located in a zip file generated by the protoc compiler. Everything works when I run the program, however PyDev reports …

Animating a network graph to show the progress of an algorithm

I would like to animate a network graph to show the progress of an algorithm. I am using NetworkX for graph creation. From this SO answer, I came up with a solution using clear_ouput from IPython.displ…

How to run grpc on ipv4 only

Im going to run a grpc server on IPv4 address like this: server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) protoc_pb2_grpc.add_ProtocServicer_to_server(StockProtocServicer(), server) ser…