From 32c813eecebb758550f0f7d548186dbc03360e01 Mon Sep 17 00:00:00 2001 From: ngosang Date: Mon, 20 Jul 2015 03:56:54 +0200 Subject: [PATCH] [search engine] Fix cpu_count in old Python versions --- src/searchengine/nova/nova2.py | 9 +++++++-- src/searchengine/nova3/nova2.py | 13 +++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/searchengine/nova/nova2.py b/src/searchengine/nova/nova2.py index f2303c954..15e4c7f13 100644 --- a/src/searchengine/nova/nova2.py +++ b/src/searchengine/nova/nova2.py @@ -1,4 +1,4 @@ -#VERSION: 1.40 +#VERSION: 1.41 # Author: # Fabien Devaux @@ -41,6 +41,11 @@ from multiprocessing import Pool, cpu_count from fix_encoding import fix_encoding THREADED = True +try: + MAX_THREADS = cpu_count() +except NotImplementedError: + MAX_THREADS = 1 + CATEGORIES = {'all', 'movies', 'tv', 'music', 'games', 'anime', 'software', 'pictures', 'books'} ################################################################################ @@ -170,7 +175,7 @@ def main(args): if THREADED: #child process spawning is controlled min(number of searches, number of cpu) - pool = Pool(min(len(engines_list), cpu_count())) + pool = Pool(min(len(engines_list), MAX_THREADS)) pool.map(run_search, ([globals()[engine], what, cat] for engine in engines_list)) else: map(run_search, ([globals()[engine], what, cat] for engine in engines_list)) diff --git a/src/searchengine/nova3/nova2.py b/src/searchengine/nova3/nova2.py index dc16239a5..78ed5a615 100644 --- a/src/searchengine/nova3/nova2.py +++ b/src/searchengine/nova3/nova2.py @@ -1,4 +1,4 @@ -#VERSION: 1.40 +#VERSION: 1.41 # Author: # Fabien Devaux @@ -34,12 +34,17 @@ # POSSIBILITY OF SUCH DAMAGE. import urllib.parse -from os import path, cpu_count +from os import path from glob import glob from sys import argv -from multiprocessing import Pool +from multiprocessing import Pool, cpu_count THREADED = True +try: + MAX_THREADS = cpu_count() +except NotImplementedError: + MAX_THREADS = 1 + CATEGORIES = {'all', 'movies', 'tv', 'music', 'games', 'anime', 'software', 'pictures', 'books'} ################################################################################ @@ -168,7 +173,7 @@ def main(args): what = urllib.parse.quote(' '.join(args[2:])) if THREADED: #child process spawning is controlled min(number of searches, number of cpu) - with Pool(min(len(engines_list), cpu_count())) as pool: + with Pool(min(len(engines_list), MAX_THREADS)) as pool: pool.map(run_search, ([globals()[engine], what, cat] for engine in engines_list)) else: #py3 note: map is needed to be evaluated for content to be executed