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