From a77d449e7a3cada600c41a530e0e79feca54ac1a Mon Sep 17 00:00:00 2001 From: ChrisLoveringSendient Date: Tue, 19 Nov 2024 15:43:01 +0000 Subject: [PATCH 1/3] Use Get-WmiObject instead of wmic for counting cores on windows wmic is disabled by default on new versions of windows. See https://techcommunity.microsoft.com/blog/windows-itpro-blog/wmi-command-line-wmic-utility-deprecation-next-steps/4039242 --- loky/backend/context.py | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/loky/backend/context.py b/loky/backend/context.py index d0f59031..a1fc66a6 100644 --- a/loky/backend/context.py +++ b/loky/backend/context.py @@ -254,18 +254,32 @@ def _count_physical_cores(): cpu_info = {line for line in cpu_info if not line.startswith("#")} cpu_count_physical = len(cpu_info) elif sys.platform == "win32": - cpu_info = subprocess.run( - "wmic CPU Get NumberOfCores /Format:csv".split(), - capture_output=True, - text=True, - ) - cpu_info = cpu_info.stdout.splitlines() - cpu_info = [ - l.split(",")[1] - for l in cpu_info - if (l and l != "Node,NumberOfCores") - ] - cpu_count_physical = sum(map(int, cpu_info)) + try: + cpu_info = subprocess.run( + "wmic CPU Get NumberOfCores /Format:csv".split(), + capture_output=True, + text=True, + ) + except FileNotFoundError: + cpu_info = subprocess.run( + ( + "powershell Get-WmiObject " + "-Class 'win32_processor' -Property 'numberOfCores' | " + "Select-Object -ExpandProperty 'numberOfCores'" + ).split(), + capture_output=True, + text=True, + ) + cpu_info = cpu_info.stdout.splitlines() + cpu_count_physical = int(cpu_info[0]) + else: + cpu_info = cpu_info.stdout.splitlines() + cpu_info = [ + l.split(",")[1] + for l in cpu_info + if (l and l != "Node,NumberOfCores") + ] + cpu_count_physical = sum(map(int, cpu_info)) elif sys.platform == "darwin": cpu_info = subprocess.run( "sysctl -n hw.physicalcpu".split(), From c797a78c60a5e428a30d3bcbbb0bda7c9421b8fa Mon Sep 17 00:00:00 2001 From: ChrisLoveringSendient Date: Tue, 19 Nov 2024 15:46:45 +0000 Subject: [PATCH 2/3] Add CHANGES entry --- CHANGES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index c3c6a9c6..1524cead 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,8 @@ ### 3.5.0 - in development +- Use powershell's ``Get-WmiObject`` instead of ``wmic`` for counting cores on + windows if ``wmic`` could not be found. ``wmic`` is deprecated and not available + on new builds of Windows. (#423) ### 3.4.1 - 2023-06-29 From 171c797689dd57fef820bc5a26bc28c8b51f0627 Mon Sep 17 00:00:00 2001 From: ChrisLoveringSendient Date: Tue, 19 Nov 2024 16:13:15 +0000 Subject: [PATCH 3/3] Move all logic for getting windows cores into the try block, instead of split into an else block Co-authored-by: tommoral --- loky/backend/context.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/loky/backend/context.py b/loky/backend/context.py index a1fc66a6..f97adcec 100644 --- a/loky/backend/context.py +++ b/loky/backend/context.py @@ -255,11 +255,19 @@ def _count_physical_cores(): cpu_count_physical = len(cpu_info) elif sys.platform == "win32": try: + # Wmic is not available on recent windows version cpu_info = subprocess.run( "wmic CPU Get NumberOfCores /Format:csv".split(), capture_output=True, text=True, ) + cpu_info = cpu_info.stdout.splitlines() + cpu_info = [ + l.split(",")[1] + for l in cpu_info + if (l and l != "Node,NumberOfCores") + ] + cpu_count_physical = sum(map(int, cpu_info)) except FileNotFoundError: cpu_info = subprocess.run( ( @@ -272,14 +280,6 @@ def _count_physical_cores(): ) cpu_info = cpu_info.stdout.splitlines() cpu_count_physical = int(cpu_info[0]) - else: - cpu_info = cpu_info.stdout.splitlines() - cpu_info = [ - l.split(",")[1] - for l in cpu_info - if (l and l != "Node,NumberOfCores") - ] - cpu_count_physical = sum(map(int, cpu_info)) elif sys.platform == "darwin": cpu_info = subprocess.run( "sysctl -n hw.physicalcpu".split(),