Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpu_percent return 0.0 for single process, but it was okay for system-wild CPU utilization #2278

Closed
mozz85 opened this issue Jul 18, 2023 · 4 comments
Labels

Comments

@mozz85
Copy link

mozz85 commented Jul 18, 2023

Summary

psutil 5.9.2
windows

Description

here is the test code

import subprocess
import psutil
import time

os_process = subprocess.Popen('notepad', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
psutil_process = psutil.Process(os_process.pid)
time.sleep(1)
print(psutil_process.cpu_percent(interval=None))
time.sleep(1)
print(psutil_process.cpu_percent(interval=None))
time.sleep(1)
print(psutil_process.cpu_percent(interval=None))
time.sleep(1)
print(psutil_process.cpu_percent(interval=None))

here is the output
image
if the image load failed
0.0
0.0
0.0
0.0

@mozz85 mozz85 added the bug label Jul 18, 2023
@mozz85
Copy link
Author

mozz85 commented Jul 18, 2023

i can get the vaild value of system-wild CPU utilization

time.sleep(1)
print(psutil.cpu_percent(interval=None))
time.sleep(1)
print(psutil.cpu_percent(interval=None))
time.sleep(1)
print(psutil.cpu_percent(interval=None))
time.sleep(1)
print(psutil.cpu_percent(interval=None))

output goes like
4.7
2.5
5.5
3.6

i also tried for the current process like this, and i failed too

p = psutil.Process()
time.sleep(1)
print(p.cpu_percent(interval=None))
time.sleep(1)
print(p.cpu_percent(interval=None))
time.sleep(1)
print(p.cpu_percent(interval=None))
time.sleep(1)
print(p.cpu_percent(interval=None))

output goes like
0.0
0.0
0.0
0.0

@mozz85
Copy link
Author

mozz85 commented Jul 18, 2023

image
image

@mozz85 mozz85 changed the title cpu_percent always return 0.0 for other process cpu_percent return 0.0 for single process, but it was okay for system-wild CPU utilization Jul 18, 2023
@giampaolo
Copy link
Owner

I guess it's because the process does basically nothing.

@mozz85
Copy link
Author

mozz85 commented Jul 19, 2023

You are right. Althought the process that i want to check out did something, when i print out the os_process.pid, i found out that's not it. The code that i copied from chatgpt set the shell=True when calling the Popen. and i know nothing about it. That opens a shell, and that shell opens the intended process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants