Created
September 13, 2021 09:42
-
-
Save saikyun/9e6784245d287b7057ef78b4c04c58fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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