Definition of FetchHandlerMonitor class, it's for fetch handler.
| 163 | |
| 164 | |
| 165 | class FetchHandlerMonitor: |
| 166 | """ |
| 167 | Definition of FetchHandlerMonitor class, |
| 168 | it's for fetch handler. |
| 169 | """ |
| 170 | |
| 171 | def __init__(self, scope, handler): |
| 172 | self.fetch_instance = handler |
| 173 | self.fetch_thread = threading.Thread( |
| 174 | target=self.handler_launch_func, args=(scope, self.fetch_instance) |
| 175 | ) |
| 176 | self.running_lock = threading.Lock() |
| 177 | self.running = False |
| 178 | |
| 179 | def handler_launch_func(self, scope, handler): |
| 180 | fetch_instance = handler |
| 181 | period_secs = fetch_instance.period_secs |
| 182 | var_name_to_key = {} |
| 183 | for key in fetch_instance.var_dict: |
| 184 | if isinstance(fetch_instance.var_dict[key], Variable): |
| 185 | var_name_to_key[fetch_instance.var_dict[key].name] = key |
| 186 | else: |
| 187 | local_logger.warning(f"the value of {key} is not a Variable") |
| 188 | var_name_to_key["None.var"] = key |
| 189 | elapsed_secs = 0 |
| 190 | while True: |
| 191 | self.running_lock.acquire() |
| 192 | if self.running is False: |
| 193 | break |
| 194 | if elapsed_secs < period_secs: |
| 195 | # TODO(guru4elephant): needs customized condition |
| 196 | time.sleep(1) |
| 197 | elapsed_secs += 1 |
| 198 | else: |
| 199 | elapsed_secs = 0 |
| 200 | fetch_dict = {} |
| 201 | for key in var_name_to_key: |
| 202 | var = scope.find_var(key) |
| 203 | fetch_dict[key] = var |
| 204 | if var is None: |
| 205 | local_logger.warning( |
| 206 | f"{var_name_to_key[key]} value currently not available" |
| 207 | ) |
| 208 | res_dict = {} |
| 209 | for key in fetch_dict: |
| 210 | user_name = var_name_to_key[key] |
| 211 | if fetch_dict[key] is None: |
| 212 | res_dict[user_name] = None |
| 213 | continue |
| 214 | else: |
| 215 | res_dict[user_name] = fetch_dict[key].get_tensor() |
| 216 | |
| 217 | lod = res_dict[user_name].lod() |
| 218 | if len(lod) > 0: |
| 219 | raise RuntimeError( |
| 220 | "Some of your fetched tensors \ |
| 221 | hold LoD information. \ |
| 222 | They can not be completely cast \ |