def run(self): """ Start the process and hook up the callbacks. """ env = os.environ env['TERM'] = 'xterm' process = Popen(self.command_string + ' 2>&1', shell=True, env=env, universal_newlines=True, stdout=PIPE, stdin=PIPE, ) self.process = process while True: out_char = process.stdout.read(1) if out_char == '': if process.poll() is not None: # The process has finished break else: # The process is not giving any interesting # output. No use polling it immediatly. sleep(0.1) else: self.out_callback(out_char) if self.end_callback is not None: self.end_callback()
def run(self): """ Start the process and hook up the callbacks. """ env = os.environ env['TERM'] = 'xterm' process = Popen( self.command_string + ' 2>&1', shell=True, env=env, universal_newlines=True, stdout=PIPE, stdin=PIPE, ) self.process = process while True: out_char = process.stdout.read(1) if out_char == '': if process.poll() is not None: # The process has finished break else: # The process is not giving any interesting # output. No use polling it immediatly. sleep(0.1) else: self.out_callback(out_char) if self.end_callback is not None: self.end_callback()
def _run(self): ''' Runs the command in self.cmdlist from self.workingdir with a timer bounded by self.runtimeout ''' logger.debug('Running: %s %s', self.cmdlist, self.workingdir) process_info = {} id = None done = False started = False wmiInterface = None # set timeout(s) # run program if self.hideoutput: p = Popen(self.cmdlist, stdout=open( os.devnull), stderr=open(os.devnull)) else: p = Popen(self.cmdlist) if self.watchcpu == True: # Initialize things used for CPU monitoring logger.debug('Initializing WMI...') wmiInterface = wmi.WMI() id = p.pid logger.debug('...Timer: %f', self.runtimeout) t = Timer(self.runtimeout, kill, args=[p]) self.t = t logger.debug('...timer start') t.start() if self.watchcpu == True: # This is a race. In some cases, a GUI app could be done before we can even measure it # TODO: Do something about it while p.poll() is None and not done and id: for proc in wmiInterface.Win32_PerfRawData_PerfProc_Process(IDProcess=id): n1, d1 = long(proc.PercentProcessorTime), long( proc.Timestamp_Sys100NS) n0, d0 = process_info.get(id, (0, 0)) try: percent_processor_time = ( float(n1 - n0) / float(d1 - d0)) * 100.0 except ZeroDivisionError: percent_processor_time = 0.0 process_info[id] = (n1, d1) logger.debug( 'Process %s CPU usage: %s', id, percent_processor_time) if percent_processor_time < 0.0000000001: if started: logger.debug( 'killing %s due to CPU inactivity', id) done = True kill(p) else: # Detected CPU usage. Now look for it to drop near zero started = True if not done: time.sleep(0.2) else: p.wait() # probably racy logger.debug('...timer stop') t.cancel() self.returncode = ctypes.c_uint(p.returncode).value logger.debug( '...Returncode: raw=%s cast=%s', p.returncode, self.returncode) logger.debug('...Exceptions: %s', self.exceptions) if self.returncode in self.exceptions: self.saw_crash = True logger.debug('...Saw_crash: %s', self.saw_crash)
def _run(self): ''' Runs the command in self.cmdlist from self.workingdir with a timer bounded by self.runtimeout ''' logger.debug('Running: %s %s', self.cmdlist, self.workingdir) process_info = {} id = None done = False started = False wmiInterface = None # set timeout(s) # run program if self.hideoutput: p = Popen(self.cmdlist, stdout=open(os.devnull), stderr=open(os.devnull)) else: p = Popen(self.cmdlist) if self.watchcpu == True: # Initialize things used for CPU monitoring logger.debug('Initializing WMI...') wmiInterface = wmi.WMI() id = p.pid logger.debug('...Timer: %f', self.runtimeout) t = Timer(self.runtimeout, kill, args=[p]) self.t = t logger.debug('...timer start') t.start() if self.watchcpu == True: # This is a race. In some cases, a GUI app could be done before we can even measure it # TODO: Do something about it while p.poll() is None and not done and id: for proc in wmiInterface.Win32_PerfRawData_PerfProc_Process( IDProcess=id): n1, d1 = long(proc.PercentProcessorTime), long( proc.Timestamp_Sys100NS) n0, d0 = process_info.get(id, (0, 0)) try: percent_processor_time = (float(n1 - n0) / float(d1 - d0)) * 100.0 except ZeroDivisionError: percent_processor_time = 0.0 process_info[id] = (n1, d1) logger.debug('Process %s CPU usage: %s', id, percent_processor_time) if percent_processor_time < 0.0000000001: if started: logger.debug('killing %s due to CPU inactivity', id) done = True kill(p) else: # Detected CPU usage. Now look for it to drop near zero started = True if not done: time.sleep(0.2) else: p.wait() # probably racy logger.debug('...timer stop') t.cancel() self.returncode = ctypes.c_uint(p.returncode).value logger.debug('...Returncode: raw=%s cast=%s', p.returncode, self.returncode) logger.debug('...Exceptions: %s', self.exceptions) if self.returncode in self.exceptions: self.saw_crash = True logger.debug('...Saw_crash: %s', self.saw_crash)