超时后龙卷风未来结果

Tornado future result after timeout

这可能听起来有点奇怪,但是 Tornado 是否有可能在超时包装后完成执行未来?

所以像这样:

try:
    result = yield gen.with_timeout(time.time() + 1, future)
except gen.TimeoutError as e:
    print('Timed out!')

所以在这种情况下,future 在超时之前没有完成,但我希望它继续执行它具有的任何可调用项。

换句话说,我希望能够将其与 gen.WaitIterator 一起使用以获得一组期货的结果,如文档中所述:

If you need to get the result of each future as soon as possible, or if you need the result of some futures even if others produce errors, you can use WaitIterator.

这正是我要找的,我希望每个未来尽快得到结果,因为我有一些任务比其他任务花费的时间更长,但有一个例外:那些缓慢的任务应该继续产生结果,以便我可以访问它们稍后。

这可能吗?

with_timeout不注销底层Future,所以可以重复使用:

future = do_something_async()
while True:
    try:
        result = yield gen.with_timeout(timedelta(seconds=1), future)
        break
    except gen.TimeoutError:
        print('tick')

将它与 WaitIterator 结合起来有点棘手,因为在前一个完成之前,您不能再次调用 WaitIterator.next

还要考虑 Tornado 4.2 中引入的队列 类。这些通常可以产生比 WaitIterator 更干净的代码(并且它们具有内置的超时支持而不是 with_timeout 包装器)。