def profile_signal_handler(signum, frame): if state.profile_level > 0: state.accumulate_time(clock()) sample_stack_procs(frame) itimer.setitimer(itimer.ITIMER_PROF, state.sample_interval, 0.0) state.last_start_time = clock()
def start(): state.profile_level += 1 if state.profile_level == 1: state.last_start_time = clock() rpt = state.remaining_prof_time state.remaining_prof_time = None signal.signal(signal.SIGPROF, profile_signal_handler) itimer.setitimer(itimer.ITIMER_PROF, rpt or state.sample_interval, 0.0) state.gc_time_taken = 0 # dunno
def preemptive_disable(): global the_preemptive_rate if not itimer: return the_preemptive_rate = None itimer.setitimer(itimer.ITIMER_PROF, 0, 0) signal.signal(signal.SIGPROF, signal.SIG_IGN)
def preemptive_enable(frequency=50): global the_preemptive_rate if not itimer: raise RuntimeError("the 'itimer' extension is required for preempting") if the_preemptive_rate is None: signal.signal(signal.SIGPROF, preemptive_signal_handler) the_preemptive_rate = 1.0 / frequency itimer.setitimer(itimer.ITIMER_PROF, the_preemptive_rate, the_preemptive_rate)
def stop(): state.profile_level -= 1 if state.profile_level == 0: state.accumulate_time(clock()) state.last_start_time = None rpt = itimer.setitimer(itimer.ITIMER_PROF, 0.0, 0.0) signal.signal(signal.SIGPROF, signal.SIG_IGN) state.remaining_prof_time = rpt[0] state.gc_time_taken = 0 # dunno
except Exception, e: log.traceback() def preemptive_enable(frequency = 50): global the_preemptive_rate if not itimer: raise RuntimeError("the 'itimer' extension is required for preempting") if the_preemptive_rate is None: signal.signal(signal.SIGPROF, preemptive_signal_handler) the_preemptive_rate = 1.0/frequency itimer.setitimer( itimer.ITIMER_PROF, the_preemptive_rate, the_preemptive_rate) def preemptive_disable(): global the_preemptive_rate if not itimer: return the_preemptive_rate = None itimer.setitimer(itimer.ITIMER_PROF, 0, 0) signal.signal(signal.SIGPROF, signal.SIG_IGN) class event_list(object):
def itimer(seconds): py_itimer.setitimer(0, seconds, 0)
def catcher(n, f): global s print "Alarmed in %.4f" % (time.time() - s) s = time.time() if __name__ == "__main__": signal.signal(signal.SIGALRM, catcher) print "Setting alarm for 0.3 seconds" s = time.time() itimer.alarm(0.3) time.sleep(1) print "Setting alarm for 1.1 seconds" s = time.time() itimer.alarm(1.1) time.sleep(2) print "Setting alarm for 5.5 seconds" itimer.alarm(5.5) time.sleep(2.3) print "Afeter sleeping 2.3 there was %.2f left till alarm" % itimer.alarm(0) print "Setting alarm for 0.3 seconds with repeat every 0.5 seconds" itimer.setitimer(itimer.ITIMER_REAL, 0.3, 0.5) time.sleep(1) time.sleep(1) print "Next alarm in %.2f seconds, repeat every %.2f seconds" % itimer.getitimer(itimer.ITIMER_REAL) time.sleep(1) print "At cancel, alarm in %.2f seconds, repeating in %.2f seconds" % itimer.setitimer(itimer.ITIMER_REAL, 0) print "Next alarm in %.2f seconds, repeat every %.2f seconds" % itimer.getitimer(itimer.ITIMER_REAL)