Model Sharing
The lenskit.sharing
module provides utilities for managing models and sharing them
between processes, particularly for the multiprocessing in lenskit.batch
.
Persistence API
These functions are used for internal LensKit infrastructure code to persist models into shared memory for parallel processing.
- lenskit.sharing.persist(model, *, method=None)
Persist a model for cross-process sharing.
This will return a persisted model that can be used to reconstruct the model in a worker process (using
PersistedModel.get()
).If no method is provided, this function automatically selects a model persistence strategy from the the following, in order:
If LK_TEMP_DIR is set, use
binpickle
in shareable mode to save the object into the LensKit temporary directory.If
multiprocessing.shared_memory
is available, usepickle
to save the model, placing the buffers into shared memory blocks.Otherwise, use
binpickle
in shareable mode to save the object into the system temporary directory.
- Parameters
model (obj) – The model to persist.
method (str or None) – The method to use. Can be one of
binpickle
orshm
.
- Returns
The persisted object.
- Return type
- class lenskit.sharing.PersistedModel
Bases:
ABC
A persisted model for inter-process model sharing.
These objects can be pickled for transmission to a worker process.
Note
Subclasses need to override the pickling protocol to implement the proper pickling implementation.
- abstract get()
Get the persisted model, reconstructing it if necessary.
- abstract close()
Release the persisted model resources. Should only be called in the parent process (will do nothing in a child process).
- transfer()
Mark an object for ownership transfer. This object, when pickled, will unpickle into an owning model that frees resources when closed. Used to transfer ownership of shared memory resources from child processes to parent processes. Such an object should only be unpickled once.
The default implementation sets the
is_owner
attribute to'transfer'
.- Returns
self
(for convenience)