新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python函數(shù)超時怎么自動退出?

本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
安裝timeout-decorator庫:
pip3 install timeout-decorator
編寫異常語句:
@timeout_decorator.timeout(5, timeout_exception=StopIteration)
函數(shù)限制超時:
@timeout_decorator.timeout(5, use_signals=False)
解決案例:
import timeout_decorator
@timeout_decorator.timeout(5)
def mytest():
print("Start")
for i in range(1, 10):
time.sleep(1)
print("{} seconds have passed".format(i))
def main():
mytest()
if __name__ == '__main__':
main()
《相關(guān)推薦:php框架》
這是非常使用的案例技巧功能,很適合大家在項目編寫時候,解決遇到的問題,大家可以參考學習,但是面對不同的場景,還是要分情況使用哦~
分享題目:創(chuàng)新互聯(lián)Python教程:python函數(shù)超時怎么自動退出?
文章來源:http://www.dlmjj.cn/article/djcedoi.html


咨詢
建站咨詢
