Cache results of CPU count detection

This commit is contained in:
Kovid Goyal 2014-11-08 18:13:15 +05:30
parent 77ebeb6eea
commit b423ddc87c

View File

@ -469,20 +469,24 @@ class CurrentDir(object):
pass pass
_ncpus = None
def detect_ncpus(): def detect_ncpus():
"""Detects the number of effective CPUs in the system""" """Detects the number of effective CPUs in the system"""
if iswindows: global _ncpus
import win32api if _ncpus is None:
ans = win32api.GetSystemInfo()[5] if iswindows:
else: import win32api
import multiprocessing ans = win32api.GetSystemInfo()[5]
ans = -1 else:
try: import multiprocessing
ans = multiprocessing.cpu_count() ans = -1
except Exception: try:
from PyQt5.Qt import QThread ans = multiprocessing.cpu_count()
ans = QThread.idealThreadCount() except Exception:
return max(1, ans) from PyQt5.Qt import QThread
ans = QThread.idealThreadCount()
_ncpus = max(1, ans)
return _ncpus
relpath = os.path.relpath relpath = os.path.relpath