Disclaimer: erlang newbie.
One of the things that attracted me to erlang in the first place is the Actor model; the idea that different processes run concurrently and interact via asynchronous messaging.
I'm just starting to get my teeth into OTP and in particular looking at gen_server. All the examples I've seen - and granted they are tutorial type examples - use handle_call()
rather than handle_cast()
to implement module behaviour.
I find that a little confusing. As far as I can tell, handle_call is a synchronous operation: the caller is blocked until the callee completes and returns. Which seems to run counter to the async message passing philosophy.
I'm about to start a new OTP application. This seems like a fundamental architectural decision so I want to be sure I understand before embarking.
To be specific, my questions are:
- In real practice do people tend to use handle_call rather than handle_cast?
- If so, what's the scalability impact when multiple clients can call the same process/module?
Thanks.
IMO, in concurrent world
handle_call
is generally a bad idea. Say we have process A (gen_server) receiving some event (user pressed a button), and then casting message to process B (gen_server) requesting heavy processing of this pressed button. Process B can spawn sub-process C, which in turn cast message back to A when ready (of to B which cast message to A then). During processing time both A and B are ready to accept new requests. When A receives cast message from C (or B) it e.g. displays result to the user. Of course, it is possible that second button will be processed before first, so A should probably accumulate results in proper order. Blocking A and B throughhandle_call
will make this system single-threaded (though will solve ordering problem)In fact, spawning C is similar to
handle_call
, the difference is that C is highly specialized, process just "one message" and exits after that. B is supposed to have other functionality (e.g. limit number of workers, control timeouts), otherwise C could be spawned from A.Edit: C is asynchronous also, so spawning C it is not similar to
handle_call
(B is not blocked).Adam's answer is great, but I have one point to add
This is always true for the client who made the handle_call call. This took me a while to wrap my head around but this doesn't necessarily mean the gen_server also has to block when answering the handle_call.
In my case, I encountered this when I created a database handling gen_server and deliberately wrote a query that executed
SELECT pg_sleep(10)
, which is PostgreSQL-speak for "sleep for 10 seconds", and was my way of testing for very expensive queries. My challenge: I don't want the database gen_server to sit there waiting for the database to finish!My solution was to use gen_server:reply/2:
In code:
Depends on your situation.
If you want to get a result,
handle_call
is really common. If you're not interested in the result of the call, usehandle_cast
. Whenhandle_call
is used, the caller will block, yes. This is most of time okay. Let's take a look at an example.If you have a web server, that returns contents of files to clients, you'll be able to handle multiple clients. Each client have to wait for the contents of files to be read, so using
handle_call
in such a scenario would be perfectly fine (stupid example aside).When you really need the behavior of sending a request, doing some other processing and then getting the reply later, typically two calls are used (for example, one cast and the one call to get the result) or normal message passing. But this is a fairly rare case.
Using
handle_call
will block the process for the duration of the call. This will lead to clients queuing up to get their replies and thus the whole thing will run in sequence.If you want parallel code, you have to write parallel code. The only way to do that is to run multiple processes.
So, to summarize:
handle_call
will block the caller and occupy the process called for the duration of the call.There are two ways to go with this. One is to change to using an event management approach. The one I am using is to use cast as shown...
And the cast/submit code is...
The reference is used to track the query asynchronously.