From 6ec88e3877b2eb97b7a2cb7547006ea67677f3cd 2010-12-20 19:50:03
From: Mani chandra <mchandra@iitk.ac.in>
Date: 2010-12-20 19:50:03
Subject: [PATCH] Changed autosave code.

---

diff --git a/IPython/core/history.py b/IPython/core/history.py
index 41a9dfc..6c80f44 100644
--- a/IPython/core/history.py
+++ b/IPython/core/history.py
@@ -17,6 +17,8 @@ import fnmatch
 import json
 import os
 import sys
+import threading
+import time
 
 # Our own packages
 import IPython.utils.io
@@ -251,6 +253,23 @@ class HistoryManager(object):
         # The directory history can't be completely empty
         self.dir_hist[:] = [os.getcwd()]
 
+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()
 
 def magic_history(self, parameter_s = ''):
     """Print input history (_i<n> variables), with most recent last.
diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py
index db18a1c..05961bd 100644
--- a/IPython/core/interactiveshell.py
+++ b/IPython/core/interactiveshell.py
@@ -27,8 +27,6 @@ import re
 import sys
 import tempfile
 import types
-import threading
-import time
 from contextlib import nested
 
 from IPython.config.configurable import Configurable
@@ -47,6 +45,7 @@ from IPython.core.error import TryNext, UsageError
 from IPython.core.extensions import ExtensionManager
 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
 from IPython.core.history import HistoryManager
+from IPython.core.history import HistorySaveThread
 from IPython.core.inputsplitter import IPythonInputSplitter
 from IPython.core.logger import Logger
 from IPython.core.magic import Magic
@@ -131,23 +130,6 @@ class SeparateStr(Str):
 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
@@ -312,7 +294,7 @@ class InteractiveShell(Configurable, Magic):
         self.init_payload()
         self.hooks.late_startup_hook()
         atexit.register(self.atexit_operations)
-        self.history_thread = HistorySaveThread(self, 1, False)
+        self.history_thread = HistorySaveThread(self, 10, False)
         self.history_thread.start()
 
     # While we're trying to have each part of the code directly access what it
diff --git a/IPython/frontend/terminal/interactiveshell.py b/IPython/frontend/terminal/interactiveshell.py
index 4f56f81..de170a2 100644
--- a/IPython/frontend/terminal/interactiveshell.py
+++ b/IPython/frontend/terminal/interactiveshell.py
@@ -24,7 +24,7 @@ import sys
 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.core.history 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
@@ -497,16 +497,13 @@ class TerminalInteractiveShell(InteractiveShell):
         """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.shell.history_thread.exit_now=True
                 self.ask_exit()
-            else:
-                self.shell.history_thread = HistorySaveThread(self.shell, 1,
-                        False)
-                self.shell.history_thread.start()
         else:
+            self.shell.history_thread = HistorySaveThread(self.shell, 10, False)
+            self.shell.history_thread.start()
             self.ask_exit()
             
     #------------------------------------------------------------------------