-
Notifications
You must be signed in to change notification settings - Fork 104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
retry(seconds=...)
broke
#368
Comments
…d name Per tmux-python/libtmux#368, retry(seconds=...) is broke.
…d name Per tmux-python/libtmux#368, retry(seconds=...) is broke.
It turns out that this is the cause of tmux-python/tmuxp#704 (comment) . The current implementation of the Why is this function necessary? Maybe it can be replaced by a |
Ok, I just found a case where a piece of code was tried 89 times before it succeeded, which was about 300 ms. |
I came up with this function: def retry(fun, secs=1):
ini = time.time()
while not fun():
end = time.time()
if end - ini >= secs:
raise Exception('retry timed out') which does the retrying thing accurately with the timeout, it is more or less clean to use (little change to the code) and reports when the retrying timed out: def f():
session.server._update_windows()
return w.name == "sh"
retry(f)
assert w.name == "sh" what do you think? Of course it can read the timeout from the environment just like the code does right now |
@categulario I'm still working and won't be able to focus on this in earnest until the weekend, but here's what i have to offer (to make it easier, maybe): class WaitTimeout(Exception):
pass
class WaitConditionCallbackProtocol(typing.Protocol):
def __call__(self: Generic) -> bool:
...
def wait_for_condition(
self,
callback: WaitConditionCallbackProtocol,
wait_time=0.5,
interval=0.25,
raises=False,
):
if not callable(callback):
raise TypeError("callback should be callable")
time_spent = 0
condition = False
while condition:
self.server.update_panes()
if time_spent >= wait_time:
if raises:
raise WaitTimeout(f"Condition timed out after {time_spent}")
break
condition = callback(self)
if condition is True:
return True
time.sleep(interval)
time_spent += interval
return False
class WaitableMixin:
wait_for_condition = wait_for_condition
# In window.py (example):
class Window(..., WaitableMixin):
...
# In pane.py (example):
class Pane(..., WaitableMixin):
... Usage:
|
For an instant I thought this issue was about the I understand that its usage can be extended to
Things that I like:
|
@categulario Would you like to make a new PR with a wait function, using the if not i can get around to it in this weekend or next weekend i may not be around in the weekdays as much due to normal work deadlines |
Sorry, I didn't understand that. Got confused between "with a wait function" and "without the time function" |
@categulario I edited it. I'm just asking if you want to make PR with an example of what you meant here |
Just updated #372 with that |
Fixes #368, as retry() in its current form is broke and won't work within a `with` block. The function is deprecated and will be removed in 0.13.x. 0.12.x will warn when using it. retry_until() is now available and will either raise or return False if raise=False is passed. See also: - tmux-python/tmuxp#620 - tmux-python/tmuxp#704 (comment)
…d name Per tmux-python/libtmux#368, retry(seconds=...) is broke.
Instead
Find cross platform way to do timeout with signals
See also tmux-python/tmuxp#620
The text was updated successfully, but these errors were encountered: