python futures and tuple unpacking

故事扮演 提交于 2021-01-27 18:45:50

问题


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.


回答1:


A quick glance at:

https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Future

suggests that you'll have to do something like

afuture = ex.submit(f, x)
a,b,c = afuture.result()
...

submit returns a Future object, not the result of running f(x).

This SO answer indicates that chaining futures is not trivial:

How to chain futures in a non-blocking manner? That is, how to use one future as an input in another future without blocking?



来源:https://stackoverflow.com/questions/46334728/python-futures-and-tuple-unpacking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!