Code that I got from below link, can persist data to the disk.
http://tohyongcheng.github.io/python/2016/06/07/persisting-a-cache-in-python-to-disk.html
I tried it but the file does not get generated.
import atexit
import pickle
# or import cPickle as pickle
def persist_cache_to_disk(filename):
def decorator(original_func):
try:
cache = pickle.load(open(filename, 'r'))
except (IOError, ValueError):
cache = {}
atexit.register(lambda: pickle.dump(cache, open(filename, "w")))
def new_func(*args):
if tuple(args) not in cache:
cache[tuple(args)] = original_func(*args)
return cache[args]
return new_func
return decorator
I tried to use this code as per the example...
@persist_cache_to_disk('users.p')
def get_all_users():
x = 'some user'
return x
Update:
This is working at python command prompt, but does not work in ipython notebook.
Aucun commentaire:
Enregistrer un commentaire