Below is the code I use for the generator. It is modified from Saving and loading multiple objects in pickle file?. The file was created with joblib.dump, and consists (so far) of a single machine learning model.
import pickle
from sklearn.externals import joblib
filename = 'ML_models.pkl'
def load(filename):
with open(filename, "rb") as f:
while True:
try:
yield joblib.load(f)
except EOFError:
break
Next I test the generator as follows:
models = load(filename)
for model in models:
print model
Unfortunately, this prints the model repeatedly until I interrupt the kernel. What is the right way to signal EOF?