Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. def cpu_percent(interval=0.01):
  2.     """Return the current system-wide CPU utilization as a percentage.
  3.    Interval
  4.    """
  5.     t1 = cpu_times()
  6.     t1_all = sum(t1)
  7.     t1_busy = t1_all - t1.idle
  8.  
  9.     time.sleep(interval)
  10.    
  11.     t2 = cpu_times()
  12.     t2_all = sum(t2)
  13.     t2_busy = t2_all - t2.idle
  14.  
  15.     busy_delta = t2_busy - t1_busy
  16.     difference = t2_all - t1_all
  17.     try:
  18.         busy_perc = (busy_delta / difference) * 100
  19.         if busy_perc < 0.0:
  20.            raise ZeroDivisionError            
  21.     except ZeroDivisionError:
  22.         raise ValueError("inverval too low")
  23.     else:
  24.         return float(busy_perc)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement