What is an elagant/idiomatic way to achieve something like tuple unpacking with futures?
I have code like
a, b, c = f(x)
y = g(a, b)
z = h(y, c)
and I would like to convert it to use futures. Ideally I would like to write something like
a, b, c = ex.submit(f, x)
y = ex.submit(g, a, b)
z = ex.submit(h, y, c)
The first line of that throws
TypeError: 'Future' object is not iterable
though.
How can I get a,b,c
without having to make 3 additional ex.submit
calls? ie. I would like to avoid having to write this as:
import operator as op
fut = ex.submit(f, x)
a = client.submit(op.getitem, fut, 0)
b = client.submit(op.getitem, fut, i)
c = client.submit(op.getitem, fut, 2)
y = ex.submit(g, a, b)
z = ex.submit(h, y, c)
I guess a potential solution is to write an unpack
function like below,
import operator as op
def unpack(fut, n):
return [client.submit(op.getitem, fut, i) for i in range(n)]
a, b, c = unpack(ex.submit(f, x), 3)
y = ex.submit(g, a, b)
z = ex.submit(h, y, c)
which works: for example if you first define:
def f(x):
return range(x, x+3)
x = 5
g = op.add
h = op.mul
then you get
z.result() #===> 77
I thought something like this might already exist.
The above only works with dask.distributed.Future
. It does not work for plain concurrent.futures.Future
.