0
让我们从一些例子开始。有图书馆tornado-redis以下是使用它的例子。 Python与龙卷风 - 类实现与太多异步
import tornadoredis
import tornado.web
import tornado.gen
c = tornadoredis.Client()
c.connect()
class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def get(self):
foo = yield tornado.gen.Task(c.get, 'foo')
bar = yield tornado.gen.Task(c.get, 'bar')
zar = yield tornado.gen.Task(c.get, 'zar')
self.set_header('Content-Type', 'text/html')
self.render("template.html", title="Simple demo", foo=foo, bar=bar, zar=zar)
而且一切都很简单。但我需要编写一个包装类。编码对我来说没有问题,但我认为我错了异步模式。
我的封装类应该异步调用redis类,对吧!现在,我还必须以处理程序可以用Task(异步)调用它的方式异步实现我的类吗?然后我会有两个异步位置。 保持Tronado异步并保持简单的正确方法是什么?
Handler --async call--> MyWrapper --async call--> tronado-redis
或
Handler --sync call--> MyWrapper --async call--> tronado-redis
或
Handler --async call--> MyWrapper --sync call--> tronado-redis