python 函数超时调用

2018-02-07  本文已影响0人  橙姜

import time
import signal

def test():
while(True):
print "111111"
time.sleep(1)

def exec_timeout(func, timeout):
def handler(signum, frame):
raise AssertionError
try:
signal.signal(signal.SIGALRM, handler)
signal.alarm(timeout)
test()

signal.alarm(0)

except AssertionError:
    print "timeout"

if name == "main":
exec_timeout(test, 5)

上一篇下一篇

猜你喜欢

热点阅读