简体   繁体   English

如何使用core.async正确批处理邮件?

[英]How to properly batch messages with core.async?

I would like to batch messages on a core.async chan by count and timeout, (ie 10ms or 10 messages, whichever comes first). 我想通过计数超时在core.async chan上批量消息(即10ms或10条消息,以先到者为准)。 Tim Baldridge has a video on batching , but it uses deprecated functions in core.async and does not use transducers. Tim Baldridge 有关于批处理的视频 ,但它在core.async中使用了已弃用的函数,并且不使用传感器。 I'm looking for something like the following... 我正在寻找类似以下的东西......

(defn batch [in out max-time max-count]
  ...
 )

Transducers shouldn't really be a concern for a batching function – as a taker on the in channel, it will see values transformed by any transducers on that channel, and any takers listening on out will in turn see values transformed by that channel's transducer. 传感器真的不应该是一个批处理功能的关注-作为对一个考生in通过该通道上的任何转换器转换频道,就会看到价值,并听取对任何考生out将依次看到由该通道的转换器转换后的值。

As for an implementation, the function below will take batches of max-count items from in , or however many arrive by max-time since the last batch was output, and output them to out , closing when the input channel closes, subject to the input channel's transducer (if any, and any takers listening on out will also have that channel's transducer applied as noted above): 至于实现,下面的函数将采取分批max-count项来自in ,或者不过多以到达max-time以来的最后一批被输出,并输出到out ,收盘时输入通道关闭,受输入通道的换能器(如果有的话,并监听任何接受者out也将具有应用的信道的换能器如上面所指出的):

(defn batch [in out max-time max-count]
  (let [lim-1 (dec max-count)]
    (async/go-loop [buf [] t (async/timeout max-time)]
      (let [[v p] (async/alts! [in t])]
        (cond
          (= p t)
          (do
            (async/>! out buf)
            (recur [] (async/timeout max-time)))

          (nil? v)
          (if (seq buf)
            (async/>! out buf))

          (== (count buf) lim-1)
          (do
            (async/>! out (conj buf v))
            (recur [] (async/timeout max-time)))

          :else
          (recur (conj buf v) t))))))

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

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