[pysqlite] Erratic behavior under high loads, with APSW code sample

"Frank McIngvale" <[email protected]> Mon, 30 Jun 2008 06:09:11 -0500
Newsgroups gmane.comp.python.db.pysqlite.user
Message-ID <[email protected]>
Hi, I'm having a strange problem with APSW & pysqlite that shows up under
higher loads. I've attached two files -- running them like this shows the
problem (this occurs under both APIs, I just wrote this sample for APSW):

$ rm -f test.db; python raw_apsw.py & python raw_apsw.py & python
raw_apsw.py
$ python r.py

This should show "ROWS=90" at the end. Under Windows (both XP and Vista)
this is rock-solid. Under Linux however, the results are erratic, and almost
always significantly less than 90. I've tried this under both Ubuntu 8.04
(linux-2.6.24, apsw 3.3.13, sqlite 3.4.2) and RedHat ES 4 (linux 2-6.9, apsw
3.5.9-r1, sqlite 3.5.9). As I understand it, SQLite's auto-transaction
feature should keep the INSERTs isolated, but I've also tried this same
sample using explicit BEGIN EXCLUSIVE/COMMIT/ROLLBACK, with the same
results.

Am I doing something wrong in my code? (I know I should do better than the
bare "except" statements, but I wanted to keep the sample minimal).

thanks,
Frank

_______________________________________________
list-pysqlite mailing list
list-pysqlite-FR6EJeJVuqdwc357pe9rcyQmJico6nz3epZhswDD4dQ@public.gmane.org
http://itsystementwicklung.de/cgi-bin/mailman/listinfo/list-pysqlite
raw_apsw.py (text/plain, 1017 B)
import apsw
import os
from threading import currentThread, Thread

NAME = 'test.db'

def run(nr):
	"each thread runs this ..."
	print "%d %s start" % (os.getpid(), currentThread().getName())
	
	conn = apsw.Connection(NAME)
	
	# create table to log writers
	while 1:
		qs = 'create table if not exists writers ('
		qs += 'id integer not null primary key autoincrement,'
		qs += 'pid integer,'
		qs += 'thread text,'
		qs += 'nr integer)'
		try:
			cur = conn.cursor()
			cur.execute(qs)
			break
		except:
			pass
		
	# write all my entries ...
	while nr > 0:
		try:
			cur = conn.cursor()
			
			# record writer
			qs = 'insert into writers (pid,thread,nr) values (?,?,?)'
			cur.execute(qs, (os.getpid(), currentThread().getName(), nr))
			
			nr -= 1
		except:
			pass
						
	conn.close()
	print "%d %s finished" % (os.getpid(), currentThread().getName())
	
NR_THREADS = 6
COUNTS_EACH = 5

ts = [Thread(target=run, args=(COUNTS_EACH,)) for i in range(NR_THREADS)]
for t in ts:
	t.start()
	
for t in ts:
	t.join()
r.py (text/plain, 178 B)
import apsw

conn = apsw.Connection('test.db')	
cur = conn.cursor()

res = cur.execute('select count(*) from writers')
nr = int(res.next()[0])
print "ROWS   =",nr

conn.close()