Skip to content

Commit

Permalink
More readable wait_until()
Browse files Browse the repository at this point in the history
  • Loading branch information
LefterisJP committed Sep 5, 2017
1 parent 90db0cc commit 2249e57
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions raiden/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,20 @@ def wait_until(func, wait_for=None, sleep_for=0.5):
sleep_for (float, integer): how much to gevent.sleep between calls
Returns:
func(): result of func, if truth value, or None"""
start = time.time()
res = func()
loop_cond = True
while loop_cond:
gevent.sleep(sleep_for)
res = func()
loop_cond = (
(True if wait_for is None else (time.time() - start < wait_for)) and
not res
)
return res or None

if res:
return res

if wait_for:
deadline = time.time() + wait_for
while not res and time.time() <= deadline:
gevent.sleep(sleep_for)
res = func()

else:
while not res:
gevent.sleep(sleep_for)
res = func()

return res

0 comments on commit 2249e57

Please sign in to comment.