Skip to content

gh-149452: os.process_cpu_count() now respects cgroups - #162510

Open
itamarst wants to merge 17 commits into
python:mainfrom
itamarst:149452-cgroups-process_count
Open

itamarst wants to merge 17 commits into
python:mainfrom
itamarst:149452-cgroups-process_count

Conversation

@itamarst

@itamarst itamarst commented Nov 23, 2026 •

Copy link
Copy Markdown

@python-cla-bot

python-cla-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@read-the-docs-community

read-the-docs-community Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

@picnixz picnixz left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about PYTHON_CPU_COUNT that is explicitly set? (I don't see a test)

Comment thread Lib/os.py Resolved
Comment thread Lib/os.py Resolved
Comment thread Lib/os.py Resolved
Comment thread Lib/os.py Resolved
Comment thread Doc/library/os.rst Resolved
python_code="import os; print(os.process_cpu_count())"
):
result = [
"systemd-run",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this test. It changes systemd settings, even temporarily.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't really change systemd settings. It creates a new scope: "The main purpose of scope units is grouping worker processes of a system service for organization and for managing resources." per the man page (https://www.freedesktop.org/software/systemd/man/latest/systemd.scope.html).

So this creates a new scope, with specific CPU quota just for those processes, in the scope, by creating a new cgroup.

So this is essentially like doing docker run: it runs a process under a new cgroup. But unlike Docker you don't have have to jump through hoops to access the host filesystem, so it's mechanically simpler.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant is that it still creates a transient systemd config. What about systems that aren't on systemd? I believe Alpine doesn't have systemd but alpine docker images are quite used right? (or maybe I misremember).

What I meant is: would it be possible to mock the result of open() instead without invoking systemd at all? And only skip the test when the files don't exist for instance (and also exercise the code paths where the file doesn't exist indeed or raises OSError)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's very useful to have end-to-end tests, when possible, otherwise it's easy to end up in a situation where the mocks/fakes diverge from reality.

But yeah there's the error paths too. And perhaps some CPython devs use non-systemd Linux, too, though CI at least will catch problems.

So maybe an additional set of mock/fake-based tests? And maybe the end-to-end test with systemd can be a bit smaller.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant is: would it be possible to mock the result of open() instead without invoking systemd at all?

Hum. If we decide to add tests mocking open(), I would still prefer to also have the systemd-run "integration tests". I already saw major bugs in code tested only by mocks. When you mock everything, you don't test the implementation anymore, but you test the mocks themselves :-D So the two kinds of tests are useful and have different purposes.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some more tests that talk to the underlying functions directly, and exercise the OSError paths. But I kept (most) of the end-to-end tests too. Let me know if there's more test coverage you want.

@bedevere-app

bedevere-app Bot commented Nov 24, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Comment thread Lib/os.py Outdated

if sys.platform == "linux":
def _get_cgroup2_mount_point():
from builtins import open

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you get use builtins.open() instead of open()?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cause open() gets masked by os.open().

Comment thread Lib/os.py
Comment on lines +1279 to +1282
# Rounds up in order to saturate CPUs if the quota is fractional:
import math

return max(math.ceil(quota / period), 1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can avoid floating point numbers and math.ceil() code with:

Suggested change
# Rounds up in order to saturate CPUs if the quota is fractional:
import math
return max(math.ceil(quota / period), 1)
# Rounds up in order to saturate CPUs if the quota is fractional:
count = (quota + (period - 1)) // period
return max(count, 1)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt the float version was more readable, but can change if you want.

Comment thread Lib/os.py Resolved
Comment thread Lib/os.py Resolved
Comment thread Lib/os.py Resolved
Comment thread Lib/os.py Resolved
Comment thread Lib/test/test_os/test_os.py Resolved
Comment thread Lib/test/test_os/test_os.py Resolved
@itamarst

Copy link
Copy Markdown
Author

Thank you for the reviews! Will start addressing now.

@itamarst

Copy link
Copy Markdown
Author

What about PYTHON_CPU_COUNT that is explicitly set? (I don't see a test)

There are existing tests for this in Lib/test/test_cmd_line.py; they still pass, and should suffice.

@itamarst

Copy link
Copy Markdown
Author

I have made the requested changes; please review again

@bedevere-app

bedevere-app Bot commented Nov 24, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@picnixz: please review the changes made to this pull request.

@bedevere-app
bedevere-app Bot requested a review from picnixz November 24, 2026 19:13
@itamarst
itamarst requested a review from vstinner November 25, 2026 14:50
@itamarst
itamarst marked this pull request as ready for review November 25, 2026 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

os.process_cpu_count() should take cgroups CPU limitations into account (on Linux)

3 participants