Repository navigation
Description
FileSystem.get_file_info(selector) returns a list, so the whole listing is materialized before the caller sees the first entry. For a very large directory or prefix that means waiting for the complete listing, and holding all of it in memory, even when the caller only wants to process entries as they arrive, or to stop once it has found what it needs.
It would be useful to overlap listing with processing: start working on the entries that have already been discovered while the rest are still being listed.
The primary use case is listing very large S3 prefixes through PyArrow's native S3FileSystem. Callers should be able to start computing on objects from the first listing page while subsequent pages are still being fetched, rather than waiting for the entire prefix to be listed. The C++ S3 implementation already pushes results as each ListObjectsV2 page completes; exposing this to Python would enable pipelined listing and processing without changing the S3 backend.
Arrow C++ already has FileSystem::GetFileInfoGenerator() for this, but it is not exposed in Python.
Proposal, with naming open to discussion: add FileSystem.get_file_info_iterator(selector), returning an iterator that yields FileInfo objects one at a time and fetches them from the filesystem in batches, so that a listing can be consumed incrementally and abandoned early.
for info in fs.get_file_info_iterator(FileSelector(path, recursive=True)):
... # info is a FileInfoBackend differences
How incremental this actually is depends on the backend. In C++ only LocalFileSystem and S3FileSystem override GetFileInfoGenerator(). GcsFileSystem, AzureFileSystem, HadoopFileSystem and PyFileSystem, which backs the fsspec integration, fall back to the base-class implementation, which lists everything and wraps the result in a single batch.
On those backends the iterator yields the same entries as get_file_info(), but the full listing is still materialized on the first next() call, so neither the memory nor the early-termination benefit applies. Only LocalFileSystem and S3FileSystem would get those today.
Possible follow-ups, out of scope here
- A capability query so callers can tell whether a filesystem actually lists incrementally. There is currently no way to detect this:
GetFileInfoGenerator()is a plain virtual method, and wrappers such asSubTreeFileSystemoverride it but forward to an inner filesystem, so the behaviour depends on what is wrapped. - Implementing
GetFileInfoGenerator()forGcsFileSystemandAzureFileSystem, whose underlying APIs are already paginated but are currently collected into a single vector.
Component(s)
Python
Activity
apacheGH-55752: [Python] Support incremental file listing with an ite…