Future Tornado Result After Timeout

This may sound a little odd, but is it possible that Tornado will terminate future execution after it times out?

So something like this:

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

      

So in this case future

it won't finish before timeout, but I want it to continue executing any callable.

To put it another way, I want to use this together with gen.WaitIterator

to get the results of a set of futures, as described in the doc:

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 are producing errors, you can use WaitIterator

.

This is exactly what I am looking for, I want the result of every future as soon as possible because I have some tasks that take longer than others, but with one exception: these slow tasks must continue to produce results so that I can get access them later.

Is it possible?

+3


source to share


1 answer


with_timeout

does not override the base one Future

, so it can be reused:

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

      



Combining this with is a WaitIterator

bit tricky, since you don't have to call WaitIterator.next

again until the previous one completes.

Also consider the Queue classes introduced in Tornado 4.2. They can often produce cleaner code than WaitIterator (and they have built-in support for timeouts instead of wrappers with_timeout

).

+2


source







All Articles