Skip to content

Instantly share code, notes, and snippets.

@tbnorth
Created August 1, 2019 14:21
Show Gist options
  • Save tbnorth/ace52f1c1a5fd2174ad0bc410eb2a11c to your computer and use it in GitHub Desktop.
Save tbnorth/ace52f1c1a5fd2174ad0bc410eb2a11c to your computer and use it in GitHub Desktop.
Test SQLite handling simultaneous connections
# A simple test of SQLite's ability to handle simultaneous connections.
# Result: no problem, DB content at end of test is correct.
# Note: by default, SQLite seems to sync. the disk on .commit(), so the
# test below will grind a physical disk for a long time. The DB path
# used below is a RAM disk, execution is acceptably fast on that.
import multiprocessing
import sqlite3
DB = "/home/tbrown/r/test.db"
def do_some(n):
con = sqlite3.connect(DB)
cur = con.cursor()
for i in range(n * 1000, n * 1000 + 10):
cur.execute("insert into test (comment) values (?)", ["At %d" % i])
con.commit()
con.close()
if __name__ == "__main__":
con = sqlite3.connect(DB)
cur = con.cursor()
cur.execute("create table test (test integer primary key, comment text)")
con.commit()
con.close()
with multiprocessing.Pool(16) as p:
p.map(do_some, range(1000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment