[英]Clojure core.async, channel vs port
In Clojure core.async, are channels and ports the same thing? 在Clojure core.async中,通道和端口是一回事吗? If not what's the difference?
如果不是有什么区别? In watching the video Timothy Baldridge - Core.Async , he creates a channel
在观看视频Timothy Baldridge - Core.Async时 ,他创建了一个频道
(def c (chan))
Then later 然后
(<!! c)
c
is channel yet the docs for <!!
c
是渠道还是<!!
的文档 state (emphasis added) 国家(重点补充)
Usage: (<!! port) takes a val from port .
用法:(<!! port)从端口获取val。 Will return nil if closed.
如果关闭,将返回nil。 Will block if nothing is available.
如果没有可用的话会阻止。
It's not clear looking at the core.async docs . 目前还不清楚核心.async文档 。
Yes, chans are ports. 是的,chans是端口。
port is in the name of the protocol that these implement port是这些实现的协议的名称
(defprotocol ReadPort
(take! [port fn1-handler] "derefable val if taken, nil if take was enqueued"))
which is used by impl/take
in: impl/take
in使用的:
(defn <!!
"takes a val from port. Will return nil if closed. Will block if nothing is available."
[port]
(let [p (promise)
ret (impl/take! port (fn-handler (fn [v] (deliver p v))))]
(if ret
@ret
(deref p))))
and the name port is used very consistently throughout async.clj. 并且在整个async.clj中使用名称port非常一致。 Conceptually this is useful because not everything that core.async works on will be a channel.
从概念上讲,这很有用,因为并非core.async工作的所有内容都是一个通道。 other things can implement ReadPort and WritePort and therefore play nicely with core.async.
其他东西可以实现ReadPort和WritePort,因此可以很好地与core.async一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.