proxystore.store.base¶
Store implementation.
Store
¶
Store(
name: str,
connector: ConnectorT,
*,
serializer: SerializerT | None = None,
deserializer: DeserializerT | None = None,
cache_size: int = 16,
metrics: bool = False,
populate_target: bool = True,
register: bool = False
)
Bases: Generic[ConnectorT]
Key-value store interface for proxies.
Tip
A Store
instance can be used as a
context manager which will automatically call
close()
on exit.
Warning
The default value of populate_target=True
can cause unexpected
behavior when providing custom serializer/deserializers because
neither the serializer nor deserializer will be applied to the target
object being cached in the resulting Proxy
.
In this example, the serialized data_bytes
was populated as the
target object in the resulting proxy so the proxy looks like a proxy
of bytes rather than the intended list of integers. To fix this, set
populate_target=False
so the custom deserializer is correctly
applied to data_bytes
when the proxy is resolved.
Note
This class is generally thread-safe, with cache access and connector operations guarded by a lock that is local to each store instance.
Warning
This class cannot be pickled. If you need to recreate a
Store
within another process, share
a StoreConfig
, a serializable
and pickle-compatbile type, that can be created using
Store.config()
.
To reconstruct the instance from the config, use
Store.from_config()
or
get_or_create_store()
.
Parameters:
-
name
(str
) –Name of the store instance.
-
connector
(ConnectorT
) –Connector instance to use for object storage.
-
serializer
(SerializerT | None
, default:None
) –Optional callable which serializes the object. If
None
, the default serializer (serialize()
) will be used. -
deserializer
(DeserializerT | None
, default:None
) –Optional callable used by the factory to deserialize the byte string. If
None
, the default deserializer (deserialize()
) will be used. -
cache_size
(int
, default:16
) –Size of LRU cache (in # of objects). If 0, the cache is disabled. The cache is local to the Python process.
-
metrics
(bool
, default:False
) –Enable recording operation metrics.
-
populate_target
(bool
, default:True
) –Set the default value of
populate_target
for proxy methods of the store. -
register
(bool
, default:False
) –Register the store instance after initialization.
Raises:
-
ValueError
–If
cache_size
is less than zero. -
StoreExistsError
–If
register=True
and a store withname
already exists.
Source code in proxystore/store/base.py
close
¶
Close the connector associated with the store.
This will (1) close the connector and (2) unregister the store if
register=True
was set during initialization.
Warning
This method should only be called at the end of the program when the store will no longer be used, for example once all proxies have been resolved.
Parameters:
-
args
(Any
, default:()
) –Positional arguments to pass to
Connector.close()
. -
kwargs
(Any
, default:{}
) –Keyword arguments to pass to
Connector.close()
.
Source code in proxystore/store/base.py
config
¶
config() -> StoreConfig
Get the store configuration.
Returns:
-
StoreConfig
–Store configuration.
Source code in proxystore/store/base.py
from_config
classmethod
¶
from_config(config: StoreConfig) -> Store[Any]
Create a new store instance from a configuration.
Parameters:
-
config
(StoreConfig
) –Configuration returned by
.config()
.
Returns:
Source code in proxystore/store/base.py
future
¶
future(
*,
evict: bool = False,
serializer: SerializerT | None = None,
deserializer: DeserializerT | None = None,
polling_interval: float = 1,
polling_backoff_factor: float = 1,
polling_interval_limit: float | None = None,
polling_timeout: float | None = None
) -> Future[T]
Create a future to an object.
Example
from proxystore.connectors.file import FileConnector
from proxystore.store import Store
from proxystore.store.future import Future
def remote_foo(future: Future) -> None:
# Computation that generates a result value needed by
# the remote_bar function.
future.set_result(...)
def remote_bar(data: Any) -> None:
# Function uses data, which is a proxy, as normal, blocking
# until the remote_foo function has called set_result.
...
with Store('future-example', FileConnector(...)) as store:
future = store.future()
# The invoke_remove function invokes a provided function
# on a remote process. For example, this could be a serverless
# function execution.
foo_result_future = invoke_remote(remote_foo, future)
bar_result_future = invoke_remote(remote_bar, future.proxy())
foo_result_future.result()
bar_result_future.result()
Warning
This method only works if the connector
is of type
DeferrableConnector
.
Warning
This method and the
Future.proxy()
are experimental features and may change in future releases.
Parameters:
-
evict
(bool
, default:False
) –If a proxy returned by
Future.proxy()
should evict the object once resolved. -
serializer
(SerializerT | None
, default:None
) –Optionally override the default serializer for the store instance.
-
deserializer
(DeserializerT | None
, default:None
) –Optionally override the default deserializer for the store instance.
-
polling_interval
(float
, default:1
) –Initial seconds to sleep between polling the store for the object.
-
polling_backoff_factor
(float
, default:1
) –Multiplicative factor applied to the polling_interval applied after each unsuccessful poll.
-
polling_interval_limit
(float | None
, default:None
) –Maximum polling interval allowed. Prevents the backoff factor from increasing the current polling interval to unreasonable values.
-
polling_timeout
(float | None
, default:None
) –Optional maximum number of seconds to poll for. If the timeout is reached an error is raised.
Returns:
-
Future[T]
–Future which can be used to get the result object at a later time or create a proxy which will resolve to the result of the future.
Raises:
-
NotImplementedError
–If the
connector
is not of typeDeferrableConnector
.
Source code in proxystore/store/base.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
|
evict
¶
evict(key: ConnectorKeyT) -> None
Evict the object associated with the key.
Parameters:
-
key
(ConnectorKeyT
) –Key associated with object to evict.
Source code in proxystore/store/base.py
exists
¶
exists(key: ConnectorKeyT) -> bool
Check if an object associated with the key exists.
Parameters:
-
key
(ConnectorKeyT
) –Key potentially associated with stored object.
Returns:
-
bool
–If an object associated with the key exists.
Source code in proxystore/store/base.py
get
¶
get(
key: ConnectorKeyT,
*,
deserializer: DeserializerT | None = None,
default: object | None = None
) -> Any | None
Get the object associated with the key.
Parameters:
-
key
(ConnectorKeyT
) –Key associated with the object to retrieve.
-
deserializer
(DeserializerT | None
, default:None
) –Optionally override the default deserializer for the store instance.
-
default
(object | None
, default:None
) –An optional value to be returned if an object associated with the key does not exist.
Returns:
-
Any | None
–Object or
None
if the object does not exist.
Raises:
-
SerializationError
–If an exception is caught when deserializing the object associated with the key.
Source code in proxystore/store/base.py
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
|
is_cached
¶
is_cached(key: ConnectorKeyT) -> bool
Check if an object associated with the key is cached locally.
Parameters:
-
key
(ConnectorKeyT
) –Key potentially associated with stored object.
Returns:
-
bool
–If the object is cached.
Source code in proxystore/store/base.py
proxy
¶
proxy(
obj: T | NonProxiableT,
*,
evict: bool = False,
lifetime: Lifetime | None = None,
serializer: SerializerT | None = None,
deserializer: DeserializerT | None = None,
populate_target: bool | None = None,
skip_nonproxiable: bool = False,
**kwargs: Any
) -> Proxy[T] | NonProxiableT
Create a proxy that will resolve to an object in the store.
Parameters:
-
obj
(T | NonProxiableT
) –The object to place in store and return a proxy for.
-
evict
(bool
, default:False
) –If the proxy should evict the object once resolved. Mutually exclusive with the
lifetime
parameter. -
lifetime
(Lifetime | None
, default:None
) –Attach the proxy to this lifetime. The object associated with the proxy will be evicted when the lifetime ends. Mutually exclusive with the
evict
parameter. -
serializer
(SerializerT | None
, default:None
) –Optionally override the default serializer for the store instance.
-
deserializer
(DeserializerT | None
, default:None
) –Optionally override the default deserializer for the store instance.
-
populate_target
(bool | None
, default:None
) –Pass
cache_defaults=True
andtarget=obj
to theProxy
constructor. I.e., return a proxy that (1) is already resolved, (2) can be used inisinstance
checks without resolving, and (3) is hashable without resolving ifobj
is a hashable type. This isFalse
by default because the returned proxy will hold a reference toobj
which will prevent garbage collectingobj
. IfNone
, defaults to the store-wide setting. -
skip_nonproxiable
(bool
, default:False
) –Return non-proxiable types (e.g., built-in constants like
bool
orNone
) rather than raising aNonProxiableTypeError
. -
kwargs
(Any
, default:{}
) –Additional keyword arguments to pass to
Connector.put()
.
Returns:
-
Proxy[T] | NonProxiableT
–A proxy of the object unless
obj
is a non-proxiable typeskip_nonproxiable is True
in which caseobj
is returned directly.
Raises:
-
NonProxiableTypeError
–If
obj
is a non-proxiable type. This behavior can be overridden by settingskip_nonproxiable=True
. -
ValueError
–If
evict
isTrue
andlifetime
is notNone
because these parameters are mutually exclusive.
Source code in proxystore/store/base.py
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 |
|
proxy_batch
¶
proxy_batch(
objs: Sequence[T | NonProxiableT],
*,
evict: bool = False,
lifetime: Lifetime | None = None,
serializer: SerializerT | None = None,
deserializer: DeserializerT | None = None,
populate_target: bool | None = None,
skip_nonproxiable: bool = False,
**kwargs: Any
) -> list[Proxy[T] | NonProxiableT]
Create proxies that will resolve to an object in the store.
Parameters:
-
objs
(Sequence[T | NonProxiableT]
) –The objects to place in store and return a proxies for.
-
evict
(bool
, default:False
) –If a proxy should evict its object once resolved. Mutually exclusive with the
lifetime
parameter. -
lifetime
(Lifetime | None
, default:None
) –Attach the proxies to this lifetime. The objects associated with each proxy will be evicted when the lifetime ends. Mutually exclusive with the
evict
parameter. -
serializer
(SerializerT | None
, default:None
) –Optionally override the default serializer for the store instance.
-
deserializer
(DeserializerT | None
, default:None
) –Optionally override the default deserializer for the store instance.
-
populate_target
(bool | None
, default:None
) –Pass
cache_defaults=True
andtarget=obj
to theProxy
constructor. I.e., return a proxy that (1) is already resolved, (2) can be used inisinstance
checks without resolving, and (3) is hashable without resolving ifobj
is a hashable type. IfNone
, defaults to the store-wide setting. -
skip_nonproxiable
(bool
, default:False
) –Return non-proxiable types (e.g., built-in constants like
bool
orNone
) rather than raising aNonProxiableTypeError
. -
kwargs
(Any
, default:{}
) –Additional keyword arguments to pass to
Connector.put_batch()
.
Returns:
-
list[Proxy[T] | NonProxiableT]
–A list of proxies of each object or the object itself if said object is not proxiable and
skip_nonproxiable is True
.
Raises:
-
NonProxiableTypeError
–If
obj
is a non-proxiable type. This behavior can be overridden by settingskip_nonproxiable=True
. -
ValueError
–If
evict
isTrue
andlifetime
is notNone
because these parameters are mutually exclusive.
Source code in proxystore/store/base.py
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 |
|
proxy_from_key
¶
proxy_from_key(
key: ConnectorKeyT,
*,
evict: bool = False,
lifetime: Lifetime | None = None,
deserializer: DeserializerT | None = None
) -> Proxy[T]
Create a proxy that will resolve to an object already in the store.
Parameters:
-
key
(ConnectorKeyT
) –The key associated with an object already in the store.
-
evict
(bool
, default:False
) –If the proxy should evict the object once resolved. Mutually exclusive with the
lifetime
parameter. -
lifetime
(Lifetime | None
, default:None
) –Attach the proxy to this lifetime. The object associated with the proxy will be evicted when the lifetime ends. Mutually exclusive with the
evict
parameter. -
deserializer
(DeserializerT | None
, default:None
) –Optionally override the default deserializer for the store instance.
Returns:
-
Proxy[T]
–A proxy of the object.
Raises:
-
ValueError
–If
evict
isTrue
andlifetime
is notNone
because these parameters are mutually exclusive.
Source code in proxystore/store/base.py
locked_proxy
¶
locked_proxy(
obj: T | NonProxiableT,
*,
evict: bool = False,
lifetime: Lifetime | None = None,
serializer: SerializerT | None = None,
deserializer: DeserializerT | None = None,
populate_target: bool | None = None,
skip_nonproxiable: bool = True,
**kwargs: Any
) -> ProxyLocker[T] | NonProxiableT
Proxy an object and return ProxyLocker
.
Parameters:
-
obj
(T | NonProxiableT
) –The object to place in store and return a proxy for.
-
evict
(bool
, default:False
) –If the proxy should evict the object once resolved. Mutually exclusive with the
lifetime
parameter. -
lifetime
(Lifetime | None
, default:None
) –Attach the proxy to this lifetime. The object associated with the proxy will be evicted when the lifetime ends. Mutually exclusive with the
evict
parameter. -
serializer
(SerializerT | None
, default:None
) –Optionally override the default serializer for the store instance.
-
deserializer
(DeserializerT | None
, default:None
) –Optionally override the default deserializer for the store instance.
-
populate_target
(bool | None
, default:None
) –Pass
cache_defaults=True
andtarget=obj
to theProxy
constructor. I.e., return a proxy that (1) is already resolved, (2) can be used inisinstance
checks without resolving, and (3) is hashable without resolving ifobj
is a hashable type. IfNone
, defaults to the store-wide setting. -
skip_nonproxiable
(bool
, default:True
) –Return non-proxiable types (e.g., built-in constants like
bool
orNone
) rather than raising aNonProxiableTypeError
. -
kwargs
(Any
, default:{}
) –Additional keyword arguments to pass to
Connector.put()
.
Returns:
-
ProxyLocker[T] | NonProxiableT
–A proxy wrapped in a
ProxyLocker
unlessobj
is a non-proxiable typeskip_nonproxiable is True
in which caseobj
is returned directly.
Raises:
-
NonProxiableTypeError
–If
obj
is a non-proxiable type. This behavior can be overridden by settingskip_nonproxiable=True
. -
ValueError
–If
evict
isTrue
andlifetime
is notNone
because these parameters are mutually exclusive.
Source code in proxystore/store/base.py
owned_proxy
¶
owned_proxy(
obj: T | NonProxiableT,
*,
serializer: SerializerT | None = None,
deserializer: DeserializerT | None = None,
populate_target: bool | None = None,
skip_nonproxiable: bool = True,
**kwargs: Any
) -> OwnedProxy[T] | NonProxiableT
Create a proxy that will enforce ownership rules over the object.
An OwnedProxy
will auto-evict
the object once it goes out of scope. This proxy type can also
be borrowed.
Parameters:
-
obj
(T | NonProxiableT
) –The object to place in store and return a proxy for.
-
serializer
(SerializerT | None
, default:None
) –Optionally override the default serializer for the store instance.
-
deserializer
(DeserializerT | None
, default:None
) –Optionally override the default deserializer for the store instance.
-
populate_target
(bool | None
, default:None
) –Pass
cache_defaults=True
andtarget=obj
to theProxy
constructor. I.e., return a proxy that (1) is already resolved, (2) can be used inisinstance
checks without resolving, and (3) is hashable without resolving ifobj
is a hashable type. IfNone
, defaults to the store-wide setting. -
skip_nonproxiable
(bool
, default:True
) –Return non-proxiable types (e.g., built-in constants like
bool
orNone
) rather than raising aNonProxiableTypeError
. -
kwargs
(Any
, default:{}
) –Additional keyword arguments to pass to
Connector.put()
.
Returns:
-
OwnedProxy[T] | NonProxiableT
–A proxy of the object unless
obj
is a non-proxiable typeskip_nonproxiable is True
in which caseobj
is returned directly.
Raises:
-
NonProxiableTypeError
–If
obj
is a non-proxiable type. This behavior can be overridden by settingskip_nonproxiable=True
.
Source code in proxystore/store/base.py
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 |
|
put
¶
put(
obj: Any,
*,
lifetime: Lifetime | None = None,
serializer: SerializerT | None = None,
**kwargs: Any
) -> ConnectorKeyT
Put an object in the store.
Parameters:
-
obj
(Any
) –Object to put in the store.
-
serializer
(SerializerT | None
, default:None
) –Optionally override the default serializer for the store instance.
-
lifetime
(Lifetime | None
, default:None
) –Attach the key to this lifetime. The object associated with the key will be evicted when the lifetime ends.
-
kwargs
(Any
, default:{}
) –Additional keyword arguments to pass to
Connector.put()
.
Returns:
-
ConnectorKeyT
–A key which can be used to retrieve the object.
Raises:
-
TypeError
–If the output of
serializer
is not bytes.
Source code in proxystore/store/base.py
put_batch
¶
put_batch(
objs: Sequence[Any],
*,
lifetime: Lifetime | None = None,
serializer: SerializerT | None = None,
**kwargs: Any
) -> list[ConnectorKeyT]
Put multiple objects in the store.
Parameters:
-
objs
(Sequence[Any]
) –Sequence of objects to put in the store.
-
serializer
(SerializerT | None
, default:None
) –Optionally override the default serializer for the store instance.
-
lifetime
(Lifetime | None
, default:None
) –Attach the keys to this lifetime. The objects associated with each key will be evicted when the lifetime ends.
-
kwargs
(Any
, default:{}
) –Additional keyword arguments to pass to
Connector.put_batch()
.
Returns:
-
list[ConnectorKeyT]
–A list of keys which can be used to retrieve the objects.
Raises:
-
TypeError
–If the output of
serializer
is not bytes.
Source code in proxystore/store/base.py
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 |
|