![](/img/trans.png)
[英]How do I limit using one CPU per python processes launched via gnu parallel?
[英]How to determine the maximum number of processes that can be run per CPU core using python?
在 windows 機器上使用 python 3.8.8,如何確定每個 CPU 核心可以運行的最大進程數? 我關注了一些先前提出的相關問題,例如1 、 2 、 3 、 4 。
現在關於這個主題的 python 文檔建議使用len(os.sched_getaffinity(0))
。 但是,當我嘗試使用代碼時,它會返回錯誤,
import os
print(len(os.sched_getaffinity(0)))
AttributeError: module 'os' has no attribute 'sched_getaffinity'
然后我嘗試另一個代碼片段來識別可用的功能,
import os
print(dir(os))
它給了我一個函數列表,其中sched_getaffinity()
不可用。
我的環境是:
一個 CPU 內核可以同時運行的進程數等於每個 CPU 內核的線程數。 要在 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))
要檢查您的腳本可以在其上運行的不同處理器,您可以使用:
print(psutil.Process().cpu_affinity())
查看__init__.pyi
for os
庫中的函數定義
if sys.platform != "win32":
from posix import sched_param
...
def sched_getaffinity(pid: int) -> Set[int]: ... # some flavors of Unix
此功能不適用於 windows 系統。
您可以使用鏈接的答案中的另一個選項,例如os.cpu_count()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.