Dave Hirschfeld Dave Hirschfeld - 10 months ago 91
Python Question

How to get the result of a future in a callback?


method was recently added to the distributed
object which allows you to take some action after the future finishes, irrespective of whether it succeeded or not.


The callback function will hang if you try to directly call any of the methods
on the passed future object.

The exception and traceback can however be accessed in the callback as follows:


Trying the same pattern with the result - i.e.
raises an exception:

File "C:\Python\lib\site-packages\tornado\concurrent.py", line 316, in _check_done
raise Exception("DummyFuture does not support blocking for results")
Exception: DummyFuture does not support blocking for results

Without being able to access the result of the future in the callback, being able to add a callback is of limited use to me.

Am I missing something - is there a way to get the result of the future in the callback?

In the asyncio documentation it seems to give an example where accessing the
method directly is possible:


...I'm not sure how this related to tornado/distributed, but it would be very useful to be able to do the same.

from distributed import Client

client = Client("")

def f(delay):
from time import sleep
from numpy.random import randn
if randn() > 1:
return delay

def callback(fut):
import logging
logger = logging.getLogger('distributed')
if fut.status == 'finished':
res = future._result().result() # <-------------- Doesn't work!
logger.info("{!r} - {!s}".format(fut, res))
logger.info("{!r} - {!s}".format(fut, fut.status))

args = rand(10)
futs = client.map(f, args)
for fut in futs:

Answer Source

Currently your callback gets called within the Tornado Event loop. If you want to get the result of the future you'll have to use the Tornado API.

Here is a minimal example:

In [1]: from distributed import Client
In [2]: client = Client()
In [3]: def inc(x):
   ...:     return x + 1
In [4]: from tornado import gen

In [5]: @gen.coroutine
   ...: def callback(future):
   ...:     result = yield future._result()
   ...:     print(result * 10)
In [6]: future = client.submit(inc, 1)

In [7]: future.add_done_callback(callback)


However, your question highlights that perhaps this is not the most intuitive way for users to interact with add_done_callback, so I wouldn't be surprised if we introduced a breaking change for later versions.

In [8]: import distributed

In [8]: distributed.__version__
Out[8]: '1.14.0'