def cpu_percent(interval=None, percpu=False):
 global _last_cpu_times
 global _last_per_cpu_times
 blocking = ((interval is not None) and (interval > 0.0))
 if ((interval is not None) and (interval < 0)):
  raise ValueError(('interval is not positive (got %r)' % interval))
 def calculate(t1, t2):
  t1_all = _cpu_tot_time(t1)
  t1_busy = _cpu_busy_time(t1)
  t2_all = _cpu_tot_time(t2)
  t2_busy = _cpu_busy_time(t2)
  if (t2_busy <= t1_busy):
   return 0.0
  busy_delta = (t2_busy - t1_busy)
  all_delta = (t2_all - t1_all)
  busy_perc = ((busy_delta / all_delta) * 100)
  return round(busy_perc, 1)
 if (not percpu):
  if blocking:
   t1 = cpu_times()
   time.sleep(interval)
  else:
   t1 = _last_cpu_times
   if (t1 is None):
    t1 = cpu_times()
  _last_cpu_times = cpu_times()
  return calculate(t1, _last_cpu_times)
 else:
  ret = []
  if blocking:
   tot1 = cpu_times(percpu=True)
   time.sleep(interval)
  else:
   tot1 = _last_per_cpu_times
   if (tot1 is None):
    tot1 = cpu_times(percpu=True)
  _last_per_cpu_times = cpu_times(percpu=True)
  for (t1, t2) in zip(tot1, _last_per_cpu_times):
   ret.append(calculate(t1, t2))
  return ret

def cpu_percent ( interval = None , percpu = False ) : global _last_cpu_times global _last_per_cpu_times blocking = ( ( interval is not None ) and ( interval > 0.0 ) ) if ( ( interval is not None ) and ( interval < 0 ) ) : raise ValueError ( ( 'interval is not positive (got %r)' % interval ) ) def calculate ( t1 , t2 ) : t1_all = _cpu_tot_time ( t1 ) t1_busy = _cpu_busy_time ( t1 ) t2_all = _cpu_tot_time ( t2 ) t2_busy = _cpu_busy_time ( t2 ) if ( t2_busy <= t1_busy ) : return 0.0 busy_delta = ( t2_busy - t1_busy ) all_delta = ( t2_all - t1_all ) busy_perc = ( ( busy_delta / all_delta ) * 100 ) return round ( busy_perc , 1 ) if ( not percpu ) : if blocking : t1 = cpu_times ( ) time . sleep ( interval ) else : t1 = _last_cpu_times if ( t1 is None ) : t1 = cpu_times ( ) _last_cpu_times = cpu_times ( ) return calculate ( t1 , _last_cpu_times ) else : ret = [ ] if blocking : tot1 = cpu_times ( percpu = True ) time . sleep ( interval ) else : tot1 = _last_per_cpu_times if ( tot1 is None ) : tot1 = cpu_times ( percpu = True ) _last_per_cpu_times = cpu_times ( percpu = True ) for ( t1 , t2 ) in zip ( tot1 , _last_per_cpu_times ) : ret . append ( calculate ( t1 , t2 ) ) return ret
return the percent of time the cpu is busy .