Reader
import sqlite3 from time import sleep conn = sqlite3.connect('example.db', isolation_level=None) c = conn.cursor() while True: c.execute("SELECT * FROM statistics") try: print '**' print c.fetchone() except: pass sleep(3)
Writer
import sqlite3 from time import sleep import os if os.path.exists('example.db'): os.remove('example.db') conn = sqlite3.connect('example.db', isolation_level=None) c = conn.cursor() c.execute('PRAGMA journal_mode=wal') print c.fetchone() c.execute("CREATE TABLE statistics (stat1 real, stat2 real)") stat1 = 0 stat2 = 0.0 while True: stat1 += 1 stat2 += 0.1 cmd = "INSERT INTO statistics VALUES (%d,%f)" % (stat1, stat2) print cmd c.execute(cmd) #conn.commit() c.execute("PRAGMA wal_checkpoint=FULL") sleep(0.25)
References
https://stackoverflow.com/questions/30821179/sqlite-wal-mode-in-python-concurrency-with-one-writer-and-one-reader
https://sqlite.org/wal.html