[PEAK] Anybody using reactors?
Phillip J. Eby
pje at telecommunity.com
Wed Jan 14 12:19:26 EST 2004
At 06:58 PM 1/14/04 +0200, alexander smishlajev wrote:
>Phillip J. Eby wrote, at 14.01.2004 18:35:
>
>>>* ability to remove sheduled procedures from the laters list
>>I'm curious how this use case would be handled with 'peak.events'. I
>>haven't yet felt a strong need for it myself, obviously, else I'd have
>>added it to UntwistedReactor. Do you have a sample usage or kinds of
>>usage that I could see, so I could see how I'd write it with peak.events,
>>and make sure it's still tractable?
>
>we use it to handle timeouts: timeout procedure is registered with
>callLater(), and we continue with normal data flow. when expected event
>arrives, timeout procedure (if any) is removed from the queue, with
>possible resheduling at a later time.
Ah. Okay, so something like:
while True:
yield events.AnyOf(data.readable, scheduler.timeout(30));
src,evt=events.resume()
if src is data.readable:
# got something, act on it
else:
# timed out
would work, then? Yielding on an 'AnyOf()' automatically discards
callbacks received after the first one it receives from any of its
inputs. So, the above is a loop that waits for the 'data.readable'
condition with a 30-second timeout each time. It could also be written:
untilReadableOrTimeout = events.AnyOf(data.readable, scheduler.sleep(30))
while True:
yield untilReadableOrTimeout; src,evt=events.resume()
if src is data.readable:
# got something, act on it
else:
# timed out
('events.IScheduler.sleep()' returns a relative delay object that is reset
each time it's used, while timeout() returns a fixed deadline. So, a
sleep() object is reusable, while a timeout() would have to be created each
time through the loop unless a fixed deadline were desired.)
More information about the PEAK
mailing list