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

Adds as_completed utility for PrefectFuture #14641

Merged
merged 18 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
53 changes: 53 additions & 0 deletions src/prefect/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,59 @@ def result(
) from exc


def as_completed(futures: List[PrefectFuture], timeout=None) -> Iterator[PrefectFuture]:
jeanluciano marked this conversation as resolved.
Show resolved Hide resolved
"""
An iterator over the given futures that yields each as it completes.

Args:
fs: The sequence of Futures (possibly created by different Executors) to
jeanluciano marked this conversation as resolved.
Show resolved Hide resolved
iterate over.
timeout: The maximum number of seconds to wait. If None, then there
is no limit on the wait time.

Returns:
An iterator that yields the given Futures as they complete (finished or
cancelled). If any given Futures are duplicated, they will be returned
once.

Raises:
TimeoutError: If the entire result iterator could not be generated
before the given timeout.

Examples:
```python
@task
def sleep_task(seconds):
sleep(seconds)
return 42

@flow
def flow():
futures = random_task.map(range(10))
for future in as_completed(futures):
print(future.result())
```
"""
futures = set(futures)
jeanluciano marked this conversation as resolved.
Show resolved Hide resolved
total_futures = len(futures)
try:
with timeout_context(timeout):
done = {f for f in futures if f._final_state}
pending = futures - done
for future in done:
yield future

for future in pending.copy():
future.wait()
pending.remove(future)
yield future

except TimeoutError:
raise TimeoutError(
"%d (of %d) futures unfinished" % (len(pending), total_futures)
)


DoneAndNotDoneFutures = collections.namedtuple("DoneAndNotDoneFutures", "done not_done")


Expand Down
20 changes: 20 additions & 0 deletions tests/test_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
PrefectFuture,
PrefectFutureList,
PrefectWrappedFuture,
as_completed,
resolve_futures_to_states,
wait,
)
Expand Down Expand Up @@ -55,6 +56,25 @@ def test_wait_with_timeout(self):
futures = wait(mock_futures, timeout=0.01)
assert futures.not_done == {mock_futures[-1]}

def test_as_completed(self):
mock_futures = [MockFuture(data=i) for i in range(5)]
for future in as_completed(mock_futures):
assert future.state.is_completed()

@pytest.mark.timeout(method="thread")
def test_as_completed_with_timeout(self):
mock_futures = [MockFuture(data=i) for i in range(5)]
hanging_future = Future()
mock_futures.append(PrefectConcurrentFuture(uuid.uuid4(), hanging_future))

with pytest.raises(TimeoutError) as exc_info:
for future in as_completed(mock_futures, timeout=0.01):
assert future.state.is_completed()

assert (
exc_info.value.args[0] == f"1 (of {len(mock_futures)}) futures unfinished"
)


class TestPrefectConcurrentFuture:
def test_wait_with_timeout(self):
Expand Down