gpt4 book ai didi

multithreading - 您可以在Clojure中使用 `send`和 `send-off`混合代理 Action 提交吗?应该发生什么?

转载 作者:行者123 更新时间:2023-12-03 13:16:52 26 4
gpt4 key购买 nike

我知道Clojure的“代理”是ref,带有“操作”的添加工作队列。 Action 是使用ref的值在第一个位置调用的函数,可以将其传递给其他参数。操作将返回ref的新值。因此,“代理”是一种计算ref的设备:=(action_ [n]°action_ [n-1]°action_ [n-2]°...°action_ [1]°action_ [0])(ref) ,可能是 Action 引起的副作用。

调用(send-off agent action & args)会导致action被系统分配给agent的单独线程排入队列以进行异步处理(据我所知,该线程来自可增长的线程池,并且在代理的队列为空后,该线程将停止或返回到游泳池)。

调用(send agent action & args)会导致action被线程从固定系统池中排队以进行异步处理,其中线程在属于多个代理的 Action 之间切换。在这种情况下,action应该避免太长时间阻塞或占用线程。

到目前为止,一切都很好,但是如果将某些 Action 用send入队,而某些 Action 用send-off入队到同一代理,会发生什么呢?当系统将操作从代理队列中出队时,系统是否会在其两种处理模式之间切换?

最佳答案

sendsend-off之间的唯一区别是使用哪个线程组。代理程序和线程的调度是(必须)分开进行的。

首先存在2个线程组的唯一原因是send可用于“快速运行”任务(例如,内存/非网络操作)。 “慢速”任务(例如网络调用)应该使用send-off,因此它们不会阻止“快速”任务。

“快”和“慢”的定义是模糊的,取决于用户。请注意,此处sendsend-off的语言有所不同(用于“潜在阻止的操作”,例如磁盘或网络I/O):

https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/send

更多信息在这里:https://clojure.org/reference/agents

The actions of all Agents get interleaved amongst threads in a thread pool. At any point in time, at most one action for each Agent is being executed. Actions dispatched to an agent from another single agent or thread will occur in the order they were sent, potentially interleaved with actions dispatched to the same agent from other sources. send should be used for actions that are CPU limited, while send-off is appropriate for actions that may block on IO.

关于multithreading - 您可以在Clojure中使用 `send`和 `send-off`混合代理 Action 提交吗?应该发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57662953/

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