Created
August 27, 2021 13:53
-
-
Save bakpakin/f9d59ac184c2ad1930e81ef3bd08b3a7 to your computer and use it in GitHub Desktop.
Try (and fail) to repro thread issue.
This file contains hidden or 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
(defn parallel | |
[jobs] | |
(print "Beginning " (length jobs) " jobs...") | |
(let [results-chan (ev/thread-chan 0) | |
res @{}] | |
(each job jobs | |
(ev/thread (fiber/new job) results-chan :n)) | |
(while (< (length res) (length jobs)) | |
(let [[k v] (ev/take results-chan)] | |
(print "job " k " finished") | |
(put res k v))) | |
(print "Finished jobs.") | |
res)) | |
(defn randsleep | |
[] | |
(math/seedrandom (os/cryptorand 16)) | |
(os/sleep (math/random))) | |
(def jobs | |
[(fn [x] (randsleep) (ev/give x [:a "A"])) | |
(fn [x] (randsleep) (ev/give x [:b "B"])) | |
(fn [x] (randsleep) (ev/give x [:c stdout])) | |
(fn [x] (randsleep) (ev/give x [:d "D"]))]) | |
(pp (parallel jobs)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment