proxystore.proxy¶
Proxy implementation and helpers.
ProxyOr
module-attribute
¶
Type alias for a union of a type T
or a Proxy[T]
.
This type alias is useful for typing functions that operate on or return mixed types involving proxies.
Proxy
¶
Proxy(
factory: FactoryType[T],
*,
cache_defaults: bool = False,
target: T | None = None
)
Bases: as_metaclass(ProxyMetaType)
, Generic[T]
Lazy object proxy.
An extension of the Proxy from lazy-object-proxy with modified attribute lookup and pickling behavior.
An object proxy acts as a thin wrapper around a Python object, i.e. the proxy behaves identically to the underlying object. The proxy is initialized with a callable factory object. The factory returns the underlying object when called, i.e. resolves the proxy. This means a proxy performs lazy/just-in-time resolution, i.e., the proxy does not call the factory until the first access to the proxy.
Note
The factory
, by default, is only ever called once during the
lifetime of a proxy instance.
Note
When a proxy instance is pickled, only the factory
is pickled, not
the wrapped object. Thus, proxy instances can be pickled and passed
around cheaply, and once the proxy is unpickled and used, the factory
will be called again to resolve the object.
Tip
Common data structures (e.g., dict
or set
) and
operations (e.g., isinstance
) will resolve an
unresolved proxy. This can result in unintentional performance
degradation for expensive factories, such as those that require
significant I/O or produce target objects that require a lot of memory.
The target
and cache_defaults
parameters of
Proxy
can prevent these unintenional
proxy resolves by caching the __class__
and __hash__
values of the
target object in the proxy.
- Using
isinstance
calls__class__
on the target object which requires the proxy to be resolved. In many cases, it may be desirable to check the type of a proxy's target object without incurring the cost of resolving the target. - If the target is available when constructing the proxy, the
proxy can precompute and cache the
__class__
and__hash__
values of the target. - Using
isinstance
no longer requires the proxy to be resolved, instead using the precomputed value.
Warning
A proxy of a singleton type (e.g., True
, False
, and None
) will
not behave exactly as a singleton type would. This is because the
proxy itself is not a singleton.
Warning
Python bindings to other languages (e.g., C, C++) may throw type
errors when receiving a Proxy
instance.
Casting the proxy or extracting the target object may be needed.
Attributes:
-
__proxy_factory__
(FactoryType[T]
) –Factory function which resolves to the target object.
-
__proxy_target__
(T
) –The target object once resolved.
-
__proxy_resolved__
(bool
) –True
if__proxy_target__
is set. -
__proxy_wrapped__
(T
) –A property that either returns
__proxy_target__
if it exists else calls__proxy_factory__
, saving the result to__proxy_target__
and returning said result. -
__proxy_default_class__
(DefaultClassType
) –Optional default class type value to use when a proxy is in the unresolved state. This avoids needing to resolve the proxy to perform
isinstance
checks. This value is always ignored while the proxy is resolved because__class__
is a writable property of the cached target and could be altered. -
__proxy_default_hash__
(DefaultHashType
) –Optional default hash value to use when a proxy is in the unresolved state and
hash()
is called. This avoids needing to resolve the proxy for simple operations like dictionary updates. This value is always ignored while the proxy is resolved because the cached target may be modified which can alter the value of the hash.
Parameters:
-
factory
(FactoryType[T]
) –Callable object that returns the underlying object when called. The factory should be pure meaning that every call of the factory returns the same object.
-
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.
Raises:
-
TypeError
–If
factory
is not callable.
Source code in proxystore/proxy/__init__.py
ProxyLocker
¶
ProxyLocker(proxy: Proxy[T])
Bases: Generic[T]
Proxy locker that prevents resolution of wrapped proxies.
The class prevents unintended access to a wrapped proxy to ensure a proxy
is not resolved. The wrapped proxy can be retrieved with
proxy = ProxyLocker(proxy).unlock()
.
Parameters:
-
proxy
(Proxy[T]
) –Proxy to lock.
Source code in proxystore/proxy/__init__.py
get_factory
¶
get_factory(proxy: Proxy[T]) -> FactoryType[T]
Get the factory contained in a proxy.
Parameters:
-
proxy
(Proxy[T]
) –Proxy instance to get the factory from.
Returns:
-
FactoryType[T]
–The factory, a callable object which, when invoked, returns an object
-
FactoryType[T]
–of type
T
.
Source code in proxystore/proxy/__init__.py
extract
¶
extract(proxy: Proxy[T]) -> T
Return object wrapped by proxy.
If the proxy has not been resolved yet, this will force the proxy to be resolved prior.
Parameters:
-
proxy
(Proxy[T]
) –Proxy instance to extract from.
Returns:
-
T
–Object wrapped by proxy.