A bit late, but I used tornado-redis . It works with tornado ioloop and the tornado.gen module
Install tornadoredis
It can be installed from pip
pip install tornadoredis
or using setuptools
easy_install tornadoredis
but you really shouldn't do that. You can also clone storage and retrieve it. Then run
python setup.py build python setup.py install
Connect to redis
The following code is in your main.py or equivalent
redis_conn = tornadoredis.Client('hostname', 'port') redis_conn.connect()
redis.connect is called only once. This is a blocking call, so it should be called before starting the main ioloop. The same connection object is shared by all handlers.
You can add it to your application settings, for example
settings = { redis = redis_conn } app = tornado.web.Application([('/.*', Handler),], **settings)
Use tornadoredis
A connection can be used in handlers in self.settings['redis'] or it can be added as a property of BaseHandler and a subclass of this class for other request handlers.
class BaseHandler(tornado.web.RequestHandler): @property def redis(): return self.settings['redis']
The tornado.web.asynchronous and tornado.gen.engine decorators are used to communicate with redis.
class SomeHandler(BaseHandler): @tornado.web.asynchronous @tornado.gen.engine def get(self): foo = yield gen.Task(self.redis.get, 'foo') self.render('sometemplate.html', {'foo': foo}
Additional Information
Additional examples and other functions, such as pooling and pipelines, can be found in the github registry.