What is the difference between Clojure's "send" and "send-off" functions with respect to dispatching an action to an agent?
Asked Answered
H

1

27

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?

Hexapla answered 29/10, 2009 at 20:31 Comment(0)
S
28

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.

Smothers answered 29/10, 2009 at 22:12 Comment(3)
send-off does not really create a new thread, it uses a different, expandable thread pool.Tyne
thanks: I'll edit to include that. thanks for pointing that out.Smothers
Seems to me that if you have enough additional resources and are doing things like IO where there is a lot of blocking, the send-off variant would be good, and if you are doing CPU intensive things, then the straight send variant would be more efficient?Rebuild

© 2022 - 2024 — McMap. All rights reserved.