From b423ddc87c1ef0ea0f383f0fdde3c1f011c4dc0b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 8 Nov 2014 18:13:15 +0530 Subject: [PATCH] Cache results of CPU count detection --- src/calibre/__init__.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/calibre/__init__.py b/src/calibre/__init__.py index 81c8027bb4..d13541f151 100644 --- a/src/calibre/__init__.py +++ b/src/calibre/__init__.py @@ -469,20 +469,24 @@ class CurrentDir(object): pass +_ncpus = None def detect_ncpus(): """Detects the number of effective CPUs in the system""" - if iswindows: - import win32api - ans = win32api.GetSystemInfo()[5] - else: - import multiprocessing - ans = -1 - try: - ans = multiprocessing.cpu_count() - except Exception: - from PyQt5.Qt import QThread - ans = QThread.idealThreadCount() - return max(1, ans) + global _ncpus + if _ncpus is None: + if iswindows: + import win32api + ans = win32api.GetSystemInfo()[5] + else: + import multiprocessing + ans = -1 + try: + ans = multiprocessing.cpu_count() + except Exception: + from PyQt5.Qt import QThread + ans = QThread.idealThreadCount() + _ncpus = max(1, ans) + return _ncpus relpath = os.path.relpath