-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
191 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from contextlib import contextmanager, nullcontext | ||
from functools import partial | ||
from typing import TYPE_CHECKING, Optional | ||
|
||
from fsspec.callbacks import DEFAULT_CALLBACK, Callback | ||
|
||
from datachain.asyn import AsyncMapper | ||
from datachain.cache import temporary_cache | ||
from datachain.catalog.catalog import Catalog | ||
from datachain.lib.file import File | ||
|
||
if TYPE_CHECKING: | ||
from contextlib import AbstractContextManager | ||
|
||
from datachain.cache import DataChainCache as Cache | ||
|
||
|
||
def noop(*args, **kwargs): | ||
pass | ||
|
||
|
||
async def _prefetch_input(row, catalog, download_cb): | ||
try: | ||
callback = download_cb.increment_file_count | ||
except AttributeError: | ||
callback = noop | ||
|
||
for obj in row: | ||
if isinstance(obj, File): | ||
obj._set_stream(catalog, True, download_cb) | ||
await obj._prefetch() | ||
callback() | ||
return row | ||
|
||
|
||
@contextmanager | ||
def catalog_with_cache(catalog: Catalog, cache): | ||
ocache = catalog.cache | ||
try: | ||
catalog.cache = cache | ||
yield catalog | ||
finally: | ||
catalog.cache = ocache | ||
|
||
|
||
def rows_prefetcher( | ||
catalog, | ||
rows, | ||
prefetch: int, | ||
cache: Optional["Cache"] = None, | ||
download_cb: Callback = DEFAULT_CALLBACK, | ||
): | ||
cache_ctx: AbstractContextManager[Cache] | ||
if cache: | ||
cache_ctx = nullcontext(cache) | ||
else: | ||
tmp_dir = catalog.cache.tmp_dir | ||
assert tmp_dir | ||
cache_ctx = temporary_cache(tmp_dir, prefix="prefetch-") | ||
|
||
with cache_ctx as prefetch_cache, catalog_with_cache(catalog, prefetch_cache): | ||
func = partial(_prefetch_input, download_cb=download_cb, catalog=catalog) | ||
mapper = AsyncMapper(func, rows, workers=prefetch) | ||
yield from mapper.iterate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters