/usr/lib/python2.7/dist-packages/traits/util/async_trait_wait.py is in python-traits 4.6.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 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 52 53 54 | import threading
def wait_for_condition(condition, obj, trait, timeout=None):
"""
Wait until the given condition is true, re-evaluating on trait change.
This is intended for use in multithreading situations where traits can be
modified from a different thread than the calling thread.
Wait until `condition` is satisfied. Raise a RuntimeError if
`condition` is not satisfied within the given timeout.
`condition` is a callback function that will be called with `obj`
as its single argument. It should return a boolean indicating
whether the condition is satisfied or not.
`timeout` gives the maximum time in seconds to wait for the
condition to become true. The default value of `None` indicates
no timeout.
(obj, trait) give an object and trait to listen to for indication
of a possible change: whenever the trait changes, the condition is
re-evaluated. The condition will also be evaluated on entering
this function.
Note that in cases of unusual timing it's possible for the condition to be
evaluated one more time *after* the ``wait_for_condition`` call has
returned.
"""
condition_satisfied = threading.Event()
def handler():
if condition(obj):
condition_satisfied.set()
obj.on_trait_change(handler, trait)
try:
if condition(obj):
# Catch case where the condition was satisfied before
# the on_trait_change handler was active.
pass
elif timeout is None:
# Allow a Ctrl-C to interrupt. The 0.05 value matches
# what's used by the standard library's Condition.wait.
while not condition_satisfied.is_set():
condition_satisfied.wait(timeout=0.05)
else:
condition_satisfied.wait(timeout=timeout)
if not condition_satisfied.is_set():
raise RuntimeError("Timed out waiting for condition.")
finally:
obj.on_trait_change(handler, trait, remove=True)
|