Fork me on GitHub
#aleph
<
2018-05-25
>
tianshu05:05:48

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?

jetmind06:05:13

s/take! returns deferred, so it will block only if you deref the value s/take! returns

tianshu07:05:47

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 ?

mccraigmccraig08:05:47

@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

mccraigmccraig08:05:58

we don't ever use deref apart from in tests and the repl - there's no need for blocking

tianshu08:05:04

is that if my handler took 10s to response, the thread call deref will block 10s?

tianshu08:05:25

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?

mccraigmccraig08:05:50

@doglooksgood deref is not the async way!

mccraigmccraig08:05:53

you compose chains of operations on Deferreds with deferred/chain, and they don't block anything

mccraigmccraig08:05:52

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

mccraigmccraig08:05:01

use chain instead

tianshu08:05:13

I want to understand what what aleph will do when request handler return a d/deferred?

tianshu08:05:01

with your words, I think I understand a little

mccraigmccraig08:05:29

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

mccraigmccraig08:05:46

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

tianshu08:05:39

is it good or not to use core.async in a web server?

mccraigmccraig08:05:22

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

mccraigmccraig08:05:13

i tend to prefer manifold/aleph because i find the promise abstraction more natural for most of what a web/app server does

simple_smile 2
mccraigmccraig08:05:16

but if you ask in #core-async you'll probably get a different answer!