Python 官方文档:入门教程 => 点击学习
背景: 最近写的python代码不知为何,总是执行到一半卡住不动,为了使程序能够继续运行,设置了函数调用超时机制。 代码: import time import signal def test(i): tim
import time
import signal
def test(i):
time.sleep(i % 4)
print "%d within time" % (i)
return i
if __name__ == '__main__':
def handler(signum, frame):
raise AssertionError
for i in range(1, 10):
try:
signal.signal(signal.SIGALRM, handler)
signal.alarm(3)
test(i)
except AssertionError:
print "%d timeout" % (i)
finally:
signal.alarm(0)
signal.signal(signal.SIGALRM, signal.SIG_DFL)
1 within time2 within time3 timeout4 within time5 within time6 within time7 timeout8 within time9 within time
--结束END--
本文标题: Python设置函数调用超时,进程卡住
本文链接: https://www.lsjlt.com/news/186570.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0