blob: 0adbe9ef9ca12cd351afe40d2e5b5be1c7a69f11 [file] [log] [blame]
# Copyright (C) 2018 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import time
def exec_fn_with_timeout(func, limit=60):
"""Helper function that executes a given function until it returns True or a
given time limit is reached.
Arguments:
func {function} -- Function to execute. The function can return some output
(or None) and as a second return value a boolean indicating,
whether the event the function was waiting for has happened.
Keyword Arguments:
limit {int} -- Maximum time in seconds to wait for a positive response of
the function (default: {60})
Returns:
boolean -- False, if the timeout was reached
any -- Last output of fn
"""
timeout = time.time() + limit
while time.time() < timeout:
output, is_finished = func()
if is_finished:
return True, output
return False, output