(self, method, *args)
| 135 | raise Exception("Failed to connect to all URLs") |
| 136 | |
| 137 | def query_all(self, method, *args): |
| 138 | start_time = time.perf_counter() |
| 139 | futures = [ |
| 140 | self.executor.submit(getattr(client, method), *args) |
| 141 | for client in self.clients |
| 142 | ] |
| 143 | # Retrieve results and filter out 'Not Found!' |
| 144 | is_dict_return = method in [ |
| 145 | "get_all_relations_of_an_entity", |
| 146 | "get_tail_entities_given_head_and_relation", |
| 147 | ] |
| 148 | results = [f.result() for f in futures] |
| 149 | end_time = time.perf_counter() |
| 150 | # print(f"HTTP Queries took {end_time - start_time} seconds") |
| 151 | |
| 152 | start_time = time.perf_counter() |
| 153 | real_results = ( |
| 154 | set() if not is_dict_return else {"head": [], "tail": []} |
| 155 | ) |
| 156 | for res in results: |
| 157 | if isinstance(res, str) and res == "Not Found!": |
| 158 | continue |
| 159 | elif isinstance(res, tp.List): |
| 160 | if len(res) == 0: |
| 161 | continue |
| 162 | if isinstance(res[0], tp.List): |
| 163 | res_flattened = itertools.chain(*res) |
| 164 | real_results.update(res_flattened) |
| 165 | continue |
| 166 | real_results.update(res) |
| 167 | elif is_dict_return: |
| 168 | real_results["head"].extend(res["head"]) |
| 169 | real_results["tail"].extend(res["tail"]) |
| 170 | else: |
| 171 | real_results.add(res) |
| 172 | end_time = time.perf_counter() |
| 173 | # print(f"Querying all took {end_time - start_time} seconds") |
| 174 | |
| 175 | return real_results if len(real_results) > 0 else "Not Found!" |
| 176 | |
| 177 | |
| 178 | if __name__ == "__main__": |
no outgoing calls
no test coverage detected