python 超过一定时间,自动重新运行函数
代码:
1 # -*- coding:utf-8 -*- 2 import re 3 # import Util.NEO4J.neo4j_util as Neo 4 import sys 5 import os 6 import time 7 sentence="" 8 num_zhang=[] 9 10 11 def restart_program(): 12 python = sys.executable 13 os.execl(python, python, * sys.argv) 14 15 def Get(): 16 17 for i in range(200000): 18 print(i) 19 20 21 if __name__ =="__main__": 22 # cmd='python D:\\b.py'# 必须转义'\' 23 # os.system(cmd) 24 time_start = time.time() 25 Get() 26 time_end = time.time() 27 time_sub= time_end - time_start 28 i=0 29 print("NJNNNNNNNNNNNNNNNNNNNNNNNN") 30 if(time_sub>1): 31 str = "CHCP 65001" + "&&" 32 str += "E:" + "&&" 33 str += r"cd E:\pycharm\WorkPlace\Graduation_project\Util" + "&&" 34 str += "python test.py " 35 # print(str) 36 print("TIME______",i,"________",time_sub) 37 i=i+1 38 cmd = os.system(str) 39 # print(cmd)
———————————————————————————————————————————————————————
更新:
简约版:
1 def restart(time_start,time_end,sub_deadline): 2 time_sub = time_end - time_start 3 i = 0 4 print("NJNNNNNNNNNNNNNNNNNNNNNNNN") 5 if (time_sub > sub_deadline): 6 str = "CHCP 65001" + "&&" 7 str += "E:" + "&&" 8 str += r"cd E:\pycharm\WorkPlace\Graduation_project\Util" + "&&" 9 str += "python test.py " 10 # print(str) 11 print("TIME______", i, "________", time_sub) 12 i = i + 1 13 cmd = os.system(str) 14 # print(cmd)