##// END OF EJS Templates
Add locks for input and output caches.
Thomas Kluyver -
Show More
@@ -22,6 +22,7 b' import sqlite3'
22 import threading
22 import threading
23
23
24 from collections import defaultdict
24 from collections import defaultdict
25 from contextlib import nested
25
26
26 # Our own packages
27 # Our own packages
27 from IPython.config.configurable import Configurable
28 from IPython.config.configurable import Configurable
@@ -127,6 +128,8 b' class HistoryManager(Configurable):'
127 raise
128 raise
128
129
129 self.save_flag = threading.Event()
130 self.save_flag = threading.Event()
131 self.db_input_cache_lock = threading.Lock()
132 self.db_output_cache_lock = threading.Lock()
130 self.save_thread = HistorySavingThread(self)
133 self.save_thread = HistorySavingThread(self)
131 self.save_thread.start()
134 self.save_thread.start()
132
135
@@ -383,10 +386,11 b' class HistoryManager(Configurable):'
383 self.input_hist_parsed.append(source)
386 self.input_hist_parsed.append(source)
384 self.input_hist_raw.append(source_raw)
387 self.input_hist_raw.append(source_raw)
385
388
386 self.db_input_cache.append((line_num, source, source_raw))
389 with self.db_input_cache_lock:
387 # Trigger to flush cache and write to DB.
390 self.db_input_cache.append((line_num, source, source_raw))
388 if len(self.db_input_cache) >= self.db_cache_size:
391 # Trigger to flush cache and write to DB.
389 self.save_flag.set()
392 if len(self.db_input_cache) >= self.db_cache_size:
393 self.save_flag.set()
390
394
391 # update the auto _i variables
395 # update the auto _i variables
392 self._iii = self._ii
396 self._iii = self._ii
@@ -416,7 +420,8 b' class HistoryManager(Configurable):'
416 return
420 return
417 output = json.dumps(self.output_hist_reprs[line_num])
421 output = json.dumps(self.output_hist_reprs[line_num])
418
422
419 self.db_output_cache.append((line_num, output))
423 with self.db_output_cache_lock:
424 self.db_output_cache.append((line_num, output))
420 if self.db_cache_size <= 1:
425 if self.db_cache_size <= 1:
421 self.save_flag.set()
426 self.save_flag.set()
422
427
@@ -436,27 +441,30 b' class HistoryManager(Configurable):'
436 """Write any entries in the cache to the database."""
441 """Write any entries in the cache to the database."""
437 if conn is None:
442 if conn is None:
438 conn = self.db
443 conn = self.db
439 try:
444
440 self._writeout_input_cache(conn)
445 with self.db_input_cache_lock:
441 except sqlite3.IntegrityError:
446 try:
442 self.new_session()
443 print("ERROR! Session/line number was not unique in",
444 "database. History logging moved to new session",
445 self.session_number)
446 try: # Try writing to the new session. If this fails, don't recurse
447 self._writeout_input_cache(conn)
447 self._writeout_input_cache(conn)
448 except sqlite3.IntegrityError:
448 except sqlite3.IntegrityError:
449 pass
449 self.new_session()
450 finally:
450 print("ERROR! Session/line number was not unique in",
451 self.db_input_cache = []
451 "database. History logging moved to new session",
452
452 self.session_number)
453 try:
453 try: # Try writing to the new session. If this fails, don't recurse
454 self._writeout_output_cache(conn)
454 self._writeout_input_cache(conn)
455 except sqlite3.IntegrityError:
455 except sqlite3.IntegrityError:
456 print("!! Session/line number for output was not unique",
456 pass
457 "in database. Output will not be stored.")
457 finally:
458 finally:
458 self.db_input_cache = []
459 self.db_output_cache = []
459
460 with self.db_output_cache_lock:
461 try:
462 self._writeout_output_cache(conn)
463 except sqlite3.IntegrityError:
464 print("!! Session/line number for output was not unique",
465 "in database. Output will not be stored.")
466 finally:
467 self.db_output_cache = []
460
468
461
469
462 class HistorySavingThread(threading.Thread):
470 class HistorySavingThread(threading.Thread):
General Comments 0
You need to be logged in to leave comments. Login now