Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing blocking behavior of imap_queued #14

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions compass_sdk/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import glob
import os
from collections import deque
from concurrent.futures import Executor, Future
from concurrent import futures
from concurrent.futures import Executor
from typing import Callable, Iterable, Iterator, List, Optional, TypeVar

import fsspec
Expand All @@ -15,16 +15,17 @@

def imap_queued(executor: Executor, f: Callable[[T], U], it: Iterable[T], max_queued: int) -> Iterator[U]:
assert max_queued >= 1
tasks = deque[Future[U]]()
futures_set = set()

for x in it:
tasks.append(executor.submit(f, x))

while len(tasks) > max_queued:
yield tasks.popleft().result()

while tasks:
yield tasks.popleft().result()
futures_set.add(executor.submit(f, x))
while len(futures_set) > max_queued:
done, futures_set = futures.wait(futures_set, return_when=futures.FIRST_COMPLETED)
for future in done:
yield future.result()

for future in futures.as_completed(futures_set):
yield future.result()


def get_fs(document_path: str) -> AbstractFileSystem:
Expand Down
Loading