This may sound a bit weird, but is it possible that Tornado completes executing a future after wrapping it in a timeout?
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 scenario, future
does not complete before timeout but I want it continue executing whatever callable it has.
To put it in a different way, I want to be able to use this along with gen.WaitIterator
to get results of a set of futures, like described in the document:
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
.
This is exactly what I'm looking for, I want result of each future ASAP because I have some tasks that take longer than others, but with one exception: those slow tasks should continue producing results so I can access them later.
Is this even possible?
with_timeout
does not cancel the underlying 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 WaitIterator
is a little tricky since you must not call WaitIterator.next
again until the previous one completes.
Also consider the Queue classes introduced in Tornado 4.2. These can often produce cleaner code than WaitIterator (and they have built-in timeout support instead of a with_timeout
wrapper).