Ironpython Python:在最长时间后停止执行
我有一个IronPython脚本,可以执行一些EXE。如果当前exe的运行时间超过一定的时间,我想停止它的执行。我该怎么做 当前功能:Ironpython Python:在最长时间后停止执行,ironpython,Ironpython,我有一个IronPython脚本,可以执行一些EXE。如果当前exe的运行时间超过一定的时间,我想停止它的执行。我该怎么做 当前功能: def execute(programName): t0 = time.time() #should stop the execution of "programName" #if execution exceeds limit p = os.popen(programName,"r") result = p.readli
def execute(programName):
t0 = time.time()
#should stop the execution of "programName"
#if execution exceeds limit
p = os.popen(programName,"r")
result = p.readline()
p.close()
execTime = time.time() - t0
return result, execTime
我认为您可以在子流程模块中使用Popen.poll来执行您想要的操作。os.popen在2.6中不推荐使用
我认为您可以在子流程模块中使用Popen.poll来执行您想要的操作。os.popen在2.6中不推荐使用
这段代码适用于我使用python的情况。我不确定它在IronPython上的表现如何,希望这能有所帮助
"""
Monitors a set of processes execution, and termination
"""
import subprocess
from threading import Thread
import time
class ProcessMonitor( Thread ):
def __init__(self):
Thread.__init__(self)
self.processList = {}
self.running = True
def monitor( self, pClass, timeout ):
self.processList[ pClass ] = (time.time(),0,timeout)
def stop(self):
running = false
def run(self):
print time.time() # just for debugging!!
while self.running:
# sleep for a second
time.sleep(10)
# go over all processes and update the time
for pClass in self.processList:
currentTime = time.time()
# if the process didn't terminate yet
if pClass.poll() == None:
# check if needs to be terminated!
(time_,overall,timeout)=self.processList[pClass]
overall = overall + (currentTime-time_)
print "%f seconds passed from running of process %d"%(overall,pClass.pid)
if overall > timeout:
pClass.kill()
del self.processList[ pClass]
else:
self.processList[pClass] = (currentTime,overall,timeout)
else:
del self.processList[ pClass ]
这段代码适用于我使用python的情况。我不确定它在IronPython上的表现如何,希望这能有所帮助
"""
Monitors a set of processes execution, and termination
"""
import subprocess
from threading import Thread
import time
class ProcessMonitor( Thread ):
def __init__(self):
Thread.__init__(self)
self.processList = {}
self.running = True
def monitor( self, pClass, timeout ):
self.processList[ pClass ] = (time.time(),0,timeout)
def stop(self):
running = false
def run(self):
print time.time() # just for debugging!!
while self.running:
# sleep for a second
time.sleep(10)
# go over all processes and update the time
for pClass in self.processList:
currentTime = time.time()
# if the process didn't terminate yet
if pClass.poll() == None:
# check if needs to be terminated!
(time_,overall,timeout)=self.processList[pClass]
overall = overall + (currentTime-time_)
print "%f seconds passed from running of process %d"%(overall,pClass.pid)
if overall > timeout:
pClass.kill()
del self.processList[ pClass]
else:
self.processList[pClass] = (currentTime,overall,timeout)
else:
del self.processList[ pClass ]
因为它是在IronPython中运行的,所以我假设它是在Windows上运行的,所以进程管理可能与我从unix中了解到的不同 从代码的外观来看,您要做的是:读取进程的标准输出,如果运行时间过长,则将其杀死。要正确地执行此操作,您需要对标准输出进行非阻塞读取、从中读取进程输出的临时文件或两个执行线程 一个线程将读取子进程输出 另一个线程将定期轮询进程以检查它是否终止,如果它在一段时间后没有终止,则杀死它。如果不想轮询,则需要另外一个线程:一个线程等待子进程,另一个线程在一段时间后将其终止 相关标准库模块: :用这个代替popen,它在各个方面都更好。 :在单个线程中实现定期轮询和终止的基本计划程序。 :高级线程库。尤其是对于构建无轮询解决方案至关重要。
因为它是在IronPython中运行的,所以我假设它是在Windows上运行的,所以进程管理可能与我从unix中了解到的不同 从代码的外观来看,您要做的是:读取进程的标准输出,如果运行时间过长,则将其杀死。要正确地执行此操作,您需要对标准输出进行非阻塞读取、从中读取进程输出的临时文件或两个执行线程 一个线程将读取子进程输出 另一个线程将定期轮询进程以检查它是否终止,如果它在一段时间后没有终止,则杀死它。如果不想轮询,则需要另外一个线程:一个线程等待子进程,另一个线程在一段时间后将其终止 相关标准库模块: :用这个代替popen,它在各个方面都更好。 :在单个线程中实现定期轮询和终止的基本计划程序。 :高级线程库。尤其是对于构建无轮询解决方案至关重要。
如果您不需要与CPython兼容的代码,听起来不像是这样,那么您可以使用来进行流程管理,比如System.Diagnostics.process类。它可能比使用os.popen要简单一些。如果您不需要与CPython兼容的代码,听起来不像是这样,那么您可以使用来进行流程管理,比如System.Diagnostics.process类。这可能比使用os.popen要简单一些。下面是我的操作方法。。。它确实使用子流程,所以您需要调整它以使用popen
class KillerThread(threading.Thread):
def __init__(self, pid, timeout, event ):
threading.Thread.__init__(self)
self.pid = pid
self.timeout = timeout
self.event = event
self.setDaemon(True)
def run(self):
self.event.wait(self.timeout)
if not self.event.isSet() :
try:
os.kill( self.pid, signal.SIGKILL )
except OSError, e:
#This is raised if the process has already completed
pass
def runTimed(dt, args, kwargs ):
event = threading.Event()
proc = subprocess.Popen(args, **kwargs )
killer = KillerThread(proc.pid, dt, event)
killer.start()
(stdout, stderr) = proc.communicate()
event.set()
return (stdout,stderr, proc.returncode)
#EXAMPLE USAGE - lets it run for at most 3 seconds
(stdout, stderr, returncode) = \
runTimed(3, \
["path/to/my/executable", "arg1" ] , \
{'stdout':subprocess.PIPE, 'stderr':subprocess.PIPE} )
如果使用stderr、stdout,您可能不需要额外的线程
直接使用而不是使用通信,您只需要小心在这些句柄上使用非阻塞IO。以下是我的做法。。。它确实使用子流程,所以您需要调整它以使用popen
class KillerThread(threading.Thread):
def __init__(self, pid, timeout, event ):
threading.Thread.__init__(self)
self.pid = pid
self.timeout = timeout
self.event = event
self.setDaemon(True)
def run(self):
self.event.wait(self.timeout)
if not self.event.isSet() :
try:
os.kill( self.pid, signal.SIGKILL )
except OSError, e:
#This is raised if the process has already completed
pass
def runTimed(dt, args, kwargs ):
event = threading.Event()
proc = subprocess.Popen(args, **kwargs )
killer = KillerThread(proc.pid, dt, event)
killer.start()
(stdout, stderr) = proc.communicate()
event.set()
return (stdout,stderr, proc.returncode)
#EXAMPLE USAGE - lets it run for at most 3 seconds
(stdout, stderr, returncode) = \
runTimed(3, \
["path/to/my/executable", "arg1" ] , \
{'stdout':subprocess.PIPE, 'stderr':subprocess.PIPE} )
如果使用stderr、stdout,您可能不需要额外的线程
直接使用而不是使用通信,您只需要小心在这些句柄上使用非阻塞IO。IronPythonsUpProcess不受IronPythonsUpProcess支持IronPythonsUpProcess不受IronPython支持。将尝试修改您的代码,看看是否可以解决我的问题IronPython不支持Subprocess。将尝试修改您的代码,看看是否可以解决我的问题