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.
My questions are:
handle_call
rather than handle_cast
?Therefore, Chatting on WhatsApp is an example of Synchronous communication. Asynchronous communication refers to any form of communication in which one person sends information and the recipients must wait a certain amount of time to process it and respond.
Asynchronous Messaging, also called “async messaging,” is a communication method where a message is placed in a queue and does not require an immediate response to move forward with processing. Samples of asynchronous messages include email and SMS, where both parties are free to respond on their own time.
While synchronous messaging is a live person-to-person conversation, asynchronous communication doesn't require both parties to be present and speaking at the same time. This is great for the customer because they are able to start, pause, and resume a conversation around their life.
Asynchronous communication is better for working with different time zones as it creates a permanent record of ideas, decisions, and discussions. Basically, synchronous communications happen in real-time, where asynchronous communications happen over a period of time.
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, use handle_cast
. When handle_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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With