proxystore.store.lifetimes¶
Lifetime managers for objects in shared stores.
Learn more about managing object lifetimes in the Object Lifetimes guide.
Lifetime
¶
Bases: Protocol
Lifetime protocol.
add_key
¶
add_key(
*keys: ConnectorKeyT, store: Store[Any] | None = None
) -> None
Associate a new object with the lifetime.
Warning
All keys should have been created by the same
Store
that this lifetime was
initialized with.
Parameters:
-
keys
(ConnectorKeyT
, default:()
) –One or more keys of objects to associate with this lifetime.
-
store
(Store[Any] | None
, default:None
) –Optional
Store
thatkeys
belongs to.
Source code in proxystore/store/lifetimes.py
add_proxy
¶
Associate a new object with the lifetime.
Warning
All proxies should have been created by the same
Store
that this lifetime was
initialized with.
Parameters:
-
proxies
(Proxy[Any]
, default:()
) –One or more proxies of objects to associate with this lifetime.
Raises:
-
ProxyStoreFactoryError
–If the proxy's factory is not an instance of
StoreFactory
.
Source code in proxystore/store/lifetimes.py
ContextLifetime
¶
Basic lifetime manager.
Object lifetime manager with context manager support.
Example
from proxystore.store.base import Store
from proxystore.store.lifetimes import ContextLifetime
store = Store(...)
with ContextLifetime(store) as lifetime:
# Objects in the store can be associated with this lifetime.
key = store.put('value', lifetime=lifetime)
proxy = store.proxy('value', lifetime=lifetime)
# Objects associated with the lifetime are evicted once the
# lifetime ends.
assert not store.exists(key)
store.close()
Parameters:
-
store
(Store[Any]
) –Store
instance use to create the objects associated with this lifetime and that will be used to evict them when the lifetime has ended. -
name
(str | None
, default:None
) –Specify a name for this lifetime used in logging. Otherwise, a unique ID will be generated.
Source code in proxystore/store/lifetimes.py
add_key
¶
add_key(
*keys: ConnectorKeyT, store: Store[Any] | None = None
) -> None
Associate a new object with the lifetime.
Warning
All keys should have been created by the same
Store
that this lifetime was
initialized with.
Parameters:
-
keys
(ConnectorKeyT
, default:()
) –One or more keys of objects to associate with this lifetime.
-
store
(Store[Any] | None
, default:None
) –Optional
Store
thatkeys
belongs to. Ignored by this implementation.
Raises:
-
RuntimeError
–If this lifetime has ended.
Source code in proxystore/store/lifetimes.py
add_proxy
¶
Associate a new object with the lifetime.
Warning
All proxies should have been created by the same
Store
that this lifetime was
initialized with.
Parameters:
-
proxies
(Proxy[Any]
, default:()
) –One or more proxies of objects to associate with this lifetime.
Raises:
-
ProxyStoreFactoryError
–If the proxy's factory is not an instance of
StoreFactory
. -
RuntimeError
–If this lifetime has ended.
Source code in proxystore/store/lifetimes.py
close
¶
close(*, close_stores: bool = False) -> None
End the lifetime and evict all associated objects.
Parameters:
-
close_stores
(bool
, default:False
) –Close any
Store
store instances associated with the lifetime.
Source code in proxystore/store/lifetimes.py
LeaseLifetime
¶
LeaseLifetime(
store: Store[Any],
expiry: datetime | timedelta | float,
*,
name: str | None = None
)
Bases: ContextLifetime
Time-based lease lifetime manager.
Example
from proxystore.store.base import Store
from proxystore.store.lifetimes import LeaseLifetime
with Store(...) as store:
# Create a new lifetime with a current lease of ten seconds.
lifetime = LeaseLifetime(store, expiry=10)
# Objects in the store can be associated with this lifetime.
key = store.put('value', lifetime=lifetime)
proxy = store.proxy('value', lifetime=lifetime)
# Extend the lease by another five seconds.
lifetime.extend(5)
time.sleep(15)
# Lease has expired so the lifetime has ended.
assert lifetime.done()
assert not store.exists(key)
Parameters:
-
store
(Store[Any]
) –Store
instance use to create the objects associated with this lifetime and that will be used to evict them when the lifetime has ended. -
expiry
(datetime | timedelta | float
) – -
name
(str | None
, default:None
) –Specify a name for this lifetime used in logging. Otherwise, a unique ID will be generated.
Source code in proxystore/store/lifetimes.py
add_key
¶
add_key(
*keys: ConnectorKeyT, store: Store[Any] | None = None
) -> None
Associate a new object with the lifetime.
Warning
All keys should have been created by the same
Store
that this lifetime was
initialized with.
Parameters:
-
keys
(ConnectorKeyT
, default:()
) –One or more keys of objects to associate with this lifetime.
-
store
(Store[Any] | None
, default:None
) –Optional
Store
thatkeys
belongs to. Ignored by this implementation.
Raises:
-
RuntimeError
–If this lifetime has ended.
Source code in proxystore/store/lifetimes.py
add_proxy
¶
Associate a new object with the lifetime.
Warning
All proxies should have been created by the same
Store
that this lifetime was
initialized with.
Parameters:
-
proxies
(Proxy[Any]
, default:()
) –One or more proxies of objects to associate with this lifetime.
Raises:
-
ProxyStoreFactoryError
–If the proxy's factory is not an instance of
StoreFactory
. -
RuntimeError
–If this lifetime has ended.
Source code in proxystore/store/lifetimes.py
close
¶
close(*, close_stores: bool = False) -> None
End the lifetime and evict all associated objects.
This can be called before the specified expiry time to end the lifetime early.
Parameters:
-
close_stores
(bool
, default:False
) –Close any
Store
store instances associated with the lifetime.
Source code in proxystore/store/lifetimes.py
extend
¶
Extend the expiry of the lifetime lease.
Parameters:
Source code in proxystore/store/lifetimes.py
StaticLifetime
¶
Static lifetime manager.
Keeps associated objects alive for the remainder of the program.
Note
This is a singleton class.
Warning
This class registers an atexit handler which will close
the lifetime at the end of the program, evicting all objects associated
with the lifetime. Therefore, Store
instances used to created objects associated with the static lifetime
should not be closed prior to program exit. The handler will close
all of these stores. It is possible to call StaticLifetime().close()
manually, after which it is safe to also close the stores.
Example
- The atexit handler will call
store.close()
at the end of the program. Settingregister=True
is recommended to prevent another instance being created internally when a proxy is resolved. - The object associated with
key
will be evicted at the end of the program. - The object associated with
proxy
will be evicted at the end of the program.
Source code in proxystore/store/lifetimes.py
add_key
¶
add_key(
*keys: ConnectorKeyT, store: Store[Any] | None = None
) -> None
Associate a new object with the lifetime.
Parameters:
-
keys
(ConnectorKeyT
, default:()
) –One or more keys of objects to associate with this lifetime.
-
store
(Store[Any] | None
, default:None
) –Store
thatkeys
belongs to. Required by this implementation.
Raises:
-
RuntimeError
–If this lifetime has ended.
-
ValueError
–If
store
isNone
.
Source code in proxystore/store/lifetimes.py
add_proxy
¶
Associate a new object with the lifetime.
Warning
This method will initialized new
Store
instances if the stores
which were used to create the input proxies have not been
registered by setting the register
flag or by calling
register_store()
.
Parameters:
-
proxies
(Proxy[Any]
, default:()
) –One or more proxies of objects to associate with this lifetime.
Raises:
-
ProxyStoreFactoryError
–If the proxy's factory is not an instance of
StoreFactory
. -
RuntimeError
–If this lifetime has ended.
Source code in proxystore/store/lifetimes.py
close
¶
close(*, close_stores: bool = False) -> None
End the lifetime and evict all associated objects.
Warning
Because this class is a singleton this operation can only be performed once.
Parameters:
-
close_stores
(bool
, default:False
) –Close any
Store
store instances associated with the lifetime.
Source code in proxystore/store/lifetimes.py
register_lifetime_atexit
¶
Register atexit callback to cleanup the lifetime.
Registers an atexit callback which will close the lifetime on normal program exit and optionally close the associated store as well.
Tip
Do not close the Store
associated
with the lifetime when registering an atexit callback. Using a
Store
after closing it is undefined
behaviour. Rather, let the callback handle closing after it is
safe to do so.
Warning
Callbacks are not guaranteed to be called in all cases. See the
atexit
docs for more details.
Parameters:
-
lifetime
(Lifetime
) –Lifetime to be closed at exit.
-
close_stores
(bool
, default:True
) –Close any
Store
instances associated with the lifetime.
Returns:
-
Callable[[], None]
–The registered callback function which can be used with
atexit.unregister()
if needed.