我正在使用代理来操纵结构,但我没有所有的副作用。
所有消息都已发送(我已打印并计算过它们),但有时我没有所有的副作用。好像不是我的所有功能都应用于代理的状态,或者如果最后一次发送应用于先前的状态..
我尝试了doall,dorun,但没有找到解决方案,感谢任何帮助。
;; aux function for adding an element to a hashmap
(defn extend-regs [reg s o]
(let [os (get reg s)]
(if (nil? os)
(assoc reg s [o])
(assoc reg s (conj os o)))))
;; the agent's altering function - adding an element to the :regs field(a hashmap)
(defn add-reg! [d s o]
(send d (fn [a] (assoc a :regs (extend-regs (:regs a) s o)))))
;; Creating the agents, dct/init returns an agent
;; pds: data for fields
(defn pdcts->init-dcts! [pds]
(doall (map dct/init (map :nam pds) (repeat nil))))
;; Altering one agent's state, dct/add-reg sends an assoc message to the agent
;; d: agent, pd: data for fields
(defn dct->add-regs! [d pd]
(dorun (map (fn [s r] (dct/add-reg! d s r))
(:syms pd)
(:regs pd)))
d)
;; Going through all agents
;; ds: agents, pds: datas
(defn dcts->add-regs! [ds pds]
(dorun (map (fn [d pd] (dct->add-regs! d pd))
ds
pds))
ds)
编辑:================================================= =====
好吧,事实证明我只是没有等到我的线程完成他们的任务。现在的问题是如何监控我的代理。我怎么知道队列中有未完成的线程?我只找到了 swank.core/ active-threads和类似的,但它们不是解决方案。