Should someone put `while true` inside the clojure core.async thread?

I have this producer / consumer pattern that I did with core.async functions thread:

(defn -db-producer-factory [order-ids-chan next-chan]
  (thread
    (while true
      (do
        (let [order-id (<!! order-ids-chan)]
          (condp = order-id
            :finished (do
                        (>!! next-chan :finished))
            (supress-w-nextexc
              (->>
                ; get denorm'd order
                (-> (r/-get-order :live order-id)
                    denorm/order->denormalized)
                ; put in a map to avoid nils
                (hash-map :data)
                (>!! next-chan)))))))))

However, when I read the documentation for thread, he says:

Executes the body in another thread, immediately returning to the calling thread. Returns the channel that will receive the result of the body after completion.

It looks like its pending stream is called one-time; not that it is built for a cycle whilewithin it.

Should I not do while truein a block thread? Or will the stream be cleared when I close threadthe chan result?

+4
source share
2

, , , while true. .

, go -routines ( , , ) . , , go -routine, - go - .

, , - , , :

(defn -db-producer-factory [order-ids-chan next-chan]
  (go-loop [order-id (<! order-ids-chan)]
    (condp = order-id

      nil
      ;; exiting
      nil

      :finished (do
                  (>! next-chan :finished)
                  (recur (<! order-ids-chan)))
      (do
        (supress-w-nextexc
         (->>
          (-> (r/-get-order :live order-id)
              denorm/order->denormalized)
          (hash-map :data)
          (>! next-chan)))
        (recur (<! order-ids-chan))))))

thread go. "" thread, . , (thread (loop ....

+1

. : http://www.braveclojure.com/core-async/

:

, go, youre , .

- , Java. ( ?), .

JVM .

0

All Articles