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

本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
安裝timeout-decorator庫(kù):
pip3 install timeout-decorator
編寫(xiě)異常語(yǔ)句:
@timeout_decorator.timeout(5, timeout_exception=StopIteration)
函數(shù)限制超時(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框架》
這是非常使用的案例技巧功能,很適合大家在項(xiàng)目編寫(xiě)時(shí)候,解決遇到的問(wèn)題,大家可以參考學(xué)習(xí),但是面對(duì)不同的場(chǎng)景,還是要分情況使用哦~
本文題目:創(chuàng)新互聯(lián)Python教程:python函數(shù)超時(shí)怎么自動(dòng)退出?
文章鏈接:http://fisionsoft.com.cn/article/djcedoi.html


咨詢
建站咨詢
