cloudpickle serializes Python objects that standard pickle cannot, including lambdas and interactively defined functions and classes. It targets shipping code to remote workers in cluster setups.
Overview
cloudpickle extends the standard library pickle module so more Python constructs can be serialized across processes or networks. It is aimed at distributed and cluster computing where worker nodes must execute code defined elsewhere.
It can pickle lambda functions and functions or classes created interactively in the main module, such as in scripts, shells, or Jupyter notebooks. Unlike pickle’s reference-based serialization, cloudpickle can serialize some constructs by value when modules are not importable on workers.
Since version 2.0.0, register_pickle_by_value and unregister_pickle_by_value let you mark modules that should always serialize by value during development across machines. Objects should only be loaded from trusted sources because unpickling can execute arbitrary code.
cloudpickle is not intended for long-term storage and requires the same Python version on both ends. It grew from picloud.com and work integrated into PySpark before becoming a standalone maintained project.
Key features
- Pickling lambdas and interactive definitions
- Serialization by value for select modules
- register_pickle_by_value API
- Cluster-friendly remote execution use
- PyPI installable package
Use cases
- Shipping functions to cluster workers
- Serializing notebook-defined callables
- Distributed Python job execution
- Spark-adjacent remote Python objects