简体   繁体   English

`(chan n)` 和 `(chan (buffer n))` 之间有什么区别吗?

[英]Any difference between `(chan n)` and `(chan (buffer n))`?

As the title asks, is there any difference between (chan n) and (chan (buffer n)) when in use?正如标题所问, (chan n)(chan (buffer n))在使用时有什么区别吗?

The question originates from the idea that I want to pull messages from a db (yeah, I do not want to use Kafka or RabbitMQ) and process them in order.问题源于我想从数据库中提取消息(是的,我不想使用 Kafka 或 RabbitMQ)并按顺序处理它们的想法。

So the code segment looks like:所以代码段看起来像:

(defn processer [id]
  (let [ch (chan (buffer 1000))]  ;;(chan 1000) or (chan (buffer 1000))?
    (go-loop []
      (when-let [msg (<! ch)]
        (process-msg msg))
      (recur))
    ch))

(defn enqueue [id]
  (let [ch (processer id)]
    (go-loop []
      (let [msgs (take-100-msg-from-db id)]
        (if (seq msgs)
          (doseq [msg msgs]
            (>! ch msg))
          (<! (timeout 100))))
      (recur))))

In my testing, their behaviours do not differ.在我的测试中,他们的行为没有区别。

Yes, they are the same.是的,它们是一样的。

You can always look at the source :您可以随时查看源代码

(defn chan
  ([] (chan nil))
  ([buf-or-n] (chan buf-or-n nil))
  ([buf-or-n xform] (chan buf-or-n xform nil))
  ([buf-or-n xform ex-handler]
     (channels/chan (if (number? buf-or-n) (buffer buf-or-n) buf-or-n) xform ex-handler)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM