Hi all,

What is the best way to await for an agent in the following way:
1. if it's running a computation, wait until either it runs to
completion or throws an exception,
2. if it has errors, do nothing (because the computation has already
been terminated by an exception)?

I'm on Clojure 1.1. I currently have something along these lines:

(send-off agent my-computation)
(await agent)
(if (agent-errors agent)
  ;; handle these errors
  (clear-agent-errors agent))

Now, this sometimes works and sometimes doesn't get past the call to
await. Clearly, there's a race condition here: if the computation
happens to bomb out with an error before await is called (case 2
above), then the 'send' fails to run (which await calls under the
hood):

Exception in thread "main" java.lang.RuntimeException: Agent has
errors
        at clojure.lang.Agent.dispatch(Agent.java:145)
        at clojure.core$send__4845.doInvoke(core.clj:1351)
        at clojure.lang.RestFn.invoke(RestFn.java:430)
        at clojure.core$await__5254.doInvoke(core.clj:2114)

I might check whether agent has errors before I call await, but I fear
introducing another, more subtle race condition (what if the error
occurs between agent-errors and await?)

How best to cope with this?

Best regards,
Daniel Janus

-- 
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