【问题标题】:Processing a long request in Tornado never finishes在 Tornado 中处理长请求永远不会完成
【发布时间】:2014-12-27 14:31:41
【问题描述】:

我有以下使用 Tornado 编写的 HTTP 服务器:

def reindex(index):
    # After some initialization, we execute a process and wait for its output
    result = subprocess.check_output([indexerBinPath, arg])

class ReindexRequestHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def post(self):
        reindexRequest = json.loads(self.request.body)
        p = self.application.settings.get('pool')
        p.apply_async(reindex, [ reindexRequest['IndexName'] ], callback = self.onIndexingFinished)         

    def onIndexingFinished(self, output):       
        self.flush()        
        self.finish()
        logger.info('Async callback: finished')

application = tornado.web.Application([ 
  (r"/reindex", ReindexRequestHandler)
], pool = Pool(8), queue = Queue())

if __name__ == "__main__":
    application.listen(8625)
    try:
        tornado.ioloop.IOLoop.instance().start()
    except KeyboardInterrupt:
        tornado.ioloop.IOLoop.instance().stop()

POST 处理程序中,我异步执行reindex 函数,该函数又启动一个进程并等待它完成。效果很好 - 该过程始终正确执行。该过程可能需要几分钟才能完成,具体取决于其参数。如果它在几秒钟内完成,一切正常。

但是,当它需要例如超过 3 分钟完成,发送 POST 请求的 HTTP 客户端永远不会得到答案。从服务器的角度来看,它看起来不错 - 我可以看到 Async callback:finished 已记录。但是,HTTP 客户端会无限期地等待响应(直到它因超时而失败)。我已经尝试过 Fiddler 的请求作曲家和 .NET HttpClient 类。

如果请求需要很长时间处理,为什么 HTTP 客户端永远得不到响应?

【问题讨论】:

    标签: python tornado


    【解决方案1】:

    我有一个类似的处理程序,self.finish() 将触发响应返回给客户端。因此,如果您将该行移到 p.apply_async 上方,它应该可以按您的意愿工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2020-09-26
      • 2018-04-29
      • 2014-06-17
      • 1970-01-01
      相关资源
      最近更新 更多