multithreading - future 与线程 : Which is better for working with channels in core. 异步?

标签 multithreading asynchronous clojure core.async

使用 channel 时,为 future推荐或者是thread ?是否有时间 future更有意义?

Rich Hickey 在 core.async 上的博文建议使用 thread而不是 future :

While you can use these operations on threads created with e.g. future, there is also a macro, thread , analogous to go, that will launch a first-class thread and similarly return a channel, and should be preferred over future for channel work.



~ http://clojure.com/blog/2013/06/28/clojure-core-async-channels.html

但是,core.async 示例广泛使用了future。使用 channel 时:
(defn fake-search [kind]
  (fn [c query]
    (future
     (<!! (timeout (rand-int 100)))
     (>!! c [kind query]))))

~ https://github.com/clojure/core.async/blob/master/examples/ex-async.clj

最佳答案

概括

一般情况下,thread对于应用程序中 channel 突出的部分,它的 channel 返回可能会更方便。另一方面,应用程序中的任何子系统在其边界与某些 channel 接口(interface)但内部不使用 core.async 时,应该可以随意以对它们最有意义的方式启动线程。
thread 之间的差异和 future
正如您引用的 core.async 博客文章的片段中所指出的那样,thread返回一个 channel ,就像 go :

(let [c (thread :foo)]
  (<!! c))
;= :foo

channel 由大小为 1 的缓冲区支持,并在 thread 的主体返回值后关闭。表格放在上面。 (除非返回的值恰好是 nil ,在这种情况下, channel 将被关闭而不放置任何东西——core.async channel 不接受 nil 。)

这使得 thread与 core.async 的其余部分完美契合。特别是,这意味着 go + 单次爆炸操作和 thread + double-bang ops在代码结构上的使用方式确实相同,您可以使用alt!中的返回 channel /alts! (以及双爆炸等价物)等等。

相比之下,future 的返回可以是deref 'd ( @ ) 以获取 future 的返回值表单的主体(可能是 nil )。这使得 future非常适合不使用 channel 的常规 Clojure 代码。

使用的线程池还有另一个区别——thread使用 core.async 特定的线程池,而 future使用代理支持池之一。

当然所有的双爆操作,以及put!take! ,无论调用它们的线程的启动方式如何,都可以正常工作。

关于multithreading - future 与线程 : Which is better for working with channels in core. 异步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20625390/

相关文章:

python - 如何在 tensorflow 中使用自定义 python 函数预取数据

javascript - 您可以直接使用 .then 调用异步函数/方法吗?

clojure - Clojure future 和 Promise 有何不同?

clojure - 什么是有状态传感器?

java - 我需要使用 Java+OpenGL 构建哪些 Ubuntu/Debian 软件包?

c# - 确定两个 BackGroundWorker 字符串是否返回 true

java - Schedulers.newElastic 和 Schedulers.elastic 方法有什么区别?

c# - ASP.Net - Global.asax 的存在阻止并行请求执行?

javascript - 窗口本身加载后如何触发加载事件?

c - 用于异步 I/O 的 librt 和 libeio 之间有什么区别,我为什么要选择其中之一?