How to get current CPU and RAM usage in Python?

前端 未结 16 1483
攒了一身酷
攒了一身酷 2020-11-22 04:09

What\'s your preferred way of getting current system status (current CPU, RAM, free disk space, etc.) in Python? Bonus points for *nix and Windows platforms.

There s

相关标签:
16条回答
  • 2020-11-22 04:48

    The psutil library gives you information about CPU, RAM, etc., on a variety of platforms:

    psutil is a module providing an interface for retrieving information on running processes and system utilization (CPU, memory) in a portable way by using Python, implementing many functionalities offered by tools like ps, top and Windows task manager.

    It currently supports Linux, Windows, OSX, Sun Solaris, FreeBSD, OpenBSD and NetBSD, both 32-bit and 64-bit architectures, with Python versions from 2.6 to 3.5 (users of Python 2.4 and 2.5 may use 2.1.3 version).


    Some examples:

    #!/usr/bin/env python
    import psutil
    # gives a single float value
    psutil.cpu_percent()
    # gives an object with many fields
    psutil.virtual_memory()
    # you can convert that object to a dictionary 
    dict(psutil.virtual_memory()._asdict())
    # you can have the percentage of used RAM
    psutil.virtual_memory().percent
    79.2
    # you can calculate percentage of available memory
    psutil.virtual_memory().available * 100 / psutil.virtual_memory().total
    20.8
    

    Here's other documentation that provides more concepts and interest concepts:

    • https://psutil.readthedocs.io/en/latest/
    0 讨论(0)
  • 2020-11-22 04:48

    Use the psutil library. On Ubuntu 18.04, pip installed 5.5.0 (latest version) as of 1-30-2019. Older versions may behave somewhat differently. You can check your version of psutil by doing this in Python:

    from __future__ import print_function  # for Python2
    import psutil
    print(psutil.__versi‌​on__)
    

    To get some memory and CPU stats:

    from __future__ import print_function
    import psutil
    print(psutil.cpu_percent())
    print(psutil.virtual_memory())  # physical memory usage
    print('memory % used:', psutil.virtual_memory()[2])
    

    The virtual_memory (tuple) will have the percent memory used system-wide. This seemed to be overestimated by a few percent for me on Ubuntu 18.04.

    You can also get the memory used by the current Python instance:

    import os
    import psutil
    pid = os.getpid()
    py = psutil.Process(pid)
    memoryUse = py.memory_info()[0]/2.**30  # memory use in GB...I think
    print('memory use:', memoryUse)
    

    which gives the current memory use of your Python script.

    There are some more in-depth examples on the pypi page for psutil.

    0 讨论(0)
  • 2020-11-22 04:49
    • For CPU details use psutil library

      https://psutil.readthedocs.io/en/latest/#cpu

    • For RAM Frequency (in MHz) use the built in Linux library dmidecode and manipulate the output a bit ;). this command needs root permission hence supply your password too. just copy the following commend replacing mypass with your password

    import os

    os.system("echo mypass | sudo -S dmidecode -t memory | grep 'Clock Speed' | cut -d ':' -f2")

    ------------------- Output ---------------------------
    1600 MT/s
    Unknown
    1600 MT/s
    Unknown 0

    • more specificly
      [i for i in os.popen("echo mypass | sudo -S dmidecode -t memory | grep 'Clock Speed' | cut -d ':' -f2").read().split(' ') if i.isdigit()]

    -------------------------- output -------------------------
    ['1600', '1600']

    0 讨论(0)
  • 2020-11-22 04:55

    you can read /proc/meminfo to get used memory

    file1 = open('/proc/meminfo', 'r') 
    
    for line in file1: 
        if 'MemTotal' in line: 
            x = line.split()
            memTotal = int(x[1])
            
        if 'Buffers' in line: 
            x = line.split()
            buffers = int(x[1])
            
        if 'Cached' in line and 'SwapCached' not in line: 
            x = line.split()
            cached = int(x[1])
        
        if 'MemFree' in line: 
            x = line.split()
            memFree = int(x[1])
    
    file1.close()
    
    percentage_used = int ( ( memTotal - (buffers + cached + memFree) ) / memTotal * 100 )
    print(percentage_used)
    
    0 讨论(0)
  • 2020-11-22 04:55

    I don't believe that there is a well-supported multi-platform library available. Remember that Python itself is written in C so any library is simply going to make a smart decision about which OS-specific code snippet to run, as you suggested above.

    0 讨论(0)
  • 2020-11-22 04:56

    To get a line-by-line memory and time analysis of your program, I suggest using memory_profiler and line_profiler.

    Installation:

    # Time profiler
    $ pip install line_profiler
    # Memory profiler
    $ pip install memory_profiler
    # Install the dependency for a faster analysis
    $ pip install psutil
    

    The common part is, you specify which function you want to analyse by using the respective decorators.

    Example: I have several functions in my Python file main.py that I want to analyse. One of them is linearRegressionfit(). I need to use the decorator @profile that helps me profile the code with respect to both: Time & Memory.

    Make the following changes to the function definition

    @profile
    def linearRegressionfit(Xt,Yt,Xts,Yts):
        lr=LinearRegression()
        model=lr.fit(Xt,Yt)
        predict=lr.predict(Xts)
        # More Code
    

    For Time Profiling,

    Run:

    $ kernprof -l -v main.py
    

    Output

    Total time: 0.181071 s
    File: main.py
    Function: linearRegressionfit at line 35
    
    Line #      Hits         Time  Per Hit   % Time  Line Contents
    ==============================================================
        35                                           @profile
        36                                           def linearRegressionfit(Xt,Yt,Xts,Yts):
        37         1         52.0     52.0      0.1      lr=LinearRegression()
        38         1      28942.0  28942.0     75.2      model=lr.fit(Xt,Yt)
        39         1       1347.0   1347.0      3.5      predict=lr.predict(Xts)
        40                                           
        41         1       4924.0   4924.0     12.8      print("train Accuracy",lr.score(Xt,Yt))
        42         1       3242.0   3242.0      8.4      print("test Accuracy",lr.score(Xts,Yts))
    

    For Memory Profiling,

    Run:

    $ python -m memory_profiler main.py
    

    Output

    Filename: main.py
    
    Line #    Mem usage    Increment   Line Contents
    ================================================
        35  125.992 MiB  125.992 MiB   @profile
        36                             def linearRegressionfit(Xt,Yt,Xts,Yts):
        37  125.992 MiB    0.000 MiB       lr=LinearRegression()
        38  130.547 MiB    4.555 MiB       model=lr.fit(Xt,Yt)
        39  130.547 MiB    0.000 MiB       predict=lr.predict(Xts)
        40                             
        41  130.547 MiB    0.000 MiB       print("train Accuracy",lr.score(Xt,Yt))
        42  130.547 MiB    0.000 MiB       print("test Accuracy",lr.score(Xts,Yts))
    

    Also, the memory profiler results can also be plotted using matplotlib using

    $ mprof run main.py
    $ mprof plot
    

    Note: Tested on

    line_profiler version == 3.0.2

    memory_profiler version == 0.57.0

    psutil version == 5.7.0


    EDIT: The results from the profilers can be parsed using the TAMPPA package. Using it, we can get line-by-line desired plots as plot

    0 讨论(0)
提交回复
热议问题