Permalink
Browse files

Implemented autosave.

  • Loading branch information...
1 parent 3498382 commit 46b78b3f4c5baf5704296c875e0e7f3d33d0c08e Mani chandra committed Dec 18, 2010
Showing with 28 additions and 1 deletion.
  1. +21 −1 IPython/core/interactiveshell.py
  2. +7 −0 IPython/frontend/terminal/interactiveshell.py
@@ -27,6 +27,8 @@
import sys
import tempfile
import types
+import threading
+import time
from contextlib import nested
from IPython.config.configurable import Configurable
@@ -129,6 +131,23 @@ def validate(self, obj, value):
class MultipleInstanceError(Exception):
pass
+class HistorySaveThread(threading.Thread):
+ """Thread to save history periodically"""
+
+ def __init__(self, IPython_object, time_interval, exit_now):
+ threading.Thread.__init__(self)
+ self.IPython_object = IPython_object
+ self.time_interval = time_interval
+ self.exit_now = exit_now
+
+ def run(self):
+ while 1:
+ if self.exit_now==True:
+ break
+ time.sleep(self.time_interval)
+ #printing for debug
+ #print "Saving..."
+ self.IPython_object.save_history()
#-----------------------------------------------------------------------------
# Main IPython class
@@ -293,6 +312,8 @@ def __init__(self, config=None, ipython_dir=None,
self.init_payload()
self.hooks.late_startup_hook()
atexit.register(self.atexit_operations)
+ self.history_thread = HistorySaveThread(self, 1, False)
+ self.history_thread.start()
# While we're trying to have each part of the code directly access what it
# needs without keeping redundant references to objects, we have too much
@@ -2523,7 +2544,6 @@ def atexit_operations(self):
except OSError:
pass
-
self.save_history()
# Clear all user namespaces to release all references cleanly.
@@ -24,6 +24,7 @@
from IPython.core.error import TryNext
from IPython.core.usage import interactive_usage, default_banner
from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
+from IPython.core.interactiveshell import HistorySaveThread
from IPython.lib.inputhook import enable_gui
from IPython.lib.pylabtools import pylab_activate
from IPython.utils.terminal import toggle_set_term_title, set_term_title
@@ -496,9 +497,15 @@ def exit(self):
"""Handle interactive exit.
This method calls the ask_exit callback."""
+ self.shell.history_thread.exit_now=True
+ self.shell.history_thread.join()
if self.confirm_exit:
if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
self.ask_exit()
+ else:
+ self.shell.history_thread = HistorySaveThread(self.shell, 1,
+ False)
+ self.shell.history_thread.start()
else:
self.ask_exit()

0 comments on commit 46b78b3

Please sign in to comment.