python - Terminating subprocess in python2.7 -
i working on windows 8 x64 system
i writing code in using subprocess execute 1 lengthy program while execution fetch output of program , analyse runtime, particular criteria
i want terminate process criteria met, can't done in program want terminate subprocess python
my code looks
class mythread (threading.thread): def __init__(self, threadi.... ........ ....... def run(self): self.process= subprocess.popen(cmd,stdout=subprocess.pipe,stderr=subprocess.stdout) line in iter(p.stdout.readline, b''): ......................... ......run-time analysis........ ......................... if (criteria_met): self.stop() ......................... ........................ def stop(self): if self.process not none: self.process.terminate() self.process = none
but error
exception in thread thread-1: traceback (most recent call last): file "c:\python27\lib\threading.py", line 551, in __bootstrap_inner self.run() file "vb_extraction.py", line 57, in run self.process.kill() file "c:\python27\lib\subprocess.py", line 1019, in terminate _subprocess.terminateprocess(self._handle, 1) windowserror: [error 5] access denied
what cause of issue? how can terminate process before completing?
call .poll()
check whether process still running:
if self.process not none , self.process.poll() none: # still running self.process.terminate() # or .kill() if doesn't terminate self.process.communicate() # close pipes, wait completion self.process = none
Comments
Post a Comment