Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

clojure - Can't seem to require >!! or <!! in Clojurescript?

I must be missing something very obvious here but I'm trying to setup a very basic program to put an item onto a channel then block until I can take it off again. The entire program is below:

(ns shopping-2.core
  (:require [cljs.core.async :as async :refer [>!! <!! put! chan <! close! <!!]]))

(let [c (chan)]
  (>!! c "hello")
  (.write js/document (<!! c))
  (close! c))

The JavaScript error I'm getting is:

Uncaught TypeError: Cannot call method 'call' of undefined 

I had that error before when I forgot to :refer chan in (if I just open the channel, then close it again the program runs fine)

However this code seems to choke when I want to use the <!! or >!! macros.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are some differences on what's available in clojurescript from the clojure version of core.async.

Because clojure on the JVM has real threads, it makes available both concurrency patterns with real threads and with go blocks:

  1. Real threads use the macro thread to enclose the core.async magic and its concurrency macros and functions end with two bangs, like <!!, >!!, alt!! and alts!!.

  2. Inversion of control threads (fake threads) use the go macro to enclose the core.async magic and uses the functions with one bang at the end, like <!, >!, alt! and alts!.

In clojurescript (which runs in js) there are no real threads, so only the IoC (inversion of control) threads are available, which means that you have to use the second variant of the concurrency constructs.

Your example would be like:

(ns shopping-2.core
  (:require-macros [cljs.core.async.macros :refer [go]])
  (:require [cljs.core.async :as async :refer [put! chan <! >! close!]]))

(go
  (let [c (chan)]
    (>! c "hello")
    (.write js/document (<! c))
    (close! c)))

Anyway, that example has a concurrency problem, since the <! >! functions are blocking and you are putting into a chan in the same routine, the routine will block on the (>! c "hello") instruction and it will never read, definitely starving your program.

You could fix this using the put! fn which puts without blocking, or efectively running those instructions in different routines which I think demonstrates better what you intended to do.

(ns shopping-2.core
  (:require-macros [cljs.core.async.macros :refer [go]])
  (:require [cljs.core.async :as async :refer [put! chan <! >! close!]]))

;; put! version
(go
  (let [c (chan)]
    (put! c "hello")
    (.write js/document (<! c))
    (close! c)))


;; Concurrent version
;; 2 *threads* concurrently running. One is the putter and the other is the
;; reader
(let [c (chan)]
  (go
    (.write js/document (<! c))
    (close! c))
  (go
    (>! c "hello")))

In the concurrent threads version, you will see that even the code that runs first is the read, it is effectively another routine so the code that runs later (the >!) effectively runs unblocking the first routine.

You can think of the go macro as spawning a new thread that will eventually start executing concurrently and that returns control to the next instructions of code after it immediately.

I suggest reading the code walk-through ignoring the clojure specific parts (>!! <!! etc) and some of the swannodette's tutorials which are great (like Clojurescript 101 and Communicating Sequential Processes)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...