Is there a way to be notified when a clojure future finishes?

前端 未结 5 1463
鱼传尺愫
鱼传尺愫 2021-02-04 14:56

Is there a way to set a watch on a future so that it triggers a callback when it is done?

something like this?

> (def a (future (Thread/sleep 1000) \"         


        
5条回答
  •  执笔经年
    2021-02-04 15:02

    For very simple cases: If you dont want to block and dont care about the result just add the callback in the future definition.

    (future (a-taking-time-computation) (the-callback))
    

    If you care about the result use comp with the call back

    (future (the-callback (a-taking-time-computation)))
    

    or

    (future (-> input a-taking-time-computation callback))
    

    Semantically speaking the java equivalent code would be:

    final MyCallBack callbackObj = new MyCallBack();
    new Thread() {
         public void run() {
             a-taking-time-computation();
             callbackObj.call();
         }
     }.start()
    

    For complex cases you may want to look:

    https://github.com/ztellman/manifold

    https://github.com/clojure/core.async

提交回复
热议问题