gpt4 book ai didi

clojure - Clojure 的 "send"和 "send-off"函数在向代理分派(dispatch)操作方面有什么区别?

转载 作者:行者123 更新时间:2023-12-03 10:28:06 26 4
gpt4 key购买 nike

Clojure API 将这两个函数描述为:

(send a f & args) - Dispatch an action to an agent. Returns the agent immediately. Subsequently, in a thread from a thread pool, the state of the agent will be set to the value of: (apply action-fn state-of-agent args)





(send-off a f & args) - Dispatch a potentially blocking action to an agent. Returns the agent immediately. Subsequently, in a separate thread, the state of the agent will be set to the value of: (apply action-fn state-of-agent args)



唯一明显的区别是 送行应该在 Action 可能阻塞时使用。有人可以更详细地解释这种功能差异吗?

最佳答案

使用 send 发送给任何代理的所有操作在线程池中运行,线程池的线程数比处理器的物理数量多。这使它们运行得更接近 CPU 的全部容量。如果您使用 send 调用 1000 次电话您并不会真正产生太多的切换开销,无法立即处理的调用只是等到处理器可用。如果它们阻塞,那么线程池可能会干涸。

当您使用 send-off ,为每个调用创建一个新线程。如果你 send-off 1000 个函数,无法立即处理的函数仍然等待下一个可用处理器,但如果 可能会导致启动线程的额外开销。送客线程池碰巧快用完了。如果线程阻塞是可以的,因为每个任务(可能)都有一个专用线程。

关于clojure - Clojure 的 "send"和 "send-off"函数在向代理分派(dispatch)操作方面有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1646351/

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