Question

I'm afraid I'm finding it difficult to work with the adbapi interface for sqlite3 ConnectionPools in twisted.

I've initialized my pool like this in a 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 * FROM %s ORDER BY Timestamp DESC LIMIT %i" % (datatype, n)
    return pool.runQuery(cmd)

Then, I'm importing db.py and using it inside a particular route handler. Unfortunately, it appears the callback is never triggered. datatype is printed, but response is 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?

Was it helpful?

Solution

Mixing Tornado and Twisted requires special attention. Try this, as the first lines executed in your entire program:

import tornado.platform.twisted
tornado.platform.twisted.install()

Then, to start your server:

tornado.ioloop.IOLoop.current().start()

What's happening now is, you start the Tornado IOLoop but you never start the Twisted Reactor. Your Twisted SQLite connection begins an IO operation when you run your query, but since the Reactor isn't running, the operation never completes. In order for the IOLoop and the Reactor to share your process you must run one of them on top of the other. Tornado provides a compatibility layer that allows you to do that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top