From 78365ffb8a083ca3e3ea905db12bdac9f273f460 Mon Sep 17 00:00:00 2001 From: Adam Tauber Date: Tue, 6 Jun 2017 22:20:20 +0200 Subject: [PATCH] [enh] add init function to engines which loads parallel --- searx/engines/__init__.py | 11 ++++++++++- searx/engines/soundcloud.py | 7 +++++-- searx/engines/wolframalpha_noapi.py | 3 ++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/searx/engines/__init__.py b/searx/engines/__init__.py index 023ec409a..1810f1c50 100644 --- a/searx/engines/__init__.py +++ b/searx/engines/__init__.py @@ -16,8 +16,9 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >. (C) 2013- by Adam Tauber, ''' -from os.path import realpath, dirname import sys +import threading +from os.path import realpath, dirname from flask_babel import gettext from operator import itemgetter from json import loads @@ -84,6 +85,14 @@ def load_engine(engine_data): for engine_attr in dir(engine): if engine_attr.startswith('_'): continue + if engine_attr == 'init': + init_fn = getattr(engine, engine_attr) + def engine_init(): + init_fn() + logger.debug('%s engine initialized', engine_data['name']) + logger.debug('Starting background initialization of %s engine', engine_data['name']) + threading.Thread(target=engine_init).start() + continue if getattr(engine, engine_attr) is None: logger.error('Missing engine config attribute: "{0}.{1}"' .format(engine.name, engine_attr)) diff --git a/searx/engines/soundcloud.py b/searx/engines/soundcloud.py index 41b40da61..d59755e04 100644 --- a/searx/engines/soundcloud.py +++ b/searx/engines/soundcloud.py @@ -41,6 +41,7 @@ embedded_url = '' cid_re = re.compile(r'client_id:"([^"]*)"', re.I | re.U) +guest_client_id = '' def get_client_id(): @@ -63,8 +64,10 @@ def get_client_id(): return "" -# api-key -guest_client_id = get_client_id() +def init(): + global guest_client_id + # api-key + guest_client_id = get_client_id() # do search-request diff --git a/searx/engines/wolframalpha_noapi.py b/searx/engines/wolframalpha_noapi.py index 2a8642f92..2cbbc5adc 100644 --- a/searx/engines/wolframalpha_noapi.py +++ b/searx/engines/wolframalpha_noapi.py @@ -55,7 +55,8 @@ def obtain_token(): return token -obtain_token() +def init(): + obtain_token() # do search-request