Skip to content

cond_waiter

Timeout-aware condition variable waiting.

This module provides CondWaiter, a small object holding a timeout and a time source, used to wait on condition variables with a consistent remaining-time semantics across multiple waits - which is needed by the components supporting custom TimeGen objects (like the executors).

CondWaiter

A condition variable waiter holding a timeout and a time source.

Each wait() call consumes the remaining time held by the object, so that a sequence of waits honors a single overall timeout.

Example
import threading

waiter = cond_waiter.CondWaiter(timeout=10.0)
cond = threading.Condition()
with cond:
  notified = waiter.wait(cond)  # notified within the 10s budget?
Source code in py_misc_utils/cond_waiter.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class CondWaiter:
  """A condition variable waiter holding a timeout and a time source.

  Each wait() call consumes the remaining time held by the object, so
  that a sequence of waits honors a single overall timeout.

  Example:
    import threading

    waiter = cond_waiter.CondWaiter(timeout=10.0)
    cond = threading.Condition()
    with cond:
      notified = waiter.wait(cond)  # notified within the 10s budget?
  """

  def __init__(self, timeout=None, timegen=None):
    """Creates a condition waiter.

    Args:
      timeout: An optional maximum overall number of seconds to wait. If
        None, the waits never expire.
      timegen: An optional TimeGen object used to evaluate the timeout -
        and to wait on the condition variables. If None, the default
        (system) time source is used.
    """
    self._timegen = tg.TimeGen() if timegen is None else timegen
    self._timeo = abst.AbsTimeout(timeout, timefn=self._timegen.now)

  def wait(self, cond):
    """Waits on a condition variable until notified - or timeout expiry.

    Args:
      cond: The condition variable to wait on. The caller must hold its
        lock.

    Returns:
      True if the condition variable has been notified, False if the
      remaining wait time expired.
    """
    return self._timegen.wait(cond, timeout=self._timeo.get())

wait(cond)

Waits on a condition variable until notified - or timeout expiry.

Parameters:

Name Type Description Default
cond

The condition variable to wait on. The caller must hold its lock.

required

Returns:

Type Description

True if the condition variable has been notified, False if the

remaining wait time expired.

Source code in py_misc_utils/cond_waiter.py
40
41
42
43
44
45
46
47
48
49
50
51
def wait(self, cond):
  """Waits on a condition variable until notified - or timeout expiry.

  Args:
    cond: The condition variable to wait on. The caller must hold its
      lock.

  Returns:
    True if the condition variable has been notified, False if the
    remaining wait time expired.
  """
  return self._timegen.wait(cond, timeout=self._timeo.get())