Skip to content

Instantly share code, notes, and snippets.

@lyo5ha
Created July 29, 2019 15:52
Show Gist options
  • Save lyo5ha/f8de26cb1e8cc9636ca357752d3c711e to your computer and use it in GitHub Desktop.
Save lyo5ha/f8de26cb1e8cc9636ca357752d3c711e to your computer and use it in GitHub Desktop.
Core async question
;; Время от времени появляется ошибка #object[clojure.core.async.impl.channels.ManyToManyChannel
;; Почему? По идее `random-add` никак не влияет на заполнение канала, только на таймаут?
(ns async-tea-party.core
(:require [clojure.core.async :as async]))
(def google-tea-service-chan (async/chan 10))
(def yahoo-tea-service-chan (async/chan 10))
(def result-chan (async/chan 10))
(defn random-add []
(reduce + (conj [] (repeat 1 (rand-int 100000)))))
(defn request-google-tea-service []
(async/go
(random-add)
(async/>! google-tea-service-chan
"tea compliments of google")))
(defn request-yahoo-tea-service []
(async/go
(random-add)
(async/>! yahoo-tea-service-chan
"tea compliments of yahoo")))
(defn request-tea []
(request-google-tea-service)
(request-yahoo-tea-service)
(async/go (let [[v] (async/alts!
[google-tea-service-chan
yahoo-tea-service-chan])]
(async/>! result-chan v))))
(defn -main [& args]
(println "Requested tea!")
(request-tea)
(println (async/<!! result-chan)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment