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 diff --git a/loky/backend/context.py b/loky/backend/context.py index d0f59031..f97adcec 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: + # 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( + ( + "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]) elif sys.platform == "darwin": cpu_info = subprocess.run( "sysctl -n hw.physicalcpu".split(),