pyrseus.core.pickle.CustomPickledClosure¶
- class pyrseus.core.pickle.CustomPickledClosure(func: Callable, args: Tuple, kwargs: Dict, dumps: Callable[[...], bytes], loads: Callable[[bytes], Any])[source]¶
- __init__(func: Callable, args: Tuple, kwargs: Dict, dumps: Callable[[...], bytes], loads: Callable[[bytes], Any])[source]¶
Wraps a function and its arguments into a nullary closure that forces a chosen pickler to be used for the closure and its return value.
The closed function will remain wrapped by this class when it undergoes round trip pickling.
The return value of the closed function will be wrapped in a
OncePickledObjectthat uses the samedumpsandloadsfunctions as the wrapper.
This is useful for wrapping functions that will be submitted to executors like
ProcessPoolExecutorwith workers in other processes. This forces the chosen pickler to be used for serialization, instead of whatever default pickler the executor normally uses.- Parameters:
func – the function whose pickling behavior we’re overriding
args – the positional arguments to
functhat we’re closing overkwargs – the keyword arguments to
functhat we’re closing overdumps – a
pickle.dumps-like function that will be used for serializing the closure contents when this wrapper is pickled. This function must accept aprotocolkeyword argument.loads – a
pickle.loads-like function that will be used for deserializing the closure contents when the pickled form of this wrapper is unpickled. To avoid bootstrapping problems, this function must itself be picklable by the built-inpickle.dumps.
Methods
__init__(func, args, kwargs, dumps, loads)Wraps a function and its arguments into a nullary closure that forces a chosen pickler to be used for the closure and its return value.