Hi, according to the example, I can use (s/take! (s/->source a-core-async-channel))
in request-handler. will s/take!
block the current thread until async channel have a item?
s/take!
returns deferred, so it will block only if you deref the value s/take!
returns
will deref
happen in the same thread which handle the request? if it is, it's likely if request handler took 10s, there will be two thread each took 10s ?
@doglooksgood deref
blocks the thread doing the deref
- Deferred
is all callbacks underneath the hood, so provided your request handling is itself non-blocking there will be no other blocking
we don't ever use deref
apart from in tests and the repl - there's no need for blocking
is that if my handler took 10s to response, the thread call deref
will block 10s?
correct
there's a single thread do nothing except blocking for this defref
? this is strange, for this case, in the old sync way, we only need one thread, cost 10s to handle the request. in the async way, we need two threads, and cost them each 10s?
@doglooksgood deref
is not the async way!
have a read of https://github.com/ztellman/manifold/blob/master/docs/deferred.md
you compose chains of operations on Deferred
s with deferred/chain
, and they don't block anything
summary: if you deref
a promise then you are doing async wrong. the only time to use deref
is in a test or to inspect a value at the repl
use chain
instead
or alternatively, use the cats manifold monad https://github.com/funcool/cats/blob/master/src/cats/labs/manifold.clj with the mlet
macro sugar http://funcool.github.io/cats/latest/#mlet
I'm just reading the example of aleph and found this: https://github.com/ztellman/aleph/blob/master/examples/src/aleph/examples/http.clj#L58
I want to understand what what aleph will do when request handler return a d/deferred
?
with your words, I think I understand a little
the Deferred
is really just a placeholder for callbacks which will be called with the value gotten from the go
block... you take the Deferred
returned from your (delayed-hello-world-handler req)
call, and d/chain
some functions to it, which do something with the returned value
if you've worked with js promises, a Deferred
is very similar to a js promise (at least the client api is very similar) - a convenient way of arranging chains of callbacks
is it good or not to use core.async in a web server?
if your webserver is implemented with core.async then maybe go with it... if your webserver is (like aleph) implemented with manifold then go with that
i tend to prefer manifold/aleph because i find the promise abstraction more natural for most of what a web/app server does
but if you ask in #core-async you'll probably get a different answer!