Clojure 的“发送”和“发送”有什么区别?和“送行”向代理分派操作的功能?

发布于 2024-08-09 13:41:39 字数 403 浏览 1 评论 0原文

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

(send af & args) - 向代理分派操作。立即返回代理。随后,在线程池中的线程中,代理的状态将设置为以下值:(apply action-fn state-of-agent args)

(send-off af & args) - 向代理分派潜在的阻塞操作。立即返回代理。随后,在一个单独的线程中,代理的状态将设置为以下值:(apply action-fn state-of-agent args)

唯一明显的区别是应该使用 send-off当一个动作可能被阻塞时。有人可以更详细地解释这种功能差异吗?

The Clojure API describes these two functions as:

(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)

and

(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)

The only obvious difference is send-off should be used when an action may block. Can somebody explain this difference in functionality in greater detail?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海夜未眠 2024-08-16 13:41:39

使用 send 发送到任何代理的所有操作都在线程池中运行,该线程池的线程数比处理器的物理数量多一些。这使得它们更接近 CPU 的满负荷运行。如果您使用 send 进行 1000 个调用,您实际上不会产生太多切换开销,无法立即处理的调用只需等待处理器可用即可。 如果它们阻塞,则线程池可能会耗尽。

当您使用send-off时,将为每个调用创建一个新线程。如果您send-off 1000个函数,无法立即处理的函数仍会等待下一个可用的处理器,但如果send-,它们可能会产生启动线程的额外开销关闭线程池恰好运行不足。 如果线程阻塞也没关系因为每个任务(可能)都有一个专用线程。

all the actions that get sent to any agent using send are run in a thread pool with a couple of more threads than the physical number of processors. this causes them to run closer to the cpu's full capacity. if you make 1000 calls using send you don't really incur much switching overhead, the calls that can't be processed immediately just wait until a processor becomes available. if they block then the thread pool can run dry.

when you use send-off, a new thread is created for each call. if you send-off 1000 functions, the ones that can't be processed immediately still wait for the next available processor, but they may incur the extra overhead of starting a thread if the send-off threadpool happens to be running low. it's ok if the threads block because each task (potentially) gets a dedicated thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文