(self)
| 1142 | |
| 1143 | @only_when_enabled |
| 1144 | def run(self) -> None: |
| 1145 | atexit.register(self.stop) |
| 1146 | # We need a separate db connection per thread: |
| 1147 | try: |
| 1148 | hm: ReferenceType[HistoryManager] |
| 1149 | with hold(self.history_manager) as hm: |
| 1150 | if hm() is not None: |
| 1151 | self.db = sqlite3.connect( |
| 1152 | str(hm().hist_file), # type: ignore [union-attr] |
| 1153 | **cast(dict[str, t.Any], hm().connection_options), # type: ignore [union-attr] |
| 1154 | ) |
| 1155 | while True: |
| 1156 | self.save_flag.wait() |
| 1157 | with hold(self.history_manager) as hm: |
| 1158 | if hm() is None: |
| 1159 | self._stop_now = True |
| 1160 | if self._stop_now: |
| 1161 | self.db.close() |
| 1162 | return |
| 1163 | self.save_flag.clear() |
| 1164 | if hm() is not None: |
| 1165 | hm().writeout_cache(self.db) # type: ignore [union-attr] |
| 1166 | |
| 1167 | except Exception as e: |
| 1168 | print( |
| 1169 | ( |
| 1170 | "The history saving thread hit an unexpected error (%s)." |
| 1171 | "History will not be written to the database." |
| 1172 | ) |
| 1173 | % repr(e) |
| 1174 | ) |
| 1175 | finally: |
| 1176 | atexit.unregister(self.stop) |
| 1177 | |
| 1178 | def stop(self) -> None: |
| 1179 | """This can be called from the main thread to safely stop this thread. |
no test coverage detected