python - Tornado adbapi callback not running -
i'm afraid i'm finding difficult work adbapi interface sqlite3 connectionpools in twisted.
i've initialized pool in file i've named db.py:
from twisted.enterprise import adbapi pool = adbapi.connectionpool("sqlite3", db=config.db_file) pool.start() def last(datatype, n): cmd = "select * %s order timestamp desc limit %i" % (datatype, n) return pool.runquery(cmd)
then, i'm importing db.py , using inside particular route handler. unfortunately, appears callback never triggered. datatype
printed, response
never printed.
class datahandler(tornado.web.requesthandler): @tornado.web.asynchronous def get(self, datatype): print datatype data = db.last(datatype, 500) data.addcallback(self.on_response) def on_response(self, response): print response self.write(json.dumps(response)) self.finish()
any ideas?
mixing tornado , twisted requires special attention. try this, first lines executed in entire program:
import tornado.platform.twisted tornado.platform.twisted.install()
then, start server:
tornado.ioloop.ioloop.current().start()
what's happening is, start tornado ioloop never start twisted reactor. twisted sqlite connection begins io operation when run query, since reactor isn't running, operation never completes. in order ioloop , reactor share process must run 1 of them on top of other. tornado provides compatibility layer allows that.
Comments
Post a Comment