简体   繁体   English

如何使用python确定每个CPU核心可以运行的最大进程数?

[英]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 .我关注了一些先前提出的相关问题,例如1234

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:我的环境是:

  • Windows 10 64-bit Windows 10 64 位
  • Python 3.8.8蟒蛇 3.8.8
  • 8 core processor 8核处理器

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.

相关问题 如何限制每个通过 gnu 并行启动的 python 进程使用一个 CPU? - How do I limit using one CPU per python processes launched via gnu parallel? 简单 CPU/GPU 上的最大并行进程数 - Maximum number of parallel processes on a simple CPU/GPU 我是否可以同时运行两个 python 进程,一个使用珊瑚 tpu,另一个只使用 cpu? - Is it possible for me to run two python processes at once, one using the coral tpu, and another using only the cpu? Python 使用 cpu 查找前 5 个进程的代码 - Python code for finding top 5 processes using cpu 如何使用 aiohttp 确定每秒的请求数? - How do I determine the number of requests per second using aiohttp? 如何找到理想数量的并行进程以使用 python 多处理运行? - How to find ideal number of parallel processes to run with python multiprocessing? 如何实现在“ N”个CPU CORES上运行的python脚本? - How to implement python script to run on “N” number of CPU CORES? 如何使用多重处理在python中每个核心运行一个进程 - how to use multiprocessing to run one process per core in python 如何使用 Python 在 Selenium 中同时运行多个 webdriver 进程? - How can I run multiple webdriver processes concurrently in Selenium using Python? 根据Python中的CPU使用情况,更改要生成的进程数 - Change the number of processes to spawn depending on the CPU usage in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM