Skip to content

os.process_cpu_count(): get the number CPUs usable by the current process #109649

Description

@vstinner

Feature or enhancement

os.cpu_count() returns the total number of CPUs that the current machine has. On Unix, sched_setaffinity() can reduce the number of CPU "usable" by a process. Currently, os.cpu_count() docstring contains a recipe how to get the number of usable CPUs:

>>> import os; help(os.cpu_count)
cpu_count()
    Return the number of CPUs in the system; return None if indeterminable.
    
    This number is not equivalent to the number of CPUs the current process can
    use.  The number of usable CPUs can be obtained with
    ``len(os.sched_getaffinity(0))``

I propose to add an affinity optional keyword-only parameter to os.cpu_count() which would return len(os.sched_getaffinity(0)).st

UPDATE: Rename usable parameter to affinity.

Linked PRs

Activity

  1. vstinner commented on Sep 21, 2023

    @vstinner
    MemberAuthor

    os.cpu_count() issues (open and closed):

    Previously, the function was known as multiprocessing.cpu_count():

  2. added 3 commits that reference this issue on Sep 21, 2023
  3. vstinner commented on Sep 21, 2023

    @vstinner
    MemberAuthor

    On Windows, maybe it can be implemented with GetProcessGroupAffinity(), GetProcessAffinityMask(), GetActiveProcessorGroupCount() and GetActiveProcessorCount().

    GetProcessGroupAffinity():
    https://learn.microsoft.com/en-us/windows/win32/api/processtopologyapi/nf-processtopologyapi-getprocessgroupaffinity

  4. vstinner commented on Sep 21, 2023

    @vstinner
    MemberAuthor

    On Windows, a CPU affinity can be run with start /affinity command. Example to only run Python on the firstCPU:

    start /affinity 0x0000000000000001 python.exe
    
  5. added a commit that references this issue on Sep 21, 2023
  6. changed the title [-]os.cpu_count(): add "usable" parameter to get the number of "usable" CPUs[/-] [+]os.cpu_count(): add "affinity" parameter to get the number of logical CPUs usable by the current process[/+] on Sep 21, 2023
  7. added 6 commits that reference this issue on Sep 21, 2023
  8. 5 remaining items

  9. added 6 commits that reference this issue on Sep 30, 2023
  10. added a commit that references this issue on Oct 2, 2023
  11. added a commit that references this issue on Oct 2, 2023
  12. added a commit that references this issue on Oct 3, 2023
  13. added 2 commits that reference this issue on Nov 3, 2023
  14. added a commit that references this issue on Feb 11, 2024
  15. added 3 commits that reference this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions