Browse Source

[enh] add init function to engines which loads parallel

Adam Tauber 7 years ago
parent
commit
78365ffb8a
3 changed files with 17 additions and 4 deletions
  1. 10
    1
      searx/engines/__init__.py
  2. 5
    2
      searx/engines/soundcloud.py
  3. 2
    1
      searx/engines/wolframalpha_noapi.py

+ 10
- 1
searx/engines/__init__.py View File

@@ -16,8 +16,9 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
16 16
 (C) 2013- by Adam Tauber, <asciimoo@gmail.com>
17 17
 '''
18 18
 
19
-from os.path import realpath, dirname
20 19
 import sys
20
+import threading
21
+from os.path import realpath, dirname
21 22
 from flask_babel import gettext
22 23
 from operator import itemgetter
23 24
 from json import loads
@@ -84,6 +85,14 @@ def load_engine(engine_data):
84 85
     for engine_attr in dir(engine):
85 86
         if engine_attr.startswith('_'):
86 87
             continue
88
+        if engine_attr == 'init':
89
+            init_fn = getattr(engine, engine_attr)
90
+            def engine_init():
91
+                init_fn()
92
+                logger.debug('%s engine initialized', engine_data['name'])
93
+            logger.debug('Starting background initialization of %s engine', engine_data['name'])
94
+            threading.Thread(target=engine_init).start()
95
+            continue
87 96
         if getattr(engine, engine_attr) is None:
88 97
             logger.error('Missing engine config attribute: "{0}.{1}"'
89 98
                          .format(engine.name, engine_attr))

+ 5
- 2
searx/engines/soundcloud.py View File

@@ -41,6 +41,7 @@ embedded_url = '<iframe width="100%" height="166" ' +\
41 41
     'data-src="https://w.soundcloud.com/player/?url={uri}"></iframe>'
42 42
 
43 43
 cid_re = re.compile(r'client_id:"([^"]*)"', re.I | re.U)
44
+guest_client_id = ''
44 45
 
45 46
 
46 47
 def get_client_id():
@@ -63,8 +64,10 @@ def get_client_id():
63 64
     return ""
64 65
 
65 66
 
66
-# api-key
67
-guest_client_id = get_client_id()
67
+def init():
68
+    global guest_client_id
69
+    # api-key
70
+    guest_client_id = get_client_id()
68 71
 
69 72
 
70 73
 # do search-request

+ 2
- 1
searx/engines/wolframalpha_noapi.py View File

@@ -55,7 +55,8 @@ def obtain_token():
55 55
     return token
56 56
 
57 57
 
58
-obtain_token()
58
+def init():
59
+    obtain_token()
59 60
 
60 61
 
61 62
 # do search-request