http://blog.sina.com.cn/s/blog_63041bb80102uy5o.html
背景:
代码:
import timeimport signal
def test(i):time.sleep(i%4) print "%d within time"%(i) return i
if __name__ == '__main__':def handler(signum, frame): raise AssertionError
i = 0 for i in range(1,10): try: signal.signal(signal.SIGALRM, handler) signal.alarm(3) test(i) i = i + 1 signal.alarm(0) except AssertionError: print "%d timeout"%(i)
说明:
程序执行结果:
1 within time2 within time3 timeout4 within time5 within time6 within time7 timeout8 within time9 within time