proxystore.store.ref¶
Object ownership and borrowing with proxies.
Warning
These features are experimental and may change in future releases.
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
T
in 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.
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
ReferenceInvalidError
when 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 once this proxy goes out of scope (this is handled via
__del__
and an atexit handler).
Parameters:
-
factory
(FactoryType[T]
) –StoreFactory
used 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 iftarget
is 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]
) –StoreFactory
used 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 iftarget
is 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
owner
will 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]
) –StoreFactory
used 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 iftarget
is 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
owner
will 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
proxy
has already been resolved, copy the reference to the target into the returned proxy such that the returned proxy is already resolved.
Raises:
-
ReferenceNotOwnedError
–if
proxy
is not anOwnedProxy
instance. -
MutableBorrowError
–if
proxy
has 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 an 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
proxy
has already been resolved, copy the reference to the target into the returned proxy such that the returned proxy is already resolved.
Raises:
-
ReferenceNotOwnedError
–if
proxy
is not anOwnedProxy
instance. -
MutableBorrowError
–if
proxy
has 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. If proxy
is in
the resolved state, the local version of T
belonging to proxy
will
be deepcopied into the cloned proxy.
Raises:
-
ReferenceNotOwnedError
–if
proxy
is not anOwnedProxy
instance.
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 reference to borrow.
-
populate_target
(bool
, default:True
) –If the target of
proxy
has already been resolved, copy the reference to the target into the returned proxy such that the returned proxy is already resolved.
Raises:
-
ValueError
–if
proxy
is already aBaseRefProxy
instance. -
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],
*,
serializer: SerializerT | None = None
) -> 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.
-
serializer
(SerializerT | None
, default:None
) –Optionally override the default serializer for the store instance when pushing the local copy to the store.
Raises:
-
MutableBorrowError
–if
proxy
has been mutably borrowed. -
NotImplementedError
–if the
connector
of thestore
used to create the proxy does not implement theDeferrableConnector
protocol. -
ReferenceNotOwnedError
–if
proxy
is not anOwnedProxy
orRefMutProxy
instance.