-
tensorstore.KvStore.read(self, key: str, *, if_not_equal: str | None =
None
, staleness_bound: float | None =None
, batch: Batch | None =None
) Future[KvStore.ReadResult] Reads the value of a single key.
A missing key is not treated as an error; instead, a
ReadResult
withReadResult.state
set to'missing'
is returned.Note
The behavior in the case of a missing key differs from that of
__getitem__
, which raisesKeyError
to indicate a missing key.Example
>>> store = await ts.KvStore.open({'driver': 'memory'}) >>> await store.write(b'a', b'value') KvStore.TimestampedStorageGeneration(...) >>> await store.read(b'a') KvStore.ReadResult(state='value', value=b'value', stamp=KvStore.TimestampedStorageGeneration(...)) >>> store[b'a'] b'value' >>> await store.read(b'b') KvStore.ReadResult(state='missing', value=b'', stamp=KvStore.TimestampedStorageGeneration(...)) >>> store[b'b'] Traceback (most recent call last): ... KeyError...
>>> store[b'a'] = b'value' >>> store[b'b'] = b'value' >>> store.list().result()
If a
transaction
is bound, the read reflects any writes made within the transaction, and the commit of the transaction will fail if the value associated withkey
changes after the read due to external writes, i.e. consistent reads are guaranteed.- Parameters:¶
- key: str¶
The key to read. This is appended (without any separator) to the existing
path
, if any.- if_not_equal: str | None =
None
¶ If specified, the read is aborted if the generation associated with
key
matchesif_not_equal
. An aborted read due to this condition is indicated by aReadResult.state
of'unspecified'
. This may be useful for validating a cached value cache validation at a higher level.- staleness_bound: float | None =
None
¶ Specifies a time in (fractional) seconds since the Unix epoch. If specified, data that is cached internally by the kvstore implementation may be used without validation if not older than the
staleness_bound
. Cached data older thanstaleness_bound
must be validated before being returned. A value offloat('inf')
indicates that the result must be current as of the time theread
request was made, i.e. it is equivalent to specifying a value oftime.time()
. A value offloat('-inf')
indicates that cached data may be returned without validation irrespective of its age.- batch: Batch | None =
None
¶ Batch to use for the read operation.
Warning
If specified, the returned
Future
will not, in general, become ready until the batch is submitted. Therefore, immediately awaiting the returned future will lead to deadlock.
- Returns:¶
Future that resolves when the read operation completes.
See also