56 def search_external_bang(self):
58 Check if there is a external bang.
59 If yes, update self.result_container and return True
61 if self.search_query.external_bang:
62 self.result_container.redirect_url = get_bang_url(self.search_query)
64 # This means there was a valid bang and the
65 # rest of the search does not need to be continued
66 if isinstance(self.result_container.redirect_url, str):
70 def search_answerers(self):
72 Check if an answer return a result.
73 If yes, update self.result_container and return True
75 answerers_results = ask(self.search_query)
78 for results in answerers_results:
79 self.result_container.extend('answer', results)
84 def _get_requests(self):
88 # max of all selected engine timeout
91 # start search-request for all selected engines
92 for engineref in self.search_query.engineref_list:
93 processor = PROCESSORS[engineref.name]
95 # stop the request now if the engine is suspend
96 if processor.extend_container_if_suspended(self.result_container):
99 # set default request parameters
100 request_params = processor.get_params(self.search_query, engineref.category)
101 if request_params is None:
104 counter_inc('engine', engineref.name, 'search', 'count', 'sent')
106 # append request to list
107 requests.append((engineref.name, self.search_query.query, request_params))
109 # update default_timeout
110 default_timeout = max(default_timeout, processor.engine.timeout)
113 max_request_timeout = settings['outgoing']['max_request_timeout']
114 actual_timeout = default_timeout
115 query_timeout = self.search_query.timeout_limit
117 if max_request_timeout is None and query_timeout is None:
118 # No max, no user query: default_timeout
120 elif max_request_timeout is None and query_timeout is not None:
121 # No max, but user query: From user query except if above default
122 actual_timeout = min(default_timeout, query_timeout)
123 elif max_request_timeout is not None and query_timeout is None:
124 # Max, no user query: Default except if above max
125 actual_timeout = min(default_timeout, max_request_timeout)
126 elif max_request_timeout is not None and query_timeout is not None:
127 # Max & user query: From user query except if above max
128 actual_timeout = min(query_timeout, max_request_timeout)
131 "actual_timeout={0} (default_timeout={1}, ?timeout_limit={2}, max_request_timeout={3})".format(
132 actual_timeout, default_timeout, query_timeout, max_request_timeout
136 return requests, actual_timeout
138 def search_multiple_requests(self, requests):
139 # pylint: disable=protected-access
140 search_id = str(uuid4())
142 for engine_name, query, request_params in requests:
143 _search = copy_current_request_context(PROCESSORS[engine_name].search)
144 th = threading.Thread( # pylint: disable=invalid-name
146 args=(query, request_params, self.result_container, self.start_time, self.actual_timeout),
150 th._engine_name = engine_name
153 for th in threading.enumerate(): # pylint: disable=invalid-name
154 if th.name == search_id:
155 remaining_time = max(0.0, self.actual_timeout - (default_timer() - self.start_time))
156 th.join(remaining_time)
159 self.result_container.add_unresponsive_engine(th._engine_name, 'timeout')
160 PROCESSORS[th._engine_name].logger.error('engine timeout')
162 def search_standard(self):
164 Update self.result_container, self.actual_timeout
166 requests, self.actual_timeout = self._get_requests()
168 # send all search-request
170 self.search_multiple_requests(requests)
172 # return results, suggestions, answers and infoboxes
176 def search(self) -> ResultContainer:
177 self.start_time = default_timer()
178 if not self.search_external_bang():
179 if not self.search_answerers():
180 self.search_standard()
181 return self.result_container