Clojure的期货和承诺有什么不同? [英] How do Clojure futures and promises differ?

查看:130
本文介绍了Clojure的期货和承诺有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

期货和承诺都会阻塞,直到他们计算出它们的价值,那么它们之间的区别是什么?

解决方案

Clojure条款,以下是来自 Sean Devlin的屏幕录像的一些示例:

 (def a-promise(promise))
(deliver a-promise:fred)

(def f(future(some-sexp)))
(deref f)

注意,在promise中你明确地传递一个你选择的值(:fred 在这种情况下)。另一方面,未来正在被创造的同一个地方消费。 some-expr 大概是在幕后启动的,最后是计算出来的,但是如果它在被访问的时候仍然未被评估,直到它可用。






编辑添加 b

要进一步区分承诺和未来,请注意以下几点:



承诺




  1. 您创建一个 promise 。该promise对象现在可以传递给任何线程。

  2. 继续计算。这些可以是非常复杂的计算,涉及副作用,下载数据,用户输入,数据库访问,其他承诺 - 无论你喜欢什么。

  3. 完成后,您可以传送结果到

  4. 在您完成计算之前,尝试 deref 的任何项目都会阻止完成。一旦完成,您已交付兑现承诺,该承诺就不会再阻止。



未来




  1. 创造未来。您的未来的一部分是用于计算的表达式。

  2. 未来可能同时执行,也可能不同时执行。它可以被分配一个线程,可能从一个池。它可以只等待,什么也不做。

  3. 在某些时候你(或另一个主题) deref 未来。如果计算已经完成,您会得到它的结果。如果它还没有完成,你阻塞,直到它有。 (假设如果它还没有开始, deref 它意味着它开始执行,但这也不能保证。)

虽然您可能使表达式与创建promise后的代码一样复杂,但这是令人怀疑的。这意味着期货真的更适合快速,可以背景的计算,而promise真的更适合大型,复杂的执行路径。太多,承诺似乎,在计算可用,有点更灵活和面向承诺创造者做工作和另一个线程收获收获。期货更倾向于自动启动一个线程(没有丑陋和容易出错的开销),并继续与其他东西,直到你 - 原始线程 - 需要结果。


Both futures and promises block until they have calculated their values, so what is the difference between them?

解决方案

Answering in Clojure terms, here are some examples from Sean Devlin's screencast:

(def a-promise (promise))
(deliver a-promise :fred)

(def f (future (some-sexp)))
(deref f)

Note that in the promise you are explicitly delivering a value that you select in a later computation (:fred in this case). The future, on the other hand, is being consumed in the same place that it was created. The some-expr is presumably launched behind the scenes and calculated in tandem (eventually), but if it remains unevaluated by the time it is accessed the thread blocks until it is available.


edited to add

To help further distinguish between a promise and a future, note the following:

promise

  1. You create a promise. That promise object can now be passed to any thread.
  2. You continue with calculations. These can be very complicated calculations involving side-effects, downloading data, user input, database access, other promises -- whatever you like. The code will look very much like your mainline code in any program.
  3. When you're finished, you can deliver the results to that promise object.
  4. Any item that tries to deref your promise before you're finished with your calculation will block until you're done. Once you're done and you've delivered the promise, the promise won't block any longer.

future

  1. You create your future. Part of your future is an expression for calculation.
  2. The future may or may not execute concurrently. It could be assigned a thread, possibly from a pool. It could just wait and do nothing. From your perspective you cannot tell.
  3. At some point you (or another thread) derefs the future. If the calculation has already completed, you get the results of it. If it has not already completed, you block until it has. (Presumably if it hasn't started yet, derefing it means that it starts to execute, but this, too, is not guaranteed.)

While you could make the expression in the future as complicated as the code that follows the creation of a promise, it's doubtful that's desirable. This means that futures are really more suited to quick, background-able calculations while promises are really more suited to large, complicated execution paths. Too, promises seem, in terms of calculations available, a little more flexible and oriented toward the promise creator doing the work and another thread reaping the harvest. Futures are more oriented toward automatically starting a thread (without the ugly and error-prone overhead) and going on with other things until you -- the originating thread -- need the results.

这篇关于Clojure的期货和承诺有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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