使用future并行运行sql:但是不执行sql

时间:2015-06-25 06:50:06

标签: clojure future

我有以下功能

(defn run [x]          
  (doseq [i  (range 1 x)]
     (println i)
     (future (j/execute! vertica-db ["insert /*+ direct */ into a select * from a limit 1"]))
   ))

使用

进行调用时
(run 100)

它将打印1..99,但是如果检查表a的行号,则行号不会增加,这意味着不执行sql。如何并行运行sql?

1 个答案:

答案 0 :(得分:3)

我在你的代码中看到的唯一可疑的事实是你永远不会等待期货结束(所以也许他们没有?)。

您需要收集future次调用返回的值,然后使用(deref f) / @f(即取消引用未来)阻止它们完成,其中f是一个这些价值观。

这样的事情应该有效:

(defn run [x]
  (let [db-insert (fn [i] ((println i) (future (j/execute! vertica-db ["insert /*+ direct */ into a select * from a limit 1"]))))
        inserts (doall (map db-insert (range 1 x)))] ;force execution of all the db-insert calls
        (doseq [insert inserts] @insert))) ;wait for all the futures to finish