Database Reference
In-Depth Information
sink (agent [])
counter (ref 0)
done (ref false) ]
(add-watch caster :counter
(partial watch-caster counter))
(send reader read-row caster sink done )
(wait-for-it 250 done)
{:results @sink
:count-watcher @counter} ))
6.
When we run the preceding code, we get the data about counts from the watcher:
user=> (:count-watcher (watch-processing (take 2 data-files)))
118095
How it works…
This time, instead of associating a validator with the agent that coerces the integers, we
called add-watch on it. Each time the agent is updated, the watch function is called with
four parameters: a key, the agent, the old state, and the new state. Our watch function irst
needs a counter reference, which we will supply by partially applying its parameters when
we call add-watch .
Once everything has been created, watch-processing just sends the input agent the irst
message, and then it waits for the processing to inish.
Debugging concurrent programs with
watchers
Watchers are not only good for logging and counting, but they are also useful for debugging.
If an agent or reference is not getting updated the way we expect, we can temporarily add a
watcher to track what's happening so we can see what's going on.
For this recipe, we'll continue the theme we've been working with for the last few recipes.
This time, instead of counting the data, the watch function will print the change of state to
the console.
 
Search WWH ::




Custom Search