The ipycache package defines a %%cache
cell magic allowing you to cache results of long-lasting computations in a pickle file.
import numpy as np
%load_ext ipycache
def long_function(a, b):
import time
time.sleep(1)
return a + b
The following cell will only be executed if the pickle file does not exist (also, there is a --force
option to always execute the cell and overwrite the results in the pickle file).
%%cache myvars.pkl a b c
a = 3
b = np.random.randn(3)
c = long_function(a, b)
print(a, b, c)
del a, b, c
Now, we execute the same cell again (in practice, the cell only appears once in the notebook, so this simulates multiple executions of the same cell).
%%cache myvars.pkl a b c
a = 3
b = np.random.randn(3)
c = long_function(a, b)
The cell has been skipped, but the variables have been recovered from the pickle file.
print(a, b, c)
Now, let's delete the file.
import os
os.remove('myvars.pkl')
Let's execute the cell once more.
%%cache myvars.pkl a b c
a = 3
b = np.random.randn(3)
c = long_function(a, b)
print(a, b, c)
os.remove('myvars.pkl')