终止Python脚本中没有响应的exe文件

2024-09-30 00:38:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python中运行一种touchy.exe文件来接收两个数据测量值。文件应该打开,测量,然后关闭。问题是有时它会崩溃,我需要能够在很长一段时间内每10分钟测量一次。在

我需要的是一个'检查',看看是否.exe没有响应,如果没有,然后让它杀死进程。或者在每次测量后就把整个脚本都干掉。问题是脚本在尝试运行没有响应的.exe文件时卡住了。在

脚本如下:

FNULL = open(os.devnull, 'a')   

filename = "current_pressure.log"

command = '"*SRH#\r"'

args = "httpget -r -o " + filename  + " -C 2 -S " + command + IP 

subprocess.call(args, stdout=FNULL, stderr=FNULL, shell=False)

基本上,需要类似于:

^{pr2}$

或者

"kill above script if running after longer than 20 seconds"

Tags: 文件数据脚本进程osargsopencurrent
2条回答

如果进程运行太长,请使用计时器终止进程。这里我有两个计时器来完成一个优雅而艰难的终止,但是如果你想的话,你可以杀了他。在

import threading

FNULL = open(os.devnull, 'a')   
filename = "current_pressure.log"
command = '"*SRH#\r"'
args = "httpget -r -o " + filename  + " -C 2 -S " + command + IP 

proc = subprocess.Popen(args, stdout=FNULL, stderr=FNULL, shell=False)

nice = threading.Timer(20, proc.terminate)
nice.start()
mean = threading.Timer(22, proc.kill)
mean.start()
proc.wait()
nice.cancel()
mean.cancel()

通常,当一个程序在windows上运行时挂起,我们会尝试转到任务管理器并结束该程序的进程。当这种方法失败时,我们用一些第三方软件来终止它。然而,还有另一种更好的方法可以自动终止这种挂起的程序

http://www.problogbooster.com/2010/01/automatically-kills-non-responding.html

相关问题 更多 >

    热门问题