I can query for that with event.is_set(), but I can't determine is timeout expired or has event been fired.
def wait_for_event(event, timeout=None):
"""Block until the internal flag is true.
If the internal flag is true on entry, return immediately. Otherwise,
block until another thread calls :meth:`set` to set the flag to true,
or until the optional timeout occurs.
When the *timeout* argument is present and not ``None``, it should be a
floating point number specifying a timeout for the operation in seconds
(or fractions thereof).
Return True if event fired, False on timeout
"""
if event._flag:
return event._flag
else:
switch = getcurrent().switch
event.rawlink(switch)
try:
timer = Timeout.start_new(timeout)
try:
try:
result = event.hub.switch()
assert result is event, 'Invalid switch into Event.wait(): %r' % (result, )
except Timeout:
ex = sys.exc_info()[1]
if ex is not timer:
raise
return False
finally:
timer.cancel()
finally:
event.unlink(switch)
return True
class Test(unittest.TestCase):
def event_fire_test(self):
# event object
event = Event()
def provider():
for x in range(10):
# print now(), 'signal event'
event.set()
event.clear()
sleep(0)
def consumer():
for x in range(10):
status = wait_for_event(event, timeout=0.1)
self.assertTrue(status)
# print now(), 'got event:', status
status = wait_for_event(event, timeout=1.0)
self.assertFalse(status)
for x in [spawn(provider), spawn(consumer)]:
x.join()
if __name__ == '__main__':
unittest.main()
--
Best Regards,
Alex