After a bit more digging - Timer is a background thread so clearly
that's not going to mesh well as a foreground blocking activity with
agent (which is what I get for plugging along one path and trying to
bring in another path without thinking things through). But the
question remains - any ideas for using Timer while blocking the
current thread? Is a future a better choice here?

On Nov 1, 4:19 pm, AndyK <andy.kri...@gmail.com> wrote:
> I would like to block the thread until an agent has done its thing -
> in this case serving as a cap on a timer. I had thought that wrapping
> a call to the timed-agent function with await would do just that, but
> apparently not. At least in the repl, the function returns immediately
> and you can follow along as the agent updates until it's finished.
>
> (defn timed-agent [limit f]
>   (let [a (agent 0)
>         t (java.util.Timer.)
>         tt (proxy [java.util.TimerTask] [] (run [] (send-off a (fn[v]
> (f) (inc v)))))]
>     (set-validator! a #(> limit %))
>     (.scheduleAtFixedRate t tt 1000 1000)
>     a))
>
> For example
> user> (await (timed-agent 20 #(println "running")))
> nil
> user>
> (returns immediately and 20 "running" strings will be printed)
>
> user> (def a (timed-agent 20 #(println "running")))
> #'user/a
> user> @a
> <whatever the value of a is at the time>
>
> Clearly I'm misunderstanding await + agents (not surprising - clojure
> is fairly new to me). Can someone clarify how await works and how one
> should block a thread for a timed task where you want a limit to the
> number of times it runs?
>
> thank you

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to