iFocus.Life News News - Breaking News & Top Stories - Latest World, US & Local News,Get the latest news, exclusives, sport, celebrities, showbiz, politics, business and lifestyle from The iFocus.Life,

How to Write a Python Script That Can Read CPU Usage on Windows

104 22
    • 1). Download and install the "psutils" library (from code.google.com/p/psutil/). The website contains a self-installing binary for the Windows operating system, listed as the newest file version ending with the ".exe" file extension. Download the file and execute it to install the library.

    • 2). In the Python Interactive Environment, import the library and read the current CPU times through the "cpu_times()" method. The following example code details how to use the cpu_times() method:

      >>>import psutil

      >>>psutil.cpu_times()

      cputimes(user=3961.46, nice=169.729, system=2150.659, idle=16900.540, iowait=629.509, irq=0.0, softirq=19.422)

    • 3). Get the CPU usage in intervals through psutil. Getting the usage intervals is similar to what the Windows Task Manager presents to the user when viewing the resources used screen (with a percentage given on 1 second intervals). The following script will mimic this functionality:

      >>> for x in range(5):

      ... psutil.cpu_percent(interval=1)

      ...

      25.0

      3.0

      10.0

      2.0

      3.0

Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time
You might also like on "Technology"

Leave A Reply

Your email address will not be published.