从同步处理程序更改为异步处理程序时没有任何改进

There is no improvement when changing from sync to async handler

我刚刚比较了同步和异步版本处理程序, 'response time' 等指标没有改进,那么在这种情况下如何调试?

处理程序

@gen.coroutine
def get(self):
    calendar = Calendar()
    response = yield (calendar.events())
    self.write(response)
    self.finish()

型号

class Calendar(object):
    @return_future
    def events(self, callback=None):
        result = # MySQLDB operation
        callback(result)

基本信息:

CPU: 1 core
Dababase: MySQLDB

@return_future 在这里没有任何意义。它用于将已经异步的函数(并使用回调)改编成可以从协程中产生的东西。几乎没有理由在现代 Tornado 应用程序中使用它;只需在任何地方使用 @gen.coroutine

要在不阻塞 IOLoop 的情况下 运行 执行 MySQLdb 操作,您必须 运行 在单独的线程上执行它。安装 futures 包并创建一个 ThreadPoolExecutor:

executor = ThreadPoolExecutor(4)

class Calendar:
    @gen.coroutine
    def events(self):
        result = yield executor.submit(mysqldb_operation)
        raise gen.Return(result)