Skip to content

Instantly share code, notes, and snippets.

@saikyun
Created September 13, 2021 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saikyun/9e6784245d287b7057ef78b4c04c58fd to your computer and use it in GitHub Desktop.
Save saikyun/9e6784245d287b7057ef78b4c04c58fd to your computer and use it in GitHub Desktop.
(def chan (ev/thread-chan))
(defn run-process
[args]
(ev/spawn-thread
(def p (os/spawn args :p {:in :pipe :out :pipe}))
(ev/give chan (:read (p :out) :all))
(:wait p)))
(run-process ["ls"])
(print "...blocking")
(pp (ev/take chan))
(run-process ["ls"])
(ev/spawn-thread
(print "not blocking, different thread")
(pp (ev/take chan)))
(run-process ["ls"])
(defn then
``
Will run each event loop cycle until chan has something to take.
Note, if another thread takes from chan, this might start blocking.
``
[chan f]
(ev/spawn
(if (pos? (ev/count chan))
(do (print "taking")
(f (ev/take chan)))
(then chan f))))
(then chan
(fn [v]
(print "not blocking, same thread")
(pp v)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment