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

[Linux] cpu_percent does not work within oneshot context manager as expected #2072

Open
nj-vs-vh opened this issue Feb 16, 2022 · 1 comment

Comments

@nj-vs-vh
Copy link

nj-vs-vh commented Feb 16, 2022

Summary

  • OS: Ubuntu 20.04
  • Architecture: x86 64bit
  • Psutil version: 5.8.0
  • Python version: 3.8.10
  • Type: doc, core (?)

Description

Not sure if this is a bug, but this behaviour is not documented at all and has left me scratching my head for couple of hours. Basically, cpu_percent method does not work as described with non-zero interval argument when called in oneshot context. My test scripts:

With oneshot

import psutil
p = psutil.Process(41264)
with p.oneshot():
    print(p.cpu_percent(interval=0.5))

prints 0.0

With oneshot and explicit sleep

import psutil
import time
p = psutil.Process(41264)
with p.oneshot():
    p.cpu_percent()
    time.sleep(0.5)
    print(p.cpu_percent())

prints 0.0

Without context manager

import psutil
p = psutil.Process(41264)
print(p.cpu_percent(interval=0.5))

Prints 16.0 or other non-zero value, as expected.

Suggestions

If this is by design, I think it should be mentioned in the documentation under oneshot and/or cpu_percent descriptions. Currently documentation for the context manager lists cpu_percent among methods that gain a speedup from it and even shows a code that doesn't seem to work:

with p.oneshot():
    p.cpu_percent()  # return cached value
@nj-vs-vh nj-vs-vh added the bug label Feb 16, 2022
@nj-vs-vh nj-vs-vh changed the title [Linux] cpu_percent does not work within oneshot context manager [Linux] cpu_percent does not work within oneshot context manager as expected Feb 16, 2022
@nj-vs-vh
Copy link
Author

Just checked the behaviour -- it's the same for the newer 5.9.0 version.

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

No branches or pull requests

1 participant