简体   繁体   中英

How to determine the maximum number of processes that can be run per CPU core using python?

Using python 3.8.8 on a windows box, how to determine the maximum number of processes that can be run per CPU core? I followed some previously asked related questions like 1 , 2 , 3 , 4 .

Now the python docs on this subject suggest to use len(os.sched_getaffinity(0)) . However, when I try in code, it returns an error,

import os
print(len(os.sched_getaffinity(0)))

AttributeError: module 'os' has no attribute 'sched_getaffinity'

Then I try another code snippet to identify the available functions,

import os
print(dir(os))

and it gives me a list of functions wherein sched_getaffinity() is not available.

My environment is:

  • Windows 10 64-bit
  • Python 3.8.8
  • 8 core processor

The number of processes a CPU core can run simultaneously is equal to the number of threads per CPU core. And to get the number of threads per CPU core in python you can do something like this:

import psutil
total_threads = psutil.cpu_count()/psutil.cpu_count(logical=False)
print('You can run {} processes per CPU core simultaneously'.format(total_threads))

To check different processors your script can run on you can use:

print(psutil.Process().cpu_affinity())

Have a look at the function definition in __init__.pyi for os library

if sys.platform != "win32":
    from posix import sched_param
    ...
    def sched_getaffinity(pid: int) -> Set[int]: ...  # some flavors of Unix

This function is not available for windows systems.

You can use another option from the answers you linked, such as os.cpu_count() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM