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

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

问题描述

future 和 promises 都会阻塞,直到它们计算出它们的值,那么它们之间有什么区别?

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

推荐答案

用 Clojure 术语回答,这里有一些来自 的例子Sean Devlin 的截屏视频:

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)

请注意,在 promise 中,您明确提供了一个您在稍后计算中选择的值(在本例中为 :fred).另一方面,未来在它被创造的同一个地方被消费.some-expr 大概是在幕后启动并串联计算(最终),但如果它在访问时仍未被评估,则线程会阻塞直到它可用.

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.

编辑添加

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

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

  1. 您创建了一个承诺.该承诺对象现在可以传递给任何线程.
  2. 您继续计算.这些可能是非常复杂的计算,涉及副作用、下载数据、用户输入、数据库访问、其他承诺——无论你喜欢什么.代码看起来很像您在任何程序中的主线代码.
  3. 完成后,您可以交付结果到该承诺对象.
  4. 在您完成计算之前尝试deref您的承诺的任何项目都将阻塞,直到您完成.一旦你完成并交付承诺,承诺将不再阻塞.
  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.

未来

  1. 你创造你的未来.你未来的一部分是计算的表达.
  2. 未来可能会也可能不会同时执行.它可以被分配一个线程,可能来自一个池.它只能等待,什么也不做.从你的角度来看你无法分辨.
  3. 在某些时候,您(或其他线程)derefs 是未来.如果计算已经完成,您将获得计算结果.如果它还没有完成,你就会阻塞直到它完成.(大概如果它还没有开始,derefing 意味着它开始执行,但这也不能保证.)
  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.)

虽然您可以使将来的表达式变得像创建承诺之后的代码一样复杂,但它是否可取是值得怀疑的.这意味着期货真的更适合快速、后台计算,而承诺更适合大型、复杂的执行路径.同样,就可用计算而言,promise 似乎更加灵活,并且面向 promise 创建者进行工作和另一个收获收获的线程.Futures 更倾向于自动启动一个线程(没有丑陋且容易出错的开销)并继续处理其他事情,直到您 - 原始线程 - 需要结果.

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天全站免登陆