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

后端 未结 8 1327

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 20:58

    Here is the threading extention to Alex's solution:

    import time
    import threading
    
    # based on https://stackoverflow.com/a/2785908/1056345                                                                                                                                                                                                                                                                         
    def wait_until(somepredicate, timeout, period=0.25, *args, **kwargs):
        must_end = time.time() + timeout
        while time.time() < must_end:
            if somepredicate(*args, **kwargs):
                return True
            time.sleep(period)
        return False
    
    def wait_until_par(*args, **kwargs):
        t = threading.Thread(target=wait_until, args=args, kwargs=kwargs)
        t.start()
        print ('wait_until_par exits, thread runs in background')
    
    def test():
        print('test')
    
    wait_until_par(test, 5)
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-23 21:07

    Unfortunately the only possibility to meet your constraints is to periodically poll, e.g....:

    import time
    
    def wait_until(somepredicate, timeout, period=0.25, *args, **kwargs):
      mustend = time.time() + timeout
      while time.time() < mustend:
        if somepredicate(*args, **kwargs): return True
        time.sleep(period)
      return False
    

    or the like. This can be optimized in several ways if somepredicate can be decomposed (e.g. if it's known to be an and of several clauses, especially if some of the clauses are in turn subject to optimization by being detectable via threading.Events or whatever, etc, etc), but in the general terms you ask for, this inefficient approach is the only way out.

    0 讨论(0)
  • 2020-12-23 21:11

    Another nice package is waiting - https://pypi.org/project/waiting/

    install:

    pip install waiting
    

    Usage: You pass a function that will be called every time as a condition, a timeout, and (this is useful) you can pass a description for the waiting, which will be displayed if you get TimeoutError.

    using function:

    from waiting import wait
    
    
    def is_something_ready(something):
        if something.ready():
            return True
        return False
    
    
    # wait for something to be ready
    something = # whatever
    
    wait(lambda: is_something_ready(something), timeout=120, waiting_for="something to be ready")
    
    # this code will only execute after "something" is ready
    print("Done")
    
    

    Note: the function must return a boolean - True when the wait is over, False otherwise

    0 讨论(0)
  • 2020-12-23 21:11

    You've basically answered your own question: no.

    Since you're dealing with external libraries in boost.python, which may change objects at their leisure, you need to either have those routines call an event handler refresh, or work with a condition.

    0 讨论(0)
  • 2020-12-23 21:12

    This worked for me

    direction = ''
        t = 0
        while direction == '' and t <= 1:
            sleep(0.1)
            t += 0.1
    

    This is for waiting for a signal while making sure time limit of 1 second

    0 讨论(0)
提交回复
热议问题