【发布时间】: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 客户端永远得不到响应?
【问题讨论】: