I'm kind of new to using flask, and I want to cache the result of a function that reads pickled data. I use memoize
function from flask_cache
as follows:
in model_chacher.py
:
from flask_cache import Cache
import pickle
model_cache = Cache(config={'CACHE_TYPE': 'simple'})
class ModelCacher():
@model_cache.memoize(50)
def get_model(self, customer_ID):
with open('/path/to/data.pickle', 'rb') as tf:
model_args = pickle.load(tf)
trained_classifier = model_args[0]
return trained_classifier
in flask_compose.py
:
from flask import Flask
from controllers.topic import controller as topic_controller
from models.modelcache.model_chacher import model_cache
app = Flask(__name__)
model_cache.init_app(app)
app.register_blueprint(topic_controller.topic_controller_blueprint)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=80, debug=True)
and I call ModelCacher.get_model(customer_ID)
in topic_controller
:
from models.modelcache.model_chacher import ModelCacher
...
trained_classifier = ModelCacher.get_model(cls_str)
...
and after running flask_compose.py
and sending a request I get the following result:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/app/controllers/topic/controller.py", line 20, in classify
return ModelCacher.get_model(cls_str)
File "/usr/local/lib/python3.6/site-packages/flask_cache/__init__.py", line 528, in decorated_function
cache_key = decorated_function.make_cache_key(f, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/flask_cache/__init__.py", line 393, in make_cache_key
**kwargs)
File "/usr/local/lib/python3.6/site-packages/flask_cache/__init__.py", line 434, in _memoize_kwargs_to_args
elif abs(i-args_len) <= len(argspec.defaults):
TypeError: object of type 'NoneType' has no len()
and my question is: how to properly set up my cache? any help is greatly appreciated.
EDIT: WHAT SOLVED MY PROBLEM:
as @stamaimer pointed out, I create and instance of my ModelCacher
and that solved the problem, also I used cached from flask_cache.Cache
instead of memoize
.
The get_model
method your defined is a instance method instead of a class method. You use ModelCacher.get_model(cls_str)
when call the method. Try to call it with ModelCacher().get_model(cls_str)
. Or you can just defined it as a global function.