@@ -242,33 +242,11 @@ def _count_physical_cores():
242
242
# Not cached yet, find it
243
243
try :
244
244
if sys .platform == "linux" :
245
- cpu_info = subprocess .run (
246
- "lscpu --parse=core" .split (), capture_output = True , text = True
247
- )
248
- cpu_info = cpu_info .stdout .splitlines ()
249
- cpu_info = {line for line in cpu_info if not line .startswith ("#" )}
250
- cpu_count_physical = len (cpu_info )
245
+ cpu_count_physical = _count_physical_cores_linux ()
251
246
elif sys .platform == "win32" :
252
- cpu_info = subprocess .run (
253
- "wmic CPU Get NumberOfCores /Format:csv" .split (),
254
- capture_output = True ,
255
- text = True ,
256
- )
257
- cpu_info = cpu_info .stdout .splitlines ()
258
- cpu_info = [
259
- l .split ("," )[1 ]
260
- for l in cpu_info
261
- if (l and l != "Node,NumberOfCores" )
262
- ]
263
- cpu_count_physical = sum (map (int , cpu_info ))
247
+ cpu_count_physical = _count_physical_cores_win32 ()
264
248
elif sys .platform == "darwin" :
265
- cpu_info = subprocess .run (
266
- "sysctl -n hw.physicalcpu" .split (),
267
- capture_output = True ,
268
- text = True ,
269
- )
270
- cpu_info = cpu_info .stdout
271
- cpu_count_physical = int (cpu_info )
249
+ cpu_count_physical = _count_physical_cores_darwin ()
272
250
else :
273
251
raise NotImplementedError (f"unsupported platform: { sys .platform } " )
274
252
@@ -286,6 +264,60 @@ def _count_physical_cores():
286
264
return cpu_count_physical , exception
287
265
288
266
267
+ def _count_physical_cores_linux ():
268
+ try :
269
+ cpu_info = subprocess .run (
270
+ "lscpu --parse=core" .split (), capture_output = True , text = True
271
+ )
272
+ cpu_info = cpu_info .stdout .splitlines ()
273
+ cpu_info = {line for line in cpu_info if not line .startswith ("#" )}
274
+ return len (cpu_info )
275
+ except :
276
+ pass # fallback to /proc/cpuinfo
277
+
278
+ cpu_info = subprocess .run (
279
+ "cat /proc/cpuinfo" .split (), capture_output = True , text = True
280
+ )
281
+ cpu_info = cpu_info .stdout .splitlines ()
282
+ cpu_info = {line for line in cpu_info if line .startswith ("core id" )}
283
+ return len (cpu_info )
284
+
285
+
286
+ def _count_physical_cores_win32 ():
287
+ try :
288
+ cmd = "-Command (Get-CimInstance -ClassName Win32_Processor).NumberOfCores"
289
+ cpu_info = subprocess .run (
290
+ f"powershell.exe { cmd } " .split (),
291
+ capture_output = True ,
292
+ text = True ,
293
+ )
294
+ cpu_info = cpu_info .stdout .splitlines ()
295
+ return int (cpu_info [0 ])
296
+ except :
297
+ pass # fallback to wmic (older Windows versions; deprecated now)
298
+
299
+ cpu_info = subprocess .run (
300
+ "wmic CPU Get NumberOfCores /Format:csv" .split (),
301
+ capture_output = True ,
302
+ text = True ,
303
+ )
304
+ cpu_info = cpu_info .stdout .splitlines ()
305
+ cpu_info = [
306
+ l .split ("," )[1 ] for l in cpu_info if (l and l != "Node,NumberOfCores" )
307
+ ]
308
+ return sum (map (int , cpu_info ))
309
+
310
+
311
+ def _count_physical_cores_darwin ():
312
+ cpu_info = subprocess .run (
313
+ "sysctl -n hw.physicalcpu" .split (),
314
+ capture_output = True ,
315
+ text = True ,
316
+ )
317
+ cpu_info = cpu_info .stdout
318
+ return int (cpu_info )
319
+
320
+
289
321
class LokyContext (BaseContext ):
290
322
"""Context relying on the LokyProcess."""
291
323
0 commit comments