@
askfermi @
maemual 谢谢。附上写好的代码。
```python
#!/usr/bin/env python
import time
import psutil
line_num = 1
def print_line (str ):
print str
#function of Get CPU State
def getCPUstate (interval=1 ):
return (" CPU: " + str (psutil.cpu_percent (interval )) + "%")
#function of Get Memory
def getMemorystate ():
phymem = psutil.phymem_usage ()
buffers = getattr (psutil, 'phymem_buffers', lambda: 0 )()
cached = getattr (psutil, 'cached_phymem', lambda: 0 )()
used = phymem.total - (phymem.free + buffers + cached )
line = " Memory: %6s" % (
str (int (used / 1024 / 1024 )) + "M",
)
return line
def poll (interval ):
"""Retrieve raw stats within an interval window."""
# sleep some time
time.sleep (interval )
# get cpu state
cpu_state = getCPUstate (interval )
# get memory
memory_state = getMemorystate ()
return (cpu_state,memory_state )
def refresh_window (cpu_state,memory_state ):
#print current time #cpu state #memory
print_line (time.asctime ())
print_line (cpu_state )
print_line (memory_state )
try:
interval =0
while 1:
args = poll (interval )
refresh_window (*args )
interval = 1
except (KeyboardInterrupt, SystemExit ):
pass
```