proxystore.proxy¶
Proxy implementation and helpers.
Proxy ¶
Bases: Proxy
, Generic[T]
Lazy Object Proxy.
An extension of the Proxy from lazy-object-proxy with modified 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. The does just-in-time resolution, i.e., the proxy does not call the factory until the first access to the proxy (hence, the lazy aspect of the proxy).
The factory contains the mechanisms to appropriately resolve the object, e.g., which in the case for ProxyStore means requesting the correct object from the backend store.
x = np.array([1, 2, 3])
f = ps.factory.SimpleFactory(x)
p = ps.proxy.Proxy(f)
assert isinstance(p, np.ndarray)
assert np.array_equal(p, [1, 2, 3])
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.
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.
Parameters:
-
factory
(FactoryType[T]
) –Callable object that returns the underlying object when called.
Raises:
-
TypeError
–If
factory
is not callable.
Source code in proxystore/proxy.py
ProxyLocker ¶
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.py
extract() ¶
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.