core-async

Ivan Koz 2019-04-30T19:39:08.117800Z

do we consider pipe as a simple version of merge or mix? i mean it looks very similar to both.

alexmiller 2019-04-30T20:10:47.119400Z

I consider it just to be what it is - a means of connecting two channels

Ivan Koz 2019-04-30T22:58:34.120Z

https://gyazo.com/029c68de8ce3a51a719883d5e87ee780.png Say we don't need the timeout, can it be written better?

(comment
  ;; search async, repllicated, take first
  (let [fake-search (fn [kind query]
                      (Thread/sleep (rand-int 100))
                      (format "%s search result for '%s'" kind query))
        fastest (fn [query ep1 ep2]
                  (go (alt!
                        (go (fake-search ep1 query)) ([v] v)
                        (go (fake-search ep2 query)) ([v] v))))
        search (fn [query]
                 (->> (async/merge [(fastest query "web 1" "web 2")
                                    (fastest query "image 1" "image 2")
                                    (fastest query "video 1" "video 2")])
                      (async/into [])))]
    (time (<!! (search "clojure")))))

2019-04-30T23:25:37.122800Z

you should never call Thread/sleep inside a go block

2019-04-30T23:26:17.123300Z

you absolutely should be using timeout channels and not faking it like that

Ivan Koz 2019-04-30T23:34:40.124500Z

@hiredman i don't understand why, there is literally no difference between blocking operation and thread sleep inside a function

2019-04-30T23:35:15.125300Z

you should not have blocking operations inside go blocks

Ivan Koz 2019-04-30T23:35:27.125600Z

i know we puy long operations on async/thread

2019-04-30T23:35:31.125900Z

only parking (channel) operations

Ivan Koz 2019-04-30T23:35:35.126100Z

but whats about clojurescript then?

Ivan Koz 2019-04-30T23:35:54.126500Z

so no blocking calls at all?

2019-04-30T23:36:02.126800Z

correct

2019-04-30T23:36:35.128Z

clojurescript inherits javascripts model, there is a single thread, any blocking/long running operations ever will block anything else

2019-04-30T23:38:23.130Z

the threadpool used to run go blocks on the jvm is not single threaded, but that just means you can include blocking things like sleeps and it will work when you test at a small scale, and things will bog down and stop once you are at a large enough scale to saturate the threadpool

2019-04-30T23:38:48.130600Z

async/thread isn't for "long operations" it is for blocking operations

Ivan Koz 2019-04-30T23:38:50.130700Z

yeah thats explains it

2019-04-30T23:39:46.131900Z

the channel ops like >! and <! appear to block but don't actually block and are sometimes said to "park" instead

Ivan Koz 2019-04-30T23:41:05.133200Z

so to sum up; do whatever but don't take thread-pool threads for your long span\blocking calls

Ivan Koz 2019-04-30T23:44:31.135Z

i'm just trying to comprehend what implications we have running csp on a threadpool, rather than green-threads\fibers

2019-04-30T23:45:46.135400Z

it is both

Ivan Koz 2019-04-30T23:46:11.136300Z

looking at that you wrote, i make conclusion that any IO task in a go block is a no no

2019-04-30T23:46:21.136700Z

go blocks are basically green threads, and those green threads are run on the real threads in the threadpool

Ivan Koz 2019-04-30T23:47:04.137500Z

yeah but you can block green thread

2019-04-30T23:47:16.137800Z

it depends on the implementation

2019-04-30T23:47:44.138700Z

most of the time you cannot add green threads as a library, so the entire runtime system is built on around non-blocking io

2019-04-30T23:48:43.139700Z

so most green thread runtimes turn what appears to be blocking io calls in to non-blocking calls

2019-04-30T23:50:00.141200Z

go doesn't do that, but does some hand wavy stuff with it detects a go routine is blocked, giving the go routine its own thread

2019-04-30T23:50:59.142100Z

(hand waving by me because I don't remember and I think it has maybe changed since the last time I read anything about it)

Ivan Koz 2019-04-30T23:52:06.143100Z

so inside go block, we should give any long-span\blocking operation it's own thread, except parking stuff

2019-04-30T23:52:52.143900Z

yes, and generally you want to park on the result of calls to async/thread

Ivan Koz 2019-04-30T23:53:16.144300Z

yeah it makes it clear now, thanks

Ivan Koz 2019-04-30T23:54:10.144600Z

aside from that above code is fine right?

2019-04-30T23:55:30.144900Z

yeah

2019-04-30T23:55:43.145300Z

alts! might be nicer then alt! there

Ivan Koz 2019-04-30T23:55:55.145800Z

same with timeout