The downside of #'thread (just as with send-off) is that it can use arbitrarily many threads for performing IO ops. And while some IO tasks benefit from performing them from multiple threads (for fairness, perf), others don't (e.g. writing to a log, AFAICT). And in no case one wants to spawn a thousand threads...
That's why I used send-via: arbitrarily many go blocks can request IO, but the resources dedicated to satisfy the demand will be bounded. Surely #'thread is a good default for channel work, but as I see it, agents add interesting, finer-grained capabilities as well. On Mon, Jul 8, 2013 at 6:23 PM, Timothy Baldridge <[email protected]>wrote: > Eh, you're right, there is however, a blocking version of go, called > thread: > > So perhaps something like this? > > (thread > (while true > (let [[filename response-chan] (<!! c)] > (>!! response-chan (slurp filename))))) > > Or perhaps I'm missing something. > > Timothy > > > On Mon, Jul 8, 2013 at 10:09 AM, vemv <[email protected]> wrote: > >> Took me a while to get the idea but higher-order channels are brilliant - >> that way one ensures a given reply was actually targeted at one's request. >> Thanks for the suggestion! >> >> Faux-agents would have the limitation of not being to (reasonably) >> perform blocking I/O - which is the point of my sample program. >> >> An improved version: >> >> (require '[clojure.core.async :as async :refer :all]) >> (import '[java.util.concurrent Executors] >> '[java.util.concurrent.locks ReentrantReadWriteLock]) >> >> >> (spit "bar" (apply str (shuffle (range 100)))) >> >> (def max-concurrency 4) >> >> (def requests (chan max-concurrency)) >> >> ;; only ensures exclusive access within the app - not across the OS >> ;; too lazy to use a FileLock :) >> (def lock (ReentrantReadWriteLock.)) >> >> (def rw-pool (Executors/newFixedThreadPool max-concurrency)) >> >> ;; one sends actions to anonymous one-off multiple agents >> ;; (as opposed to a single, named one), in order to increase concurrency >> >> (dotimes [_ max-concurrency] >> (go (loop [] >> (when-let [request (<! requests)] >> (send-via rw-pool (agent nil) (fn [_] >> (try >> (-> lock .readLock .lock) >> (>!! request (seq (slurp >> "bar"))) >> (finally >> (-> lock .readLock >> .unlock))))) >> (recur))))) >> >> (go (loop [] >> (let [request (chan) >> _ (>! requests request) >> response (<! request)] >> (send-via rw-pool (agent nil) (fn [_] >> (try >> (-> lock .writeLock .lock) >> (spit "bar" (apply str (shuffle >> response))) >> (finally >> (-> lock .writeLock >> .unlock))))) >> (recur)))) >> >> As commented, locking is only partially useful (that's why opening the >> file might display an empty string at times). Also, don't know what would >> be the best way to indicate termination to the second go block... >> >> -- >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to [email protected] >> Note that posts from new members are moderated - please be patient with >> your first post. >> To unsubscribe from this group, send email to >> [email protected] >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en >> --- >> You received this message because you are subscribed to the Google Groups >> "Clojure" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > > > > -- > “One of the main causes of the fall of the Roman Empire was that–lacking > zero–they had no way to indicate successful termination of their C > programs.” > (Robert Firth) > > -- > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to [email protected] > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > --- > You received this message because you are subscribed to a topic in the > Google Groups "Clojure" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/clojure/C5oE09GJzo8/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > [email protected]. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
