Title: os.process_cpu_count() should take cgroups CPU limitations into account (on Linux) · Issue #149452 · python/cpython · GitHub
Open Graph Title: os.process_cpu_count() should take cgroups CPU limitations into account (on Linux) · Issue #149452 · python/cpython
X Title: os.process_cpu_count() should take cgroups CPU limitations into account (on Linux) · Issue #149452 · python/cpython
Description: Feature or enhancement Proposal: os.process_cpu_count() currently takes CPU core scheduler affinity into account. However, there are other ways to restrict CPU usage on Linux, most significantly cgroups as used by Docker and Kubernetes. ...
Open Graph Description: Feature or enhancement Proposal: os.process_cpu_count() currently takes CPU core scheduler affinity into account. However, there are other ways to restrict CPU usage on Linux, most significantly cg...
X Description: Feature or enhancement Proposal: os.process_cpu_count() currently takes CPU core scheduler affinity into account. However, there are other ways to restrict CPU usage on Linux, most significantly cg...
Opengraph URL: https://github.com/python/cpython/issues/149452
X: @github
Domain: github.com
IPv4 address: 140.82.112.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"os.process_cpu_count() should take cgroups CPU limitations into account (on Linux)","articleBody":"# Feature or enhancement\n\n### Proposal:\n\n`os.process_cpu_count()` currently takes CPU core scheduler affinity into account. However, there are other ways to restrict CPU usage on Linux, most significantly cgroups as used by Docker and Kubernetes.\n\nFor example, if I ask for 2 cpus for a container, `process_cpu_count()` does not respect that:\n\n```shell-session\n$ docker run -it --cpus=2 python:3.14-slim python -c \"import os; print(os.process_cpu_count())\"\n20\n```\n\nThis can lead to surprising behavior for users, where too many threads are started in a container given the available resources.\n\n## An existing implementation in Python\n\nIn contrast, the `loky.cpu_count()` function does know about these restrictions:\n\n```shell-session\n$ docker run -it --cpus=2 python:3.14-slim bash\nroot@557b43351822:/# pip install --quiet loky\nroot@557b43351822:/# python \nPython 3.14.2 (main, Jan 13 2026, 03:08:39) [GCC 14.2.0] on linux\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n>>> import loky\n>>> loky.cpu_count()\n2\n```\n\nYou can [read the loky implementation (permanlink to version as of May 6, 2026)](https://github.com/joblib/loky/blob/5098fdf13b2e693986b55390832d063387cb54e4/loky/backend/context.py#L78).\n\n### Has this already been discussed elsewhere?\n\nThis is a minor feature, which does not need previous discussion elsewhere\n\n### Links to previous discussion of this feature:\n\nThere was some discussion in https://github.com/python/cpython/issues/77167. Given `cpu_count()` is now distinct from `process_cpu_count()`, it seems useful to make the latter accurate, and cgroups as the basis for Linux containerization has a massive scale of deployment.\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-162510\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/itamarst","@type":"Person","name":"itamarst"},"datePublished":"2026-05-06T12:02:48Z","url":"https://github.com/python/cpython/issues/149452"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:7976e65d-c64d-6df7-23f0-2d0e168bfdc1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B304:144D14:BD0540:FD9A8C:6B0649DD |
| html-safe-nonce | 84e0f361fa3b418a424cd8ad05a55093a027fb2a1b7f1482d30a0107f994791c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMzA0OjE0NEQxNDpCRDA1NDA6RkQ5QThDOjZCMDY0OUREIiwidmlzaXRvcl9pZCI6IjM4NjU4Njk0MTE3NDM1ODI2ODUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 67ddb6ede5f443adbd659f28f9f004596b2622a751880a707a8df2f8ab1be6e4 |
| hovercard-subject-tag | issue:4391156528 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/149452/issue_layout |
| twitter:image | https://opengraph.githubassets.com/cd77f040674a6e3dd1e97262b451ca76be8c4602dd5ea762ad61e2abfbaa4489/python/cpython/issues/149452 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/cd77f040674a6e3dd1e97262b451ca76be8c4602dd5ea762ad61e2abfbaa4489/python/cpython/issues/149452 |
| og:image:alt | Feature or enhancement Proposal: os.process_cpu_count() currently takes CPU core scheduler affinity into account. However, there are other ways to restrict CPU usage on Linux, most significantly cg... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | itamarst |
| hostname | github.com |
| expected-hostname | github.com |
| None | 772b6997ca4daa0428dd49a25d76052e765555ae560fe9c88517eb828bbd861e |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f1f9a08f7c769a37f1dfc18ad43d463c222a7eea |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width