def timeout_handler(nevents, maxevents): 'Recurring timeout, in loop.call_later it rescheduls itself' if not cycle.running or (nevents and ievent[timer] >= maxevents): loop.stop() else: handler(timer) loop.call_later(cycle.period, timeout_handler, nevents, maxevents)
def run(nevents=0): """ Run the Piety event loop. nevents: number of timer events to process, then exit run loop. If nevents not 0, runs even if running==False Use default nevents=0 to process until running==False or unhandled exception """ start() maxevents = ievent[timer] + nevents # ievent includes previous calls to run() interval = cycle.period # timeout interval in seconds while cycle.running or (nevents and ievent[timer] < maxevents): # Python select doesn't assign time remaining to timeout argument # so we have to time it ourselves t0 = datetime.datetime.now() inputready, outputready, exceptready = select.select(inputs, outputs, exceptions, interval) # inputs for fd in inputready: handler(fd) interval = adjust_interval(t0, interval) # periodic timeout if no input if not (inputready or outputready or exceptready): handler(timer) interval = cycle.period # if we got here, full interval has elapsed
def activate(t): """ Activate task t by registering t.input with loop and add to ievent counter. Here we assume piety has already added task t to schedule. """ if t.input != timer: loop.add_reader(t.input, (lambda: handler(t.input)))