Skip to content

Instantly share code, notes, and snippets.

@rmax
Created August 30, 2010 00:01
Show Gist options
  • Save rmax/556836 to your computer and use it in GitHub Desktop.
Save rmax/556836 to your computer and use it in GitHub Desktop.
import oursql
import sys
from twisted.enterprise import adbapi
from twisted.internet import defer, reactor
from twisted.python import log
log.startLogging(sys.stderr)
# Use 'test' database with default credentials
dbpool = adbapi.ConnectionPool("oursql", db="test")
def createTable():
"""Re-creates t1 every call"""
def _create(tx):
tx.execute("DROP TABLE IF EXISTS t1")
tx.execute("CREATE TABLE t1 (name VARCHAR(30))")
return dbpool.runInteraction(_create)
def echo(msg):
def _echo(_):
print msg
return _
return _echo
def runQueries():
# schedule many queries in parallel
queries = []
for i in xrange(10):
d1 = dbpool.runOperation("INSERT INTO t1 VALUES (?)", (i, ))
d1.addCallbacks(echo("record inserted (%d)" % i), printError)
queries.append(d1)
d2 = dbpool.runQuery("SELECT * FROM t1")
d2.addCallbacks(printLength, printError)
queries.append(d2)
return defer.DeferredList(queries)
def main():
create = createTable()
create.addCallbacks(echo("Table Created"), printError)
# be sure to run queries after table gets created
# otherwise we will get OperationalErrors and even segmentation fault (?)
create.addCallback(lambda _: runQueries())
return create
def printLength(result):
if result:
print "Got %d rows" % len(result)
else:
print "Got nothing"
def printError(result):
print "Error", result
def stop(_):
print "stopping reactor"
reactor.stop()
if __name__ == '__main__':
print "starting program"
d = main()
d.addBoth(stop)
print "starting reactor"
reactor.run()
print "reactor stopped"
2010-08-29 20:02:17-0400 [-] Log opened.
2010-08-29 20:02:17-0400 [-] starting program
2010-08-29 20:02:17-0400 [-] starting reactor
2010-08-29 20:02:17-0400 [-] Table Created
2010-08-29 20:02:17-0400 [-] record inserted (0)
2010-08-29 20:02:17-0400 [-] Got 1 rows
2010-08-29 20:02:17-0400 [-] record inserted (1)
2010-08-29 20:02:17-0400 [-] record inserted (2)
2010-08-29 20:02:17-0400 [-] Got nothing
2010-08-29 20:02:17-0400 [-] Got 3 rows
2010-08-29 20:02:17-0400 [-] record inserted (3)
2010-08-29 20:02:17-0400 [-] record inserted (5)
2010-08-29 20:02:17-0400 [-] Got 4 rows
2010-08-29 20:02:17-0400 [-] Got 4 rows
2010-08-29 20:02:17-0400 [-] record inserted (6)
2010-08-29 20:02:17-0400 [-] Got 5 rows
2010-08-29 20:02:17-0400 [-] Got 6 rows
2010-08-29 20:02:17-0400 [-] Got 6 rows
2010-08-29 20:02:17-0400 [-] record inserted (8)
2010-08-29 20:02:17-0400 [-] record inserted (4)
2010-08-29 20:02:17-0400 [-] record inserted (7)
2010-08-29 20:02:17-0400 [-] Got 9 rows
2010-08-29 20:02:17-0400 [-] record inserted (9)
2010-08-29 20:02:17-0400 [-] Got 10 rows
2010-08-29 20:02:17-0400 [-] stopping reactor
2010-08-29 20:02:17-0400 [-] Main loop terminated.
2010-08-29 20:02:17-0400 [-] reactor stopped
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment