Share your thoughts in the 2021 Clojure Community Survey!

Welcome! Please see the About page for a little more info on how this works.

0 votes
in Refs, agents, atoms by

(def a (agent 0))

(dotimes [_ 100]
(send-off a inc))

How to determine the 100 actions have all been finished?

1 Answer

+1 vote
by
selected by
 
Best answer

await does exactly this:
(clojure.repl/doc await)
clojure.core/await ([& agents]) Blocks the current thread (indefinitely!) until all actions dispatched thus far, from this thread or agent, to the agent(s) have occurred. Will block on failed agents. Will never return if a failed agent is restarted with :clear-actions true or shutdown-agents was called.

To try it out:
(def a (agent 0)) (dotimes [_ 100] (send-off a (fn [x] (Thread/sleep 30) (inc x)))) (await a)
(Will block for a few seconds)

...