MCPcopy Create free account
hub / github.com/ipython/ipython / HistorySavingThread

Class HistorySavingThread

IPython/core/history.py:805–847  ·  view source on GitHub ↗

This thread takes care of writing history to the database, so that the UI isn't held up while that happens. It waits for the HistoryManager's save_flag to be set, then writes out the history cache. The main thread is responsible for setting the flag when the cache size reaches a def

Source from the content-addressed store, hash-verified

803
804
805class HistorySavingThread(threading.Thread):
806 """This thread takes care of writing history to the database, so that
807 the UI isn't held up while that happens.
808
809 It waits for the HistoryManager's save_flag to be set, then writes out
810 the history cache. The main thread is responsible for setting the flag when
811 the cache size reaches a defined threshold."""
812 daemon = True
813 stop_now = False
814 enabled = True
815 def __init__(self, history_manager):
816 super(HistorySavingThread, self).__init__(name="IPythonHistorySavingThread")
817 self.history_manager = history_manager
818 self.enabled = history_manager.enabled
819 atexit.register(self.stop)
820
821 @needs_sqlite
822 def run(self):
823 # We need a separate db connection per thread:
824 try:
825 self.db = sqlite3.connect(self.history_manager.hist_file,
826 **self.history_manager.connection_options
827 )
828 while True:
829 self.history_manager.save_flag.wait()
830 if self.stop_now:
831 self.db.close()
832 return
833 self.history_manager.save_flag.clear()
834 self.history_manager.writeout_cache(self.db)
835 except Exception as e:
836 print(("The history saving thread hit an unexpected error (%s)."
837 "History will not be written to the database.") % repr(e))
838
839 def stop(self):
840 """This can be called from the main thread to safely stop this thread.
841
842 Note that it does not attempt to write out remaining history before
843 exiting. That should be done by calling the HistoryManager's
844 end_session method."""
845 self.stop_now = True
846 self.history_manager.save_flag.set()
847 self.join()
848
849
850# To match, e.g. ~5/8-~2/3

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected