send-off

added
1.0

ns
clojure.core

type
function

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

                user=> (def my-agent (agent ""))
#'user/my-agent
user=> @my-agent
""

;; Note the following happens asynchronously in a thread
;; pool
user=> (send-off my-agent #(slurp %2) "file.txt")
#<Agent@13c6641: "">

;; while the slurp is in-progress, @my-agent will return "".

;; Once the request has completed, the value will
;; be updated when we look at it.
user=> @my-agent
"file contents"

            
                ;; send should be used for actions that are CPU limited,
;; while send-off is appropriate for actions that may block on IO.

;; send is like async/go, send-off is like async/thread
;; so send use limited pool by CPU for agents to not overload CPU,
;; while send-off use independent threads without limitations.