Wait until a condition is met or time out with an exception. Args: condition_predictor: A function that predicts the condition. timeout: Maximum timeout in seconds. retry_interval_ms: Retry interval in milliseconds. raise_exceptions: If true, exceptions that occu
(
condition_predictor: Callable[..., bool],
timeout: float = 10,
retry_interval_ms: float = 100,
raise_exceptions: bool = False,
**kwargs: Any,
)
| 93 | |
| 94 | |
| 95 | def wait_for_condition( |
| 96 | condition_predictor: Callable[..., bool], |
| 97 | timeout: float = 10, |
| 98 | retry_interval_ms: float = 100, |
| 99 | raise_exceptions: bool = False, |
| 100 | **kwargs: Any, |
| 101 | ): |
| 102 | """Wait until a condition is met or time out with an exception. |
| 103 | |
| 104 | Args: |
| 105 | condition_predictor: A function that predicts the condition. |
| 106 | timeout: Maximum timeout in seconds. |
| 107 | retry_interval_ms: Retry interval in milliseconds. |
| 108 | raise_exceptions: If true, exceptions that occur while executing |
| 109 | condition_predictor won't be caught and instead will be raised. |
| 110 | **kwargs: Arguments to pass to the condition_predictor. |
| 111 | |
| 112 | Returns: |
| 113 | None: Returns when the condition is met. |
| 114 | |
| 115 | Raises: |
| 116 | RuntimeError: If the condition is not met before the timeout expires. |
| 117 | """ |
| 118 | start = time.monotonic() |
| 119 | last_ex = None |
| 120 | while time.monotonic() - start <= timeout: |
| 121 | try: |
| 122 | if condition_predictor(**kwargs): |
| 123 | return |
| 124 | except Exception: |
| 125 | if raise_exceptions: |
| 126 | raise |
| 127 | last_ex = ray._private.utils.format_error_message(traceback.format_exc()) |
| 128 | time.sleep(retry_interval_ms / 1000.0) |
| 129 | message = "The condition wasn't met before the timeout expired." |
| 130 | if last_ex is not None: |
| 131 | message += f" Last exception: {last_ex}" |
| 132 | raise RuntimeError(message) |
| 133 | |
| 134 | |
| 135 | async def async_wait_for_condition( |
no test coverage detected
searching dependent graphs…