Eric
Eric

Reputation: 23

Agents calling agents

I'm trying to wrap my head around what happens when agents call agents.

(def top (agent 0))
(def bottom (agent 1))

I have a minimal pair:

(defn testA []
  "This returns 'top', whose value is 'bottom', whose value is 2."
  (send top (fn [top-value]
          (send bottom inc)))
  (await top)
  top)

(defn testB []
  "This never terminates."
  (send top (fn [top-value]
          (send bottom inc)
          (await bottom) ;;<- this is new
          bottom))
  (await top)
  top)

What is happening with the inner await? What factors come into play when one agent calls another?

Thanks,

Upvotes: 2

Views: 276

Answers (1)

ivant
ivant

Reputation: 3919

Short answer is, you cannot use await in agent action. You can see the error (if you break from current waiting) with (agent-error top)

For longer answer (explaining why you can't do that) you'll have to (a)wait some clojure guru :) My take is, that you can introduce dead-locks or some other disaster.

Also note, that using top or bottom returns the agent itself, not its value. To get the value, you need (deref top) or @top for short.

Upvotes: 2

Related Questions