gpt4 book ai didi

Clojure core.async, channel 与端口

转载 作者:行者123 更新时间:2023-12-04 06:36:03 24 4
gpt4 key购买 nike

在 Clojure core.async 中, channel 和端口是一回事吗?如果不是,有什么区别?在观看视频Timothy Baldridge - Core.Async ,他创建了一个 channel

(def c (chan))

然后稍后
(<!! c)
c是 channel ,但 <!! 的文档状态(强调添加)

Usage: (<!! port) takes a val from port. Will return nil if closed. Will block if nothing is available.



core.async docs不清楚.

最佳答案

是的,chans 是端口。

端口是这些实现的协议(protocol)的名称

(defprotocol ReadPort
(take! [port fn1-handler] "derefable val if taken, nil if take was enqueued"))

impl/take 使用在:
(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))))

并且名称 port 在整个 async.clj 中非常一致地使用。从概念上讲,这很有用,因为并非 core.async 处理的所有内容都是 channel 。其他东西可以实现 ReadPort 和 WritePort,因此可以很好地与 core.async 配合使用。

关于Clojure core.async, channel 与端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29996881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com