Pickling — Python documentation

Note

Click here to download the full example code

# Pickling

This example pickles a function.

Out:

    [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
    [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

    from math import sqrt
    import pickle

    from joblib import Parallel, delayed

    assert __name__ == '__main__'
    assert '__file__' not in globals()

    def function(x):
        return sqrt(x)

    pickle.loads(pickle.dumps(function))

    # Now with joblib
    print(Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10)))
    print(Parallel(n_jobs=2)(delayed(function)(i ** 2) for i in range(10)))

Total running time of the script: ( 0 minutes 0.313 seconds)

Gallery generated by Sphinx-Gallery