python - SQLite3 Request Hangs in Twisted -
i trying write script automatically update schema of database. however, reason twisted hangs during second request make on adbapi.connectionpool. here code:
update.py
import os import glob import imp twisted.internet import reactor twisted.enterprise import adbapi twisted.internet import defer @defer.inlinecallbacks def get_schema_version(conn): schema_exists = yield conn.runquery("select name sqlite_master type='table' , name='schema_meta';") defer.returnvalue(0) def add_schema_files(schemas): # finds , imports schema_*.py files list module_files = glob.glob(os.path.dirname(os.path.abspath(__file__)) + "/schema_*.py") mod in module_files: module_name = os.path.basename(os.path.splitext(mod)[0]) newmod = imp.load_source('%s'%module_name, mod) schemas.append( (module_name, newmod) ) @defer.inlinecallbacks def update_schema(conn): # update database schema latest version schema_version = yield get_schema_version(conn) print "at schema version %d" % schema_version schemas = [] add_schema_files(schemas) schemas = sorted(schemas, key=lambda tup: tup[0]) in range(schema_version, len(schemas)): # schemas[0] v1, schemas[1] v2, etc print "updating version %d" % (i+1) yield schemas[i][1].update(conn) if __name__ == '__main__': conn = adbapi.connectionpool("sqlite3", "data.db", check_same_thread=false) d = update_schema(conn) d.addcallback(exit) reactor.run()
schema_1.py
from twisted.internet import defer update_query = """ create table schema_meta ( version int not null ); insert schema_meta (version) values (1); """ @defer.inlinecallbacks def update(conn): yield conn.runquery(update_query)
it hangs on yield conn.runquery(update_query)
in schema_1.py.
in addition, when inturrupt script, following sqlite error:
sqlite3.warning: can execute 1 statement @ time.
sqlite allows run 1 writing query @ time. code asynchronous , new query can started before first 1 finished.
to handle have serialize queries or use database server instead of sqlite.
Comments
Post a Comment