如何缩短申请时间?

2024-09-28 17:04:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我举一个简单的例子:

c = tornadoredis.Client()
c.connect()

class SourceHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def get(self):
    pipe = c.pipeline(transactional=True)
    pipe.zadd( 'test1'), 1212, "test" )
    pipe.zadd( 'test2'), 1212, "test" )
    .....
    pipe.zadd( 'testN'), 1212, "test" )
    res = yield tornado.gen.Task(pipe.execute)
    self.set_header('Content-Type', 'text/html')
    self.render("template.html", title="result")

此请求的时间=N*zadd操作的时间。在

我能缩短这个请求的时间吗?在


Tags: testselfclientwebhtmlconnect时间tornado
1条回答
网友
1楼 · 发布于 2024-09-28 17:04:20

管道请求是一个事务性请求,它要求它内部的所有操作作为一个原子单元执行。语句-res = yield tornado.gen.Task(pipe.execute)将一直等到所有zadd语句执行完毕,直到它将执行返回给get(…)函数。在

减少执行时间的唯一方法是删除fire-and-forget模式中的gen.engine位,尽管您不再有任何响应信息。在

class SourceHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get(self):
        pipe = c.pipeline(transactional=True)
        pipe.zadd( 'test1'), 1212, "test" )
        pipe.zadd( 'test2'), 1212, "test" )
        ...
        pipe.execute()
        # of course you no longer have the response information...
        ...old code...

相关问题 更多 >