using decorators to persist python objects
<p>Code that I got from below link, can persist data to the disk. </p>

<p><a href="http://tohyongcheng.github.io/python/2016/06/07/persisting-a-cache-in-python-to-disk.html">http://tohyongcheng.github.io/python/2016/06/07/persisting-a-cache-in-python-to-disk.html</a></p>

<p>I tried it but the file does not get generated.</p>

<pre><code>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
</code></pre>

<p>I tried to use this code as per the example...</p>

<pre><code>@persist_cache_to_disk('users.p')
def get_all_users():
    x = 'some user'
    return x
</code></pre>

<p>Update:</p>

<p>This is working at python command prompt, but does not work in ipython notebook.</p>