[英]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?在 windows 机器上使用 python 3.8.8,如何确定每个 CPU 核心可以运行的最大进程数? I followed some previously asked related questions like 1 , 2 , 3 , 4 .我关注了一些先前提出的相关问题,例如1 、 2 、 3 、 4 。
Now the python docs on this subject suggest to use len(os.sched_getaffinity(0))
.现在关于这个主题的 python 文档建议使用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.它给了我一个函数列表,其中sched_getaffinity()
不可用。
My environment is:我的环境是:
The number of processes a CPU core can run simultaneously is equal to the number of threads per CPU core.一个 CPU 内核可以同时运行的进程数等于每个 CPU 内核的线程数。 And to get the number of threads per CPU core in python you can do something like this:要在 python 中获取每个 CPU 核心的线程数,您可以执行以下操作:
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查看__init__.pyi
for os
库中的函数定义
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.此功能不适用于 windows 系统。
You can use another option from the answers you linked, such as os.cpu_count()
.您可以使用链接的答案中的另一个选项,例如os.cpu_count()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.