python程序超時處理 timeout_decorator


如下兩個例子,實現對某個函數的超時處理(其實就是加了一個裝飾器timeout):

成功例子:

  • 代碼:
 1 import time
 2 import timeout_decorator
 3 
 4 
 5 @timeout_decorator.timeout(6)
 6 def test():
 7     time.sleep(5)
 8     return 5
 9 
10 
11 aaa = test()
12 print(aaa)
  • 測試結果:

 

失敗例子:

  • 代碼:
 1 import time
 2 import timeout_decorator
 3 
 4 
 5 @timeout_decorator.timeout(2)
 6 def test():
 7     time.sleep(5)
 8     return 5
 9 
10 
11 aaa = test()
12 print(aaa)
  • 測試結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM