I suspect there are a number of things going on which are interacting to cause your confusion. It isn't the use of an agent inside a go-loop that is the problem per se.
send
is asynchronous, so there is no guarantee that your call to deref
in log-to-file
will see the change at the time log-to-file
is called inside the loop.
Also you are incorrectly using for
with side-effecting code (file io with spit
). It looks like you are trying to use it analogously to an imperative for-each loop in other languages. doseq
is the correct clojure construct for processing a sequence for side-effects (see: Difference between doseq and for in Clojure).
You also do nothing to remove the content of the vector being built up in the agent so even if things were running synchronously you would for example get on adding :a, :b and :c one at time an output in the file like:
:a
:a
:b
:a
:b
:c
It is not clear from the question if that is the behavior you expect from your code.
An aside on clojure.string/join:
As a general point using spit as above is very inefficient even if you were correctly using doseq
if you just wish to output a vector or other collection to a newline separated file. It is much better to use clojure.string/join
e.g.
user> (print (clojure.string/join "\n" [:a :b :c :d]))
:a
:b
:c
:d
You can use the pprint
function to see what the string actually passed to print looked like with escaping (pprint
is much more general that just this use and gives a nice visual format to all sorts of clojure data):
user> (clojure.pprint/pprint (clojure.string/join "\n" [:a :b :c :d]))
":a\n:b\n:c\n:d"