需要澄清 Scala 中的 futures 和 promises [英] Clarification needed about futures and promises in Scala

查看:33
本文介绍了需要澄清 Scala 中的 futures 和 promises的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力理解 Scala 的承诺和未来的结构.

I am trying to get my head around Scala's promise and future constructs.

我一直在阅读 Scala 文档中的Futures and Promises 并且有点困惑我有一种感觉,Promise 和 Future 的概念混在一起了.

I've been reading Futures and Promises in Scala Documentation and am a bit confused as I've got a feeling that the concepts of promises and futures are mixed up.

在我的理解中,promise 是一个我们可以填充的容器值在稍后点.而未来是某种异步将在不同的执行路径中完成的操作.

In my understanding a promise is a container that we could populate value in a later point. And future is some sort of an asynchronous operation that would complete in a different execution path.

在 Scala 中,我们可以使用附加的回调函数获取结果.

In Scala we can obtain a result using the attached callbacks to future.

我迷失的地方是承诺如何拥有未来?

Where I'm lost is how promise has a future?

我也在 Clojure 中阅读了这些概念,假设 promise 和 future 有一些通用的通用概念,但似乎我错了.

I have read about these concepts in Clojure too, assuming that promise and future have some generic common concept, but it seems like I was wrong.

promise p 完成 p.future 返回的未来.这个未来是特定于承诺 p.根据实现,它可能是p.future eq p 的情况.

A promise p completes the future returned by p.future. This future is specific to the promise p. Depending on the implementation, it may be the case that p.future eq p.

val p = promise[T]
val f = p.future

推荐答案

您可以将 future 和 promise 视为管道的两个不同方面.在promise端,数据被推入,在future端,数据可以被拉出.

You can think of futures and promises as two different sides of a pipe. On the promise side, data is pushed in, and on the future side, data can be pulled out.

未来是某种异步操作,它将在不同的执行路径中完成.

And future is some sort of an asynchronous operation that would complete in a different execution path.

实际上,未来是一个占位符对象,用于在某个时间点异步可用的值.它不是异步计算本身.

Actually, a future is a placeholder object for a value that may be become available at some point in time, asynchronously. It is not the asynchronous computation itself.

事实上有一个名为 future 的未来构造函数返回这样一个占位符对象并且产生一个异步计算来完成这个占位符对象并不意味着异步计算被称为未来.还有其他未来的构造函数/工厂方法.

The fact that there is a future constructor called future that returns such a placeholder object and spawns an asynchronous computation that completes this placeholder object does not mean that the asynchronous computation is called a future. There are also other future constructors/factory methods.

但我不明白的一点是承诺如何有未来?

But the point I do not get is how promise has a future?

将 promises 和 futures 分成 2 个独立的接口是一个设计决定.您可以在同一接口 Future 下使用这两个,但这将允许期货的客户完成它们,而不是未来的预期完成者.这会导致意外错误,因为可能有任意数量的竞争完成者.

To divide promises and futures into 2 separate interfaces was a design decision. You could have these two under the same interface Future, but that would then allow clients of futures to complete them instead of the intended completer of the future. This would cause unexpected errors, as there could be any number of contending completers.

例如对于由 future 构造产生的异步计算,它是否必须完成承诺,或者客户端是否会这样做将不再清楚.

E.g. for the asynchronous computation spawned by the future construct, it would no longer be clear whether it has to complete the promise, or if the client will do it.

Future 和 promise 旨在限制程序中的数据流.这个想法是让未来的客户端订阅数据,以便在数据到达后对其进行操作.承诺客户端的作用是提供该数据.混合这两种角色可能会导致程序更难理解或推理.

Futures and promises are intended to constrain the flow of data in the program. The idea is to have a future client that subscribes to the data to act on it once the data arrives. The role of the promise client is to provide that data. Mixing these two roles can lead to programs that are harder to understand or reason about.

您可能还会问为什么 Promise 特性不扩展 Future.这是另一个设计决策,旨在阻止程序员盲目地将 Promise 传递给客户端,他们应该将 Promise 向上转换为 Future(这种向上转换很容易导致被排除在外,而必须在承诺上显式调用 future 可确保您每次都调用它).换句话说,通过返回承诺,您将完成它的权利授予其他人,通过返回未来,您将获得订阅它的权利.

You might also ask why the Promise trait does not extend Future. This is another design decision to discourage programmers from blindly passing Promises to clients where they should upcast the Promise to Future (this upcast is prone to be left out, whereas having to explicitly call future on the promise ensures you call it every time). In other words, by returning a promise you are giving the right to complete it to somebody else, and by returning the future you are giving the right to subscribe to it.

如果您想了解有关 future 的更多信息,请参阅 Learning Concurrent Programming in Scala 一书中的第 4 章详细介绍了它们.免责声明:我是这本书的作者.

If you would like to learn more about futures, Chapter 4 in the Learning Concurrent Programming in Scala book describes them in detail. Disclaimer: I'm the author of the book.

这篇关于需要澄清 Scala 中的 futures 和 promises的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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