0

就像标题所说的那样,我正在尝试运行一些execfile调用,但我正在寻找一个超时选项,这样如果我的被调用脚本需要超过 10 秒才能运行,那么它将终止被调用脚本并继续......

信号库/包仅适用于 UNIX,我在 Windows 上,所以我有点卡住了。

# Sequential wait to finish before moving onto the next script
try: 
    execfile("SUBSCRIPTS/TESTSCRIPT.py", {})
except Exception:
    errors.write(traceback.format_exc() + '\n')
    errors.write("\n\n")

# Semi-Sequential (Don't wait for it to finish before moving onto the third script)
subprocess.Popen(["pythonw", "SUBSCRIPTS/TEST.py", "0"], shell=True)

# Sequential wait to finish before moving onto the next script
try: 
    execfile("SUBSCRIPTS/TEST.py", {})
except Exception:
    errors.write(traceback.format_exc() + '\n')
    errors.write("\n\n")

# Sequential wait to finish before moving onto the next script
try: 
    execfile("SUBSCRIPTS/TESTSCRIPT.py", {})
except Exception:
    errors.write(traceback.format_exc() + '\n')
    errors.write("\n\n")

有任何想法吗?

4

1 回答 1

4

像这样的东西应该工作:

# Sequential wait to finish before moving onto the next script
try: 
    execfile("SUBSCRIPTS/TESTSCRIPT.py", {})
except Exception:
    errors.write(traceback.format_exc() + '\n')
    errors.write("\n\n")

# Semi-Sequential (Don't wait for it to finish before moving onto the third script)
p1 = subprocess.Popen(["pythonw", "SUBSCRIPTS/TEST.py", "0"], shell=True)

# Sequential wait to finish before moving onto the next script
try: 
    execfile("SUBSCRIPTS/TEST.py", {})
except Exception:
    errors.write(traceback.format_exc() + '\n')
    errors.write("\n\n")

#Do you want to kill the "pythonw", "SUBSCRIPTS/TEST.py", "0" command after the "SUBSCRIPTS/TEST.py" call or do you want to allow the pythonw command to continue running until after the "SUBSCRIPTS/TESTSCRIPT.py"

#you need to put this code depending on where the subprocess.Popen(["pythonw", "SUBSCRIPTS/TEST.py", "0"], shell=True) #script needs to be killed
currentStatus = p1.poll()
if currentStatus is None: #then it is still running
  try:
    p1.kill() #maybe try os.kill(p1.pid,2) if p1.kill does not work
  except:
    #do something else if process is done running - maybe do nothing?
    pass

# Sequential wait to finish before moving onto the next script
try: 
    execfile("SUBSCRIPTS/TESTSCRIPT.py", {})
except Exception:
    errors.write(traceback.format_exc() + '\n')
    errors.write("\n\n")

#or put the code snippet here if you want to allow the pythonw command to continue running until after the SUBSCRIPTS/TESTSCRIPT.py command

s

于 2013-10-29T12:14:14.397 回答