Copy Code code as follows:
#!/usr/bin/env Python
Import OS
Def load_stat ():
Loadavg = {}
f = open ("/proc/loadavg")
con = F.read (). Split ()
F.close ()
loadavg[' lavg_1 ']=con[0]
loadavg[' Lavg_5 ']=con[1]
loadavg[' Lavg_15 ']=con[2]
loadavg[' nr ']=con[3]
loadavg[' Last_pid ']=con[4]
Return Loadavg
Print "Loadavg", Load_stat () [' Lavg_15 ']
Simply explain Listing 2: Listing 2 reads the information in the/proc/loadavg, import Os:python imports the different modules, including system-supplied and custom modules. The basic form is: Import module name [as Alias], if you want to import only some or all of the modules in the module can be in the form of: from Module name import * to the appropriate module. OS Module OS module provides a unified operating system interface function, the OS module can switch automatically between the specific functions of different operating system platform, such as Nt,posix, so as to achieve cross-platform operation.
You can run the script using the Python command cpu1.py results are shown in Figure 2