简体   繁体   English

Clojure - Core.async Pipeline + 混淆

[英]Clojure - Core.async Pipeline + take confusion

I'm having difficulty understanding what I thought was a pretty simple concept in Clojure's async library.我很难理解我认为 Clojure 的异步库中的一个非常简单的概念。 I'm essentially creating two channels with a pipeline, where the output channel is created using the take function of the input channel.我基本上是用管道创建两个通道,其中输出通道是使用输入通道的 take 函数创建的。

From my understanding, the purpose of take is to limit the number of items that a channel will receive before it closes itself (if the input channel has not already closed by this time).根据我的理解,take 的目的是限制通道在关闭之前将接收的项目数量(如果此时输入通道尚未关闭)。 However, the code examples I've been playing with aren't producing the results I expected.但是,我一直在使用的代码示例并没有产生我预期的结果。

Take the following code for example:以下面的代码为例:

(def in (chan 1))
(def out (async/take 5 in 1))

(doseq [i (range 10)]
  (go (>! in i)))

(pipeline 4 out (filter even?) in)

(go-loop []
  (when-some [val (<! out)]
    (println val)
    (recur))))

What I expected to happen was that the pipeline would filter out odd numbers, and only pass even numbers to the 'out' channel, when the out channel had received 5 even numbers it would close.我期望发生的是管道将过滤掉奇数,并且只将偶数传递给“输出”通道,当输出通道收到 5 个偶数时它会关闭。 However what I saw was both odd and even numbers printed to the REPL, something like the following:然而,我看到的是打印到 REPL 的奇数和偶数,如下所示:

2 7 4 0 8 6 2 7 4 0 8 6

At this point the out channel still hadn't closed and running the doseq a second time would print some other value before finally closing.此时输出通道还没有关闭,第二次运行 doseq 会在最终关闭之前打印一些其他值。

I'm incredibly perplexed as to what's going on here, it works like a charm when using take and not the pipeline and it also works when not using take but still using the pipeline, using the two in combination is a whole different story it seems.我对这里发生的事情感到非常困惑,它在使用 take 而不是管道时就像一个魅力一样,在不使用 take 但仍然使用管道时也有效,将两者结合使用似乎是一个完全不同的故事. Am I missing something obvious here?我在这里遗漏了一些明显的东西吗? Apologies if this is a simple mistake, this is my first (albeit naive) attempt at using core.async.抱歉,如果这是一个简单的错误,这是我第一次(虽然很幼稚)尝试使用 core.async。

You have placed take and pipeline in competition.您已将takepipeline置于竞争中。 Both of them are taking items from in and adding them to out .他们都从in取出项目并将它们添加到out Replace the definition of out :替换out的定义:

(def out (async/chan 3))

for example, and get the expected result例如,并获得预期的结果

0
2
4
6
8

If you really want to use async/take , you could do it like so:如果你真的想使用async/take ,你可以这样做:

(def first (async/chan 1))
(def second (async/chan 3))
(pipeline 4 second (filter even?) first)
(def third (async/take 3 second))

(defn run []
  (go
    (doseq [i (range 10)]
      (>! first i)))
  (go (loop []
        (when-some [val (<! third)]
          (println val)
          (recur)))))

with result:结果:

0
2
4

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

相关问题 在core.test中关闭Clojure core.async - Clojure core.async in core.test 使用Clojure core.async限制进程 - Throttling Processes using Clojure core.async Clojure-Apache Kafka的core.async接口 - Clojure - core.async interface for apache kafka Clojure core.async和clojure.core命名空间元素似乎发生冲突 - Clojure core.async and clojure.core namespace elements appear to collide Clojure的core.async是否类似于Jane Street的OCaml Core Async? - Is Clojure's core.async similar to Jane Street's OCaml Core Async? Scala Async是否完成了Clojure的core.async所做的一切? - Does Scala Async do everything that Clojure's core.async does? 何时使用非阻塞>! /线程和阻止> !! / goroutines with clojure core.async - When to use non-blocking >! / threads and blocking >!! / goroutines with clojure core.async 如何确保将所有数据写入clojure的core.async通道中的文件? - how can I insure all data is written to a file from clojure's core.async channels? 如何使用代理和core.async在Clojure中正确地异步记录? - How do you use an agent and core.async to properly log asynchronously in Clojure? Clojure core.async,异常“在clojure.lang.Var.popThreadBindings(Var.java:364)处没有匹配推送的Pop”的含义是什么? - Clojure core.async, what is the meaning of exception “Pop without matching push, at clojure.lang.Var.popThreadBindings(Var.java:364) ”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM