Ben Chuanlong Du's Blog

It is never too late to learn.

Hands on the psutil Module in Python

In [3]:
!pip3 install psutil
Collecting psutil
  Downloading psutil-5.7.3.tar.gz (465 kB)
     |████████████████████████████████| 465 kB 2.0 MB/s 
Building wheels for collected packages: psutil
  Building wheel for psutil (setup.py) ... done
  Created wheel for psutil: filename=psutil-5.7.3-cp38-cp38-macosx_10_15_x86_64.whl size=234560 sha256=03c5b38dcb3243006fd488e10e8ff17adf038b8755afdd316ed367976181ce45
  Stored in directory: /Users/dclong/Library/Caches/pip/wheels/f6/59/c2/38111ef4c354088a156bc95fbeb5396c0cac91a0f62f7158b9
Successfully built psutil
Installing collected packages: psutil
Successfully installed psutil-5.7.3
In [4]:
import psutil
In [5]:
psutil.virtual_memory()
Out[5]:
svmem(total=34359738368, available=16077950976, percent=53.2, used=12415860736, free=7193649152, active=8815947776, inactive=8731512832, wired=3599912960)
In [6]:
psutil.cpu_percent()
Out[6]:
4.8
In [17]:
psutil.virtual_memory()
Out[17]:
svmem(total=34359738368, available=16035885056, percent=53.3, used=12460191744, free=7184756736, active=8855121920, inactive=8669417472, wired=3605069824)
In [10]:
pids = psutil.pids()
In [14]:
p = psutil.Process(1)
p
Out[14]:
psutil.Process(pid=1, name='launchd', status='running', started='2020-10-19 22:13:12')
In [16]:
p.username()
Out[16]:
'root'
In [22]:
for p in psutil.process_iter():
    if p.username() == "dclong":
        print(p.memory_info())
        break
pmem(rss=4644864, vms=4798234624, pfaults=3825, pageins=91)
In [29]:
for p in psutil.process_iter():
    if p.username() == "dclong":
        print(f"Memory info: {p.memory_info()}")
        print(f"Memory pct: {p.memory_percent()}")
        print(f"CPU pct: {p.cpu_percent()}")
        break
Memory info: pmem(rss=4644864, vms=4798234624, pfaults=3825, pageins=91)
Memory pct: 0.013518333435058594
CPU pct: 0.0
In [32]:
p.status()
Out[32]:
'running'
In [33]:
sum(
    p.memory_percent()
    for p in psutil.process_iter()
    if p.username() == "dclong" and p.status() == "running"
)
Out[33]:
54.66163158416748
In [42]:
sum(
    p.cpu_percent()
    for p in psutil.process_iter()
    if p.username() == "dclong" and p.status() == "running"
)
Out[42]:
65.39999999999999
In [2]:
for proc in psutil.process_iter():
    print(proc)
psutil.Process(pid=1, name='bash', started='09:39:49')
psutil.Process(pid=38, name='jupyterhub', started='09:39:49')
psutil.Process(pid=43, name='node', started='09:39:50')
psutil.Process(pid=63, name='jupyterhub-sing', started='09:39:59')
psutil.Process(pid=125, name='bash', started='09:44:41')
psutil.Process(pid=147, name='python3', started='10:01:47')
psutil.Process(pid=184, name='pyls', started='10:01:59')
psutil.Process(pid=203, name='python3', started='10:02:20')
In [ ]:
#!/usr/bin/env python3
import psutil

for proc in psutil.process_iter():
    if proc.username() == "bendu":
        try:
            cmd = proc.cmdline()
        except:
            continue
        if "chrome" in cmd[0].lower():
            print(proc.pid)
In [ ]:
 

Comments