blosc2.Proxy.afetch#

async Proxy.afetch(item: slice | list[slice] | None = None) NDArray | SChunk#

Retrieve the cache container with the requested data updated asynchronously.

Parameters:

item (slice or list of slices, optional) – If provided, only the chunks intersecting with the specified slices will be retrieved if they have not been already.

Returns:

out – The local container used to cache the already requested data.

Return type:

NDArray or SChunk

Notes

This method is only available if the ProxySource or ProxyNDSource have an async aget_chunk method.

Examples

>>> import numpy as np
>>> import blosc2
>>> import asyncio
>>> from blosc2 import ProxyNDSource
>>> class MyProxySource(ProxyNDSource):
>>>     def __init__(self, data):
>>>         # If the next source is multidimensional, it must have the attributes:
>>>         self.data = data
>>>         f"Data shape: {self.shape}, Chunks: {self.chunks}"
>>>         f"Blocks: {self.blocks}, Dtype: {self.dtype}"
>>>     @property
>>>     def shape(self):
>>>         return self.data.shape
>>>     @property
>>>     def chunks(self):
>>>         return self.data.chunks
>>>     @property
>>>     def blocks(self):
>>>         return self.data.blocks
>>>     @property
>>>     def dtype(self):
>>>         return self.data.dtype
>>>     # This method must be present
>>>     def get_chunk(self, nchunk):
>>>         return self.data.get_chunk(nchunk)
>>>     # This method is optional
>>>     async def aget_chunk(self, nchunk):
>>>         await asyncio.sleep(0.1) # Simulate an asynchronous operation
>>>         return self.data.get_chunk(nchunk)
>>> data = np.arange(20).reshape(4, 5)
>>> chunks = [2, 5]
>>> blocks = [1, 5]
>>> data = blosc2.asarray(data, chunks=chunks, blocks=blocks)
>>> source = MyProxySource(data)
>>> proxy = blosc2.Proxy(source)
>>> async def fetch_data():
>>>     # Fetch a slice of the data from the proxy asynchronously
>>>     slice_data = await proxy.afetch(slice(0, 2))
>>>     # Note that only data fetched is shown, the rest is uninitialized
>>>     slice_data[:]
>>> asyncio.run(fetch_data())
>>> # Using getitem to get a slice of the data
>>> result = proxy[1:2, 1:3]
>>> f"Proxy getitem: {result}"
Data shape: (4, 5), Chunks: (2, 5)
Blocks: (1, 5), Dtype: int64
[[0 1 2 3 4]
[5 6 7 8 9]
[0 0 0 0 0]
[0 0 0 0 0]]
Proxy getitem: [[6 7]]