Back

>! (clj)

(source)

function

(>! port val)
puts a val into port. nil values are not allowed. Must be called inside a (go ...) block. Will park if no buffer space is available. Returns true unless port is already closed.

Examples

core.async
;; The clojure.core.async namespace contains the public API.
(require '[clojure.core.async :as async :refer :all])

;; In ordinary threads, we use `>!!` (blocking put) and `<!!`
;; (blocking take) to communicate via channels.

(let [c (chan 10)]
  (>!! c "hello")
  (assert (= "hello" (<!! c)))
  (close! c))

(let [c (chan)]
  (thread (>!! c "hello"))
  (assert (= "hello" (<!! c)))
  (close! c))

;; The `go` macro asynchronously executes its body in a special pool
;; of threads. Channel operations that would block will pause
;; execution instead, blocking no threads. This mechanism encapsulates
;; the inversion of control that is external in event/callback
;; systems. Inside `go` blocks, we use `>!` (put) and `<!` (take).

;; Here we convert our prior channel example to use go blocks:
(let [c (chan)]
  (go (>! c "hello"))
  (assert (= "hello" (<!! (go (<! c)))))
  (close! c))

(let [c1 (chan)
      c2 (chan)]
  (thread (while true
            (let [[v ch] (alts!! [c1 c2])]
              (println "Read" v "from" ch))))
  (>!! c1 "hi")
  (>!! c2 "there"))

(let [c1 (chan)
      c2 (chan)]
  (go (while true
        (let [[v ch] (alts! [c1 c2])]
          (println "Read" v "from" ch))))
  (go (>! c1 "hi"))
  (go (>! c2 "there")))

(let [n 1000
      cs (repeatedly n chan)
      begin (System/currentTimeMillis)]
  (doseq [c cs] (go (>! c "hi")))
  (dotimes [i n]
    (let [[v c] (alts!! cs)]
      (assert (= "hi" v))))
  (println "Read" n "msgs in" (- (System/currentTimeMillis) begin) "ms"))
core.async
(require '[clojure.core.async :as async :refer [<! >! <!! >!! timeout chan alt! alts!! go]])

(defn fan-in [ins]
  (let [c (chan)]
   (future (while true
             (let [[x] (alts!! ins)]
               (>!! c x))))
   c))

(let [cout (chan)
      cin (fan-in (fan-out cout (repeatedly 3 chan)))]
  (dotimes [n 10]
    (>!! cout n)
    (prn (<!! cin))))
core.async
(require '[clojure.core.async :as async :refer [<!! >!! timeout chan alt!!]])

(defn fake-search [kind]
  (fn [c query]
    (future
     (<!! (timeout (rand-int 100)))
     (>!! c [kind query]))))

(defn google [query]
  (let [c (chan)
        t (timeout 80)]
    (future (>!! c (<!! (fastest query web1 web2))))
    (future (>!! c (<!! (fastest query image1 image2))))
    (future (>!! c (<!! (fastest query video1 video2))))
    (loop [i 0 ret []]
      (if (= i 3)
        ret
        (recur (inc i) (conj ret (alt!! [c t] ([v] v))))))))
core.async
(require '[clojure.core.async :as async :refer [<! >! timeout chan alt! alts! go]])

(defn fan-in [ins]
  (let [c (chan)]
    (go (while true
          (let [[x] (alts! ins)]
            (>! c x))))
    c))

(let [cout (chan)
      cin (fan-in (fan-out cout (repeatedly 3 chan)))]
  (go (dotimes [n 10]
        (>! cout n)
        (prn (<! cin))))
  nil)
core.async
(require '[clojure.core.async :as async :refer [<! >! <!! timeout chan alt! go]])

(defn fake-search [kind]
  (fn [c query]
    (go
     (<! (timeout (rand-int 100)))
     (>! c [kind query]))))

(defn google [query]
  (let [c (chan)
        t (timeout 80)]
    (go (>! c (<! (fastest query web1 web2))))
    (go (>! c (<! (fastest query image1 image2))))
    (go (>! c (<! (fastest query video1 video2))))
    (go (loop [i 0 ret []]
          (if (= i 3)
            ret
            (recur (inc i) (conj ret (alt! [c t] ([v] v)))))))))
clojure/core.async
;; The clojure.core.async namespace contains the public API.
(require '[clojure.core.async :as async :refer :all])

;; In ordinary threads, we use `>!!` (blocking put) and `<!!`
;; (blocking take) to communicate via channels.

(let [c (chan 10)]
  (>!! c "hello")
  (assert (= "hello" (<!! c)))
  (close! c))

(let [c (chan)]
  (thread (>!! c "hello"))
  (assert (= "hello" (<!! c)))
  (close! c))

;; The `go` macro asynchronously executes its body in a special pool
;; of threads. Channel operations that would block will pause
;; execution instead, blocking no threads. This mechanism encapsulates
;; the inversion of control that is external in event/callback
;; systems. Inside `go` blocks, we use `>!` (put) and `<!` (take).

;; Here we convert our prior channel example to use go blocks:
(let [c (chan)]
  (go (>! c "hello"))
  (assert (= "hello" (<!! (go (<! c)))))
  (close! c))

(let [c1 (chan)
      c2 (chan)]
  (thread (while true
            (let [[v ch] (alts!! [c1 c2])]
              (println "Read" v "from" ch))))
  (>!! c1 "hi")
  (>!! c2 "there"))

(let [c1 (chan)
      c2 (chan)]
  (go (while true
        (let [[v ch] (alts! [c1 c2])]
          (println "Read" v "from" ch))))
  (go (>! c1 "hi"))
  (go (>! c2 "there")))

(let [n 1000
      cs (repeatedly n chan)
      begin (System/currentTimeMillis)]
  (doseq [c cs] (go (>! c "hi")))
  (dotimes [i n]
    (let [[v c] (alts!! cs)]
      (assert (= "hi" v))))
  (println "Read" n "msgs in" (- (System/currentTimeMillis) begin) "ms"))
clj-commons/manifold
(ns manifold.stream.async
  {:no-doc true}
  (:require
    [manifold.deferred :as d]
    [clojure.core.async :as a]
    [manifold.stream
     [graph :as g]
     [core :as s]]
    [manifold
     [executor :as executor]
     [utils :as utils]])
  (:import
    [java.util.concurrent.atomic
     AtomicReference]))

        blocking?
        (try
          (a/>!! ch x)
          true)

        :else
        (let [d  (d/deferred)
              d' (.getAndSet last-put d)
              f  (fn [_]
                   (a/go
                     (d/success! d
                                 (boolean
                                   (a/>! ch x)))))]
          (if (d/realized? d')
            (f nil)
            (d/on-realized d' f f))
          d))))
unclebob/more-speech
(ns more-speech.websocket-relay-spec
  (:require [speclj.core :refer :all]
            [clojure.core.async :as async]
            [more-speech
             [relay :as relay]
             [websocket-relay :as ws-relay]]))

  (it "can send and receive"
    (pending "be nice to relay.damus.io")
    (let [chan (async/chan)
          recv-f (fn [relay msg] (async/>!! chan [relay msg]))
          relay (ws-relay/make "wss://relay.damus.io" recv-f)
          relay-open (relay/open relay)
          _ (relay/send relay-open ["test"])
          f-reply (future (async/<!! chan))
          [relay-r reply] (deref f-reply 1000 :timeout)
          _ (relay/close relay-open)]
      (should= relay relay-r )
      (should= ["NOTICE" "could not parse command"] reply)))
  )
duanebester/clunk
(require '[com.clunk.core :as clunk]
         '[clojure.core.async :as async]
         '[clojure.core.match :as m])

;; Receive Handler
(async/go-loop []
  (when-let [msg (async/<! (:in client))]
    (println (str "Received Message: " msg))
    (m/match msg
      {:type :AuthenticationMD5}
      (async/>! (:out client) (clunk/get-password username password (byte-array (:salt msg))))
      :else nil)
    (recur)))

(async/>!! (:out client) (clunk/get-startup username database))
;; (async/>!! (:out client) (clunk/get-query "select 1 as x, 2 as y;"))
(async/>!! (:out client) (clunk/get-query "select * from country limit 10;"))
diegopacheco/Diego-Pacheco-Sandbox
(ns coreasyncfun.core)
(require '[clojure.core.async :as async :refer :all])

;; In ordinary threads, we use `>!!` (blocking put) and `<!!`
;; (blocking take) to communicate via channels.
(println (let [c (chan 10)]
  (>!! c "hello")
  (assert (= "hello" (<!! c)))
  (close! c)))

;; Because these are blocking calls, if we try to put on an
;; unbuffered channel, we will block the main thread. We can use
;; `thread` (like `future`) to execute a body in a pool thread and
;; return a channel with the result. Here we launch a background task
;; to put "hello" on a channel, then read that value in the current thread.
(let [c (chan)]
  (thread (>!! c "hello"))
  (assert (= "hello" (<!! c)))
  (close! c))

;; The `go` macro asynchronously executes its body in a special pool
;; of threads. Channel operations that would block will pause
;; execution instead, blocking no threads. This mechanism encapsulates
;; the inversion of control that is external in event/callback
;; systems. Inside `go` blocks, we use `>!` (put) and `<!` (take).

;; Here we convert our prior channel example to use go blocks:
(let [c (chan)]
  (go (>! c "hello"))
  (assert (= "hello" (<!! (go (<! c)))))
  (close! c))