Reputation: 10300
Thanks to this post, I am trying out the asynchronous driver tornado-redis
. From the demo (Github example), it shows that how to do asynchronous gets using this library, but it is not clear to me that if the sets are asynchronous too (they do have a callback function).
So if possible, what is the correct way of doing asynchronous writes using tornado-redis
? Would it be something like this:
@tornado.web.asynchronous
@tornado.gen.engine
def post(self):
...
yield tornado.gen.Task(t.set,'key', 'value')
Upvotes: 2
Views: 3455
Reputation: 9246
Look at the tornado-redis
code. Client.set
method has following defenition:
def set(self, key, value, callback=None):
self.execute_command('SET', key, value, callback=callback)
So yes, it takes callback and can be used with gen.Task
.
Correct ways to write asynchronously:
gen
. Example from github:
c = tornadoredis.Client()
c.connect()
def on_set(result):
log.debug("set result: %s" % result)
c.set('foo', 'Lorem ipsum #1', on_set)
c.set('bar', 'Lorem ipsum #2', on_set)
c.set('zar', 'Lorem ipsum #3', on_set)
Separate class with gen module:
class MyRedisWrapper(object):
@gen.engine
def set(self, key, value):
yield tornado.gen.Task(t.set, key, value)
r = MyRedisWrapper()
class MyHandler(tornado.web.RequestHandler):
def get(self):
r.set('key', 'value') #It will work, but not sure about efficiency.
Upvotes: 3