python期货和元组拆包 [英] python futures and tuple unpacking

查看:55
本文介绍了python期货和元组拆包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是用愚蠢的/惯用的方法来实现诸如将元组与期货拆包之类的东西?

What is an elagant/idiomatic way to achieve something like tuple unpacking with futures?

我有类似的代码

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)

该行的第一行

TypeError: 'Future' object is not iterable

虽然.如何获得 a,b,c 而不必进行3个其他的 ex.submit 调用?IE.我想避免这样写:

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)


我猜可能的解决方案是编写一个如下所示的 unpack 函数,

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

那么你就得到

z.result() #===> 77

我认为类似的东西可能已经存在.

I thought something like this might already exist.

以上内容仅适用于 dask.distributed.Future .它不适用于普通的 concurrent.futures.Future .

The above only works with dask.distributed.Future. It does not work for plain concurrent.futures.Future.

推荐答案

快速浏览:

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 返回一个 Future 对象,而不是运行 f(x)的结果.

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

这个SO答案表明,链接期货并非无关紧要:

This SO answer indicates that chaining futures is not trivial:

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆