diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 23f4fbf..db18a1c 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -27,6 +27,8 @@ import re import sys import tempfile import types +import threading +import time from contextlib import nested from IPython.config.configurable import Configurable @@ -129,6 +131,23 @@ 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 @@ -293,6 +312,8 @@ 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.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 @@ class InteractiveShell(Configurable, Magic): except OSError: pass - self.save_history() # Clear all user namespaces to release all references cleanly. diff --git a/IPython/frontend/terminal/interactiveshell.py b/IPython/frontend/terminal/interactiveshell.py index 671f82b..4f56f81 100644 --- a/IPython/frontend/terminal/interactiveshell.py +++ b/IPython/frontend/terminal/interactiveshell.py @@ -24,6 +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.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 @@ 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.ask_exit() + else: + self.shell.history_thread = HistorySaveThread(self.shell, 1, + False) + self.shell.history_thread.start() else: self.ask_exit()