Skip to content

poll

dev_tool.tools.poll

poll_until

A function that polls a predicate until it returns True or times out.

Parameters:

  • predicate (Callable[[], bool]) –

    A callable that returns True when the condition is met.

  • timeout (int, default: 30 ) –

    Maximum number of iterations to attempt.

  • interval (int, default: 1 ) –

    The delay in seconds between polling attempts.

  • delay (int, default: 1 ) –

    The delay in seconds after a successful predicate check.

Returns:

  • bool

    True if the predicate succeeded, False if timeout.

Source code in dev_tool/tools/poll.py
def poll_until(
    predicate: Callable[[], bool],
    timeout: int = 30,
    interval: int = 1,
    delay: int = 1
) -> bool:
    """
    A function that polls a predicate until it returns True or times out.

    :param predicate: A callable that returns True when the condition is met.
    :param timeout: Maximum number of iterations to attempt.
    :param interval: The delay in seconds between polling attempts.
    :param delay: The delay in seconds after a successful predicate check.
    :return: True if the predicate succeeded, False if timeout.
    """

    for _ in range(timeout):
        if predicate():
            if delay:
                time.sleep(delay)

            return True

        time.sleep(interval)

    return False