Skip to content

Instantly share code, notes, and snippets.

@BadUncleX
Last active April 11, 2018 05:26
Show Gist options
  • Save BadUncleX/0599a77fa37fc1450ba26b8d03110f41 to your computer and use it in GitHub Desktop.
Save BadUncleX/0599a77fa37fc1450ba26b8d03110f41 to your computer and use it in GitHub Desktop.
promise and deliver (also future)

promise vs future

reference: stackeroverflow

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.)

Both Future and Promise are mechanisms to communicate result of asynchronous computation from Producer to Consumer(s).

In case of Future the computation is defined at the time of Future creation and async execution begins “ASAP”. It also “knows” how to spawn an asynchronous computation.

Future的计算是立即执行

In case of Promise the computation, its start time and [possible] asynchronous invocation are decoupled from the delivery mechanism. When computation result is available Producer must call deliver explicitly, which also means that Producer controls when result becomes available.

Promise的计算是需要等待的, 即显式调用 deliver

For Promises Clojure makes a design mistake by using the same object (result of promise call) to both produce (deliver) and consume (deref) the result of computation. These are two very distinct capabilities and should be treated as such.

;;
;; shipping products for Order
;; 组装产品等待其他工序完成才可以完成.
;;
(defrecord Order [name price qty])
;; 统计总价
(defn merge-products [m1 m2]
;; meta data info `total-price`
{:total-price (+ (* (.price x) (.qty x)) (* (.price y) (.qty y)))}
[m1 m2])
;; 组装完产品后发货, 等待x,y完毕,即x和y的deliver
(defn ship-products [x y z]
(deliver z (merge-products @x @y))
(println "We can ship products " @z))
(def product-a (promise))
(def product-b (promise))
(def shipping-ab (promise))
(future (ship-products product-a product-b shipping-ab))
(deliver product-a (->Order "book" 10.1 5))
(deliver product-b (->Order "pencil" 2.1 10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment