我想包裝功能內的去例程(如下面的方法1)。方法2工作得很好,但方法1沒有。唯一的區別是在方法1中,我將一個通道作爲參數傳遞給一個函數,並且該函數位於函數內部。關於例程和函數的確切規則是什麼?圍繞功能調用去例行程序
(defn doit [ch i]
(print "g\n")
(async/>! ch i)
(print "f\n"))
;方法1
(let [c1 (async/chan)]
(async/go (while true
(let [[v ch] (async/alts! [c1])]
(println "Read" v "from" ch))))
(dotimes [i 10]
(async/go (doit c1 i))))
;方法2
(let [ch (async/chan)]
(async/go (while true
(let [[v ch] (async/alts! [ch])]
(println "Read" v "from" ch))))
(dotimes [i 10]
(async/go
(print "g\n")
(async/>! ch i)
(print "f\n"))))
我也注意到,如果我刪除了去的方法1,並將其移動到這樣做的功能,如下圖所示,該功能打印「g」但不是「f」,但其他方式正常工作。爲什麼?
(defn doit [ch i]
(async/go
(print "g\n")
(async/>! ch i)
(print "f\n")))
好的,謝謝。這仍然不能解釋爲什麼當我將宏移動到doit函數時,不會打印「f」字符串。 – Tim
@Tim當用'go'塊評估'doit'函數時,在同一個REPL會話中(用'lein repl'開始),我看到'f's打印的交錯字符串'Read v from# ManyToManyChannel ...>'。您沒有看到它打印的原因可能與您沒有看到該異常的原因相同(即輸出異步發生,您的nREPL客戶端不能很好地處理)。 –