Fork me on GitHub
#core-async
<
2017-07-13
>
joachim12:07:54

Hi All, I've got sth I don't understand. When I define async-get-page as follows:

(defn async-get-page [i]
    (let [c (as/chan)]
      (org.httpkit.client/get (format "" i)
                              (fn [ret]
                                (as/go (as/>! c i))))
      (as/<!! c)))
  
Then the following works fine when N<8, but completely blocks all async computations when N >= 8:
(dotimes [i N]
    (as/go (async-get-page i)))
  
What is going on?

tbaldridge12:07:47

@joachim You're calling a blocking operation <!! within the calling context of a go. That blocks the go thread, of which there are only 8

tbaldridge12:07:13

return the channel from async-get-page, then call <! from within your go block

tbaldridge12:07:11

Also, (as/go (as/>! c i)) is semantically the same as (as/put! c i) except the latter is much faster and requires less memory overhead.

joachim12:07:33

Right, makes sense. Thanks a lot!

hiredman15:07:33

you shouldn't have a go block just to put i in to a channel

hiredman15:07:31

(if you read the docs and look at examples, besides blocking, it is called out as the one thing not to do)

hiredman15:07:17

get rid of the go block there and use >!!(my preference) or put!(another option)