|
| 1 | +# |
| 2 | +# Copyright (c) 2023-2024 - Restate Software, Inc., Restate GmbH |
| 3 | +# |
| 4 | +# This file is part of the Restate SDK for Python, |
| 5 | +# which is released under the MIT license. |
| 6 | +# |
| 7 | +# You can find a copy of the license in file LICENSE in the root |
| 8 | +# directory of this repository or package, or at |
| 9 | +# https://github.com/restatedev/sdk-typescript/blob/main/LICENSE |
| 10 | +# |
| 11 | +# pylint: disable=R0913,C0301,R0917 |
| 12 | +# pylint: disable=line-too-long |
| 13 | +"""combines multiple futures into a single future""" |
| 14 | + |
| 15 | +from typing import Any, AsyncGenerator, List, Tuple |
| 16 | +from restate.exceptions import TerminalError |
| 17 | +from restate.context import RestateDurableFuture |
| 18 | +from restate.server_context import ServerDurableFuture, ServerInvocationContext |
| 19 | + |
| 20 | +FIRST_COMPLETED = 1 |
| 21 | +ALL_COMPLETED = 2 |
| 22 | + |
| 23 | +async def wait(*futures: RestateDurableFuture[Any], mode: int = FIRST_COMPLETED) -> Tuple[List[RestateDurableFuture[Any]], List[RestateDurableFuture[Any]]]: |
| 24 | + """ |
| 25 | + Blocks until at least one of the futures/all of the futures are completed. |
| 26 | +
|
| 27 | + Returns a tuple of two lists: the first list contains the futures that are completed, |
| 28 | + the second list contains the futures that are not completed. |
| 29 | +
|
| 30 | + The mode parameter can be either FIRST_COMPLETED or ALL_COMPLETED. |
| 31 | + Using FIRST_COMPLETED will return as soon as one of the futures is completed. |
| 32 | + Using ALL_COMPLETED will return only when all futures are completed. |
| 33 | +
|
| 34 | + examples: |
| 35 | +
|
| 36 | + completed, waiting = await wait(f1, f2, f3, mode=FIRST_COMPLETED) |
| 37 | + for completed_future in completed: |
| 38 | + # do something with the completed future |
| 39 | + print(await completed_future) # prints the result of the future |
| 40 | +
|
| 41 | + or |
| 42 | +
|
| 43 | + completed, waiting = await wait(f1, f2, f3, mode=ALL_COMPLETED) |
| 44 | + assert waiting == [] |
| 45 | +
|
| 46 | +
|
| 47 | + """ |
| 48 | + assert mode in (FIRST_COMPLETED, ALL_COMPLETED) |
| 49 | + |
| 50 | + remaining = list(futures) |
| 51 | + while remaining: |
| 52 | + completed, waiting = await wait_completed(remaining) |
| 53 | + if mode == FIRST_COMPLETED: |
| 54 | + return completed, waiting |
| 55 | + remaining = waiting |
| 56 | + |
| 57 | + assert mode == ALL_COMPLETED |
| 58 | + return list(futures), [] |
| 59 | + |
| 60 | +async def gather(*futures: RestateDurableFuture[Any]) -> List[RestateDurableFuture[Any]]: |
| 61 | + """ |
| 62 | + Blocks until all futures are completed. |
| 63 | +
|
| 64 | + Returns a list of all futures. |
| 65 | + """ |
| 66 | + completed, _ = await wait(*futures, mode=ALL_COMPLETED) |
| 67 | + return completed |
| 68 | + |
| 69 | +async def as_completed(*futures: RestateDurableFuture[Any]) -> AsyncGenerator[RestateDurableFuture[Any]]: |
| 70 | + """ |
| 71 | + Returns an iterator that yields the futures as they are completed. |
| 72 | + |
| 73 | + example: |
| 74 | +
|
| 75 | + async for future in as_completed(f1, f2, f3): |
| 76 | + # do something with the completed future |
| 77 | + print(await future) # prints the result of the future |
| 78 | +
|
| 79 | + """ |
| 80 | + remaining = list(futures) |
| 81 | + while remaining: |
| 82 | + completed, waiting = await wait_completed(remaining) |
| 83 | + for f in completed: |
| 84 | + yield f |
| 85 | + remaining = waiting |
| 86 | + |
| 87 | +async def wait_completed(futures: List[RestateDurableFuture[Any]]) -> Tuple[List[RestateDurableFuture[Any]], List[RestateDurableFuture[Any]]]: |
| 88 | + """ |
| 89 | + Blocks until at least one of the futures is completed. |
| 90 | +
|
| 91 | + Returns a tuple of two lists: the first list contains the futures that are completed, |
| 92 | + the second list contains the futures that are not completed. |
| 93 | + """ |
| 94 | + if not futures: |
| 95 | + return [], [] |
| 96 | + handles: List[int] = [] |
| 97 | + context: ServerInvocationContext | None = None |
| 98 | + for f in futures: |
| 99 | + if not isinstance(f, ServerDurableFuture): |
| 100 | + raise TerminalError("All futures must SDK created futures.") |
| 101 | + if context is None: |
| 102 | + context = f.context |
| 103 | + elif context is not f.context: |
| 104 | + raise TerminalError("All futures must be created by the same SDK context.") |
| 105 | + if f.is_completed(): |
| 106 | + return [f], [] |
| 107 | + handles.append(f.source_notification_handle) |
| 108 | + |
| 109 | + assert context is not None |
| 110 | + await context.create_poll_or_cancel_coroutine(handles) |
| 111 | + completed = [] |
| 112 | + uncompleted = [] |
| 113 | + for index, handle in enumerate(handles): |
| 114 | + future = futures[index] |
| 115 | + if context.vm.is_completed(handle): |
| 116 | + completed.append(future) |
| 117 | + else: |
| 118 | + uncompleted.append(future) |
| 119 | + return completed, uncompleted |
| 120 | + |
| 121 | + |
0 commit comments