def do_task(self,task=None,trace=False): """ Executes an string or callable """ trace = trace or self.trace task = task or self.task if trace: print 'In CronTab(%s).do_task(%s)'%(self.line,task) if isCallable(task): ret = task() elif isString(task): from fandango.linos import shell_command ret = shell_command(self.task) else: raise Exception('NotCallable/String') if self.keep: if self._queue.full(): self.get() self._queue.put(ret,False) if trace: print 'CronTab(%s).do_task() => %s'%(self.line,ret)
def getMemRate(self): try: #print('getMemRate()') meminfo = shell_command('cat /proc/meminfo').split('\n') meminfo = dict( map(str.strip, a.split(':', 1)) for a in meminfo if a.strip()) getter = lambda k: float( eval(meminfo[k].lower().replace('kb', '*1').replace( 'mb', '*1e3').replace('b', '*1e-3'))) self._total, self._free, self._cached = getter('MemTotal'), getter( 'MemFree'), getter('Cached') self._cached += getter('Slab') + getter('Buffers') ## Shared Memory Reclaimable could be considered also as Cached, but it is not 100% sure if it will be always reusable or not # Therefore, I prefered to ignore it. This is why this method may return less free memory than top. self._used = (self._total - (self._free + self._cached)) self._memrate = (self._used / self._total) except: traceback.print_exc() self._memrate = 0. return self._memrate
def Update(self): #self.info_stream tracer("In %s::Update(%s)" % (self.get_name(), self.Threaded)) # Add your own code here try: if self.get_state() == PyTango.DevState.INIT: #self.warn_stream("Not yet initialized to start an update.") return self._loadAverage = list(os.getloadavg()) try: self._nTasks = int(shell_command("ps auxwwH|wc -l")) except: traceback.print_exc() self._nTasks = 0 #do in a separate thread to avoid to hang the device if it takes too long if self.Threaded: if not self.updateThread == None and self.updateThread.is_alive( ): #self.warn_stream("Try to update when not finish yet last call") self.change_state(PyTango.DevState.ALARM) self.addStatusMsg( "Update() command call overlapped with a non finish previous call" ) else: if self.updateThread is not None: del self.updateThread #self.updateThread = Process(target=self.update_ProcessList) self.updateThread = threading.Thread( target=self.update_ProcessList) self.updateThread.run() self.change_state(PyTango.DevState.ON) self.addStatusMsg("") else: self.update_ProcessList() self.change_state(PyTango.DevState.ON) except Exception, e: self.error_stream("Update() exception: %s" % (str(e)))
def get_all_process(regexp=''): """ Returns a {int(PID):stats} dictionary """ import re #tracer('get_all_process(%s)'%regexp) pss = {} try: comm = "ps hax -o pid,nlwp,%cpu,%mem,rss,vsize,cmd" #hax is needed to show Starter process lines = shell_command(comm).split('\n') for r in map(str.split, lines): if not r: continue r = dict( (k, v if k == 'cmd' else (float(v) if '.' in v else int(v))) for k, v in zip(('pid', 'threads', 'cpu', 'mem', 'rss', 'vsize', 'cmd'), (r[0:6]) + [' '.join(r[6:])])) #r['rss'],r['vsize'] = 1e-3*r['rss'],1e-3*r['vsize'] #Memory read in MB r['rss'], r['vsize'] = r['rss'], r['vsize'] #Memory read in KBytes if r['pid'] and not regexp or re.search(regexp, r.get('cmd', '')): pss[int(r['pid'])] = r except: print(traceback.format_exc()) return pss