Is there an easy way in Python to wait until certain condition is true?

后端 未结 8 1342

I need to wait in a script until a certain number of conditions become true?

I know I can roll my own eventing using condition variables and friends, but I don\'t wa

8条回答
  •  死守一世寂寞
    2020-12-23 21:03

    Proposed solution:

    def wait_until(delegate, timeout: int):
    end = time.time() + timeout
    while time.time() < end:
        if delegate():
            return True
        else:
            time.sleep(0.1)
    return False
    

    Usage:

    wait_until(lambda: True, 2)
    

提交回复
热议问题