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 OncePickledObject that uses the same dumps and loads functions as the wrapper.

This is useful for wrapping functions that will be submitted to executors like ProcessPoolExecutor with 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 func that we’re closing over

  • kwargs – the keyword arguments to func that we’re closing over

  • dumps – a pickle.dumps-like function that will be used for serializing the closure contents when this wrapper is pickled. This function must accept a protocol keyword 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-in pickle.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.

__call__()[source]

Call self as a function.

__reduce_ex__(protocol: int)[source]

Helper for pickle.