proxystore.store.ref¶
Object ownership and borrowing with proxies.
This module implements Rust-like ownership and borrowing rules for Python
objects in shared memory using transparent object proxies. Thus, these
proxy reference types are similar to the type returned by
Store.proxy()---they will resolve
to an object in the global store. However, these proxy references enforce
additional rules.
- Each target object of type
Tin the global store has an associatedOwnedProxy[T]. - There can only be one
OwnedProxy[T]for any target in the global store. - When an
OwnedProxy[T]goes out of scope (e.g., gets garbage collected), the associated target is removed from the global store.
Tip
The docstrings often use T to refer to both the target object in the
global store and the type of the target object.
An OwnedProxy[T] can be borrowed without
relinquishing ownership. This requires two additional rules.
- At any given time, you can have either one mutable reference to the
target, a
RefMutProxy[T], or any number of immutable references, aRefProxy[T]. - References must always be valid. I.e., you cannot delete an
OwnedProxy[T]while it has been borrowed via aRefProxy[T]orRefMutProxy[T].
All three reference types (OwnedProxy[T],
RefProxy[T], and
RefMutProxy[T]) behave like an instance
of T, forwarding operations on themselves to a locally cached instance of
T.
MutableBorrowError
¶
Bases: BaseRefProxyError
Exception raised when violating borrowing rules.
ReferenceNotOwnedError
¶
Bases: BaseRefProxyError
Exception raised when invoking an operation on a non-owned reference.
ReferenceInvalidError
¶
Bases: BaseRefProxyError
Exception raised when a reference instance has been invalidated.
ReferenceBorrowedError
¶
Bases: BaseRefProxyError
Exception raised when moving an owned reference that is borrowed.
BaseRefProxy
¶
BaseRefProxy(
factory: FactoryType[T],
*,
cache_defaults: bool = False,
target: T | None = None,
)
Bases: Proxy[T]
Base reference proxy type.
This base type adds some features to Proxy
that are shared by all the reference types:
- Valid flag. When invalidated, the proxy will raise a
ReferenceInvalidErrorwhen accessing the wrapped target object. - Disable
copy()anddeepcopy()support to prevent misuse of the API. Generally,borrow()orclone()should be used instead.
Source code in proxystore/store/ref.py
OwnedProxy
¶
OwnedProxy(
factory: FactoryType[T],
*,
cache_defaults: bool = False,
target: T | None = None,
)
Bases: BaseRefProxy[T]
Represents ownership over an object in a global store.
This class maintains reference counts of the number of immutable and
mutable borrows of this proxy. The target object will be evicted from
the store by a background thread shortly after this proxy goes out of
scope (this is handled via __del__ and an atexit handler).
Store.close() and the exit of the
program wait for these evictions.
Pickling this proxy moves ownership to the pickled copy and invalidates
this proxy, so an owned proxy can only be pickled once. Use
borrow() or
clone() to send the object to more than
one place.
Parameters:
-
factory(FactoryType[T]) –StoreFactoryused to resolve the target object from the store. -
cache_defaults(bool, default:False) –Precompute and cache the
__proxy_default_class__and__proxy_default_hash__attributes of the proxy instance fromtarget. Ignored iftargetis not provided. -
target(T | None, default:None) –Optionally preset the target object.
Source code in proxystore/store/ref.py
RefProxy
¶
RefProxy(
factory: FactoryType[T],
*,
cache_defaults: bool = False,
owner: OwnedProxy[T] | None = None,
target: T | None = None,
)
Bases: BaseRefProxy[T]
Represents a borrowed reference to an object in the global store.
Parameters:
-
factory(FactoryType[T]) –StoreFactoryused to resolve the target object from the store. -
cache_defaults(bool, default:False) –Precompute and cache the
__proxy_default_class__and__proxy_default_hash__attributes of the proxy instance fromtarget. Ignored iftargetis not provided. -
owner(OwnedProxy[T] | None, default:None) –Proxy which has ownership over the target object. This reference will keep the owner alive while this borrowed reference is alive. In the event this borrowed reference was initialized in a different address space from the proxy with ownership, then
ownerwill beNone. -
target(T | None, default:None) –Optionally preset the target object.
Source code in proxystore/store/ref.py
RefMutProxy
¶
RefMutProxy(
factory: FactoryType[T],
*,
cache_defaults: bool = False,
owner: OwnedProxy[T] | None = None,
target: T | None = None,
)
Bases: BaseRefProxy[T]
Represents a borrowed mutable reference to an object in the global store.
Parameters:
-
factory(FactoryType[T]) –StoreFactoryused to resolve the target object from the store. -
cache_defaults(bool, default:False) –Precompute and cache the
__proxy_default_class__and__proxy_default_hash__attributes of the proxy instance fromtarget. Ignored iftargetis not provided. -
owner(OwnedProxy[T] | None, default:None) –Proxy which has ownership over the target object. This reference will keep the owner alive while this borrowed reference is alive. In the event this borrowed reference was initialized in a different address space from the proxy with ownership, then
ownerwill beNone. -
target(T | None, default:None) –Optionally preset the target object.
Source code in proxystore/store/ref.py
borrow
¶
borrow(
proxy: OwnedProxy[T], *, populate_target: bool = True
) -> RefProxy[T]
Borrow T by creating an immutable reference of T.
Note
This mutates proxy.
Parameters:
-
proxy(OwnedProxy[T]) –Proxy reference to borrow.
-
populate_target(bool, default:True) –If the target of
proxyhas already been resolved, copy the reference to the target into the returned proxy such that the returned proxy is already resolved.
Raises:
-
ReferenceNotOwnedError–if
proxyis not anOwnedProxyinstance. -
MutableBorrowError–if
proxyhas already been mutably borrowed.
Source code in proxystore/store/ref.py
mut_borrow
¶
mut_borrow(
proxy: OwnedProxy[T], *, populate_target: bool = True
) -> RefMutProxy[T]
Mutably borrow T by creating a mutable reference of T.
Note
This mutates proxy.
Parameters:
-
proxy(OwnedProxy[T]) –Proxy reference to borrow.
-
populate_target(bool, default:True) –If the target of
proxyhas already been resolved, copy the reference to the target into the returned proxy such that the returned proxy is already resolved.
Raises:
-
ReferenceNotOwnedError–if
proxyis not anOwnedProxyinstance. -
MutableBorrowError–if
proxyhas already been borrowed (mutable/immutable).
Source code in proxystore/store/ref.py
clone
¶
clone(proxy: OwnedProxy[T]) -> OwnedProxy[T]
Clone the target object.
Creates a new copy of T in the global store by copying the stored
object, and returns a new owner of the copy. The copy is made when
clone() is called, so it does not change if proxy is updated or
evicted later. The returned proxy is not resolved, even if proxy is,
so it resolves its own copy from the store when first used.
Raises:
-
ReferenceNotOwnedError–if
proxyis not anOwnedProxyinstance. -
MutableBorrowError–if
proxyis mutably borrowed, because the changes of the borrow may not be written to the store yet. -
ProxyResolveMissingKeyError–if the object of
proxyis not in the store.
Source code in proxystore/store/ref.py
into_owned
¶
into_owned(
proxy: Proxy[T], *, populate_target: bool = True
) -> OwnedProxy[T]
Convert a basic proxy into an owned proxy.
Warning
It is the caller's responsibility to ensure that proxy has not been
copied already.
Note
This will unset the evict flag on the proxy.
Parameters:
-
proxy(Proxy[T]) –Proxy to convert.
-
populate_target(bool, default:True) –If the target of
proxyhas already been resolved, copy the reference to the target into the returned proxy such that the returned proxy is already resolved.
Raises:
-
ValueError–if
proxyis already aBaseRefProxyinstance. -
ProxyStoreFactoryError–If the proxy's factory is not an instance of
StoreFactory.
Source code in proxystore/store/ref.py
update
¶
update(proxy: OwnedProxy[T] | RefMutProxy[T]) -> None
Update the global copy of the target.
Note
If the proxy has not been resolved, there is nothing to update and this function is a no-op.
Warning
This will not invalidate already cached copies of the global target.
Parameters:
-
proxy(OwnedProxy[T] | RefMutProxy[T]) –Proxy containing a modified local copy of the target to use as the new global value.
Raises:
-
MutableBorrowError–if
proxyhas been mutably borrowed. -
NotImplementedError–if the
connectorof thestoreused to create the proxy does not implement theDeferrableConnectorprotocol. -
ReferenceNotOwnedError–if
proxyis not anOwnedProxyorRefMutProxyinstance.